MailJet nested loops

无人久伴 提交于 2019-12-08 03:33:26

问题


we are building our emails with mailJet which works fine so far. Our current problem is, that we would like to have a loop within a loop within a loop (so nested loops) the first loop works fine, and also the second inner loop is working fine, but the third one isn't working anymore:

{% for trip in var:trips %}
{{trip.id}}

{% for tripSegment in trip.tripSegments %}

{% for tripBreak in tripSegment.breaks %}
{{tripBreak.duration}}
{% endfor %}

{% endfor %}

Distance: {{trip.totalDistance}} km

{% endfor %}

This is in our template. If I remove the inner loop with the breaks, everything is fine.

If I enable the Template Error Reporting I get the following message:

X-MJ-ErrorMessage: "tripSegment.breaks" is not an array value

but if I try to print it with {{tripSegment.breaks}} I get the message:

X-MJ-ErrorMessage: Array values cannot be printed: tripSegment.breaks

and the field is definetly an array, like the trips or tripSegments, so basically it should work.

What am I doing wrong for nested loops in MailJet?

Edit:

My Object Structure, I send to Mailjet looks like this:

"Vars": {
    "trips": [
        "id": 1,
        "tripSegments":[
            {
                "id":2,
                "distance": 100,
                "breaks":[
                    {
                        "duration":15
                    },{
                        "duration":20
                    }
                ]
            },{
                "id":3,
                "distance": 200,
                "breaks":[
                    {
                        "duration":10
                    },{
                        "duration":30
                    }
                ]
            },{
                "id":4,
                "distance": 200,
                "breaks":[
                ]
            },
        ]
    ]
}

回答1:


If you would like to loop on tripSegment.breaks, it should be represented as an array and not as an object.

I managed to create a sample which works for me with the same values you use.

'Vars' => [ 
        "trips"=>[ 
            'trips1' =>[
        'id'=> 123,
        'totalDistance'=> 10, 
        'tripSegments' => [ 
            ['breaks' =>[['duration'=>1],['duration'=> 2]]],
            ['breaks' =>[['duration'=>1],['duration'=> 2]]],
            ['breaks' =>[['duration'=>1],['duration'=> 2]]]
                          ]
                       ]    
                  ]
      ]



回答2:


I find this has nothing to with the WYSIWYG Editor, remains a bug in Mailjet's templating language, and is easily reproducible. Use Postman or other API testing tool to send the following JSON to the send endpoint (make sure you add your API key credentials via Basic Auth):

POST https://api.mailjet.com/v3/send

{
    "FromEmail": "me@example.com",
    "FromName": "Me",
    "Subject": "Test",
    "MJ-TemplateLanguage": true,
    "MJ-TemplateErrorReporting": "me@example.com",
    "MJ-TemplateErrorDeliver": "deliver",
    "Recipients": [
        { "Email": "me@example.com" }
    ],
    "Html-part": "<ul>{% for project in var:commissions.projects %}<li>{{project.name}}</li>{% endfor %}</ul>",
    "Vars": {
        "commissions": { "total": "235,000", "projects": [] }
    }
  }

You'll get an error report via email back to you with the content No value for "commissions.projects" instead of it simply passing through the loop silently.




回答3:


As we were in discussion with the MailJet Support [1], it turns out that there is a bug in the WYSIWYG editor of MailJet.

the multiple nested loops work, if you use them within a HTML Block and not directly in the WYSIWYG Editor.

They are working on that.

[1] https://app.mailjet.com/support/ticket/21e111b3be8630214cc082845f6cf976



来源:https://stackoverflow.com/questions/39869123/mailjet-nested-loops

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!