MailJet nested loops

ⅰ亾dé卋堺 提交于 2019-12-07 00:19:34

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]]]
                          ]
                       ]    
                  ]
      ]

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.

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

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