2

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":[
                ]
            },
        ]
    ]
}
Alexander Kiefer
  • 546
  • 1
  • 13
  • 30

3 Answers3

3

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]]]
                          ]
                       ]    
                  ]
      ]
Zhivko
  • 301
  • 1
  • 3
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.

0

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

Alexander Kiefer
  • 546
  • 1
  • 13
  • 30
  • When clicking on your link: `This ticket was opened with another Maijet account. Please sign in with the good account to access this ticket.`. Can you provide the ticket answer on this post please? – Seb33300 Jun 20 '18 at 07:35
  • I'm sorry, as I do not work at this company anymore, I do not have access to this account anymore.... – Alexander Kiefer Jun 21 '18 at 06:52