9

I am trying to use json arrays in my email using Guide template language and the issue is I need to stop showing the divider line on the last iteration of {{#each}} loop. I have tried {{@last}} or any other private variables within an {{#each}} block.

{{.dataobject content type=variable source=@content_topstories}}
                                                                {{.data}}
                                                                  {"target":"@content_topstories"}
                                                                {{/data}}
                                                              {{/dataobject}}



<table border="0" cellspacing="0" cellpadding="0" width="100%">
  <tr>
    <td style="padding: 15px 0 15px 0;">
      <a style="text-transform: uppercase; text-decoration: none; font-weight: 700; font-family: sans-serif; color: #5c5c5c; font-size: 17px;" href=""></a>
    </td>
  </tr>
</table>


    {{#each content}}                                                          
<table border="0" cellspacing="0" cellpadding="0" width="100%">
  <tr>
    <td style="padding-bottom: 5px;">
      <a style="color: #0972d3; font-weight: 200; line-height: 29px; font-size: 24px; text-decoration: none; font-family: sans-serif" href="{{=RedirectTo(itemURL)}}">{{itemHeading}}</a>
    </td>
  </tr>
  <tr>
    <td>
      <table border="0" cellspacing="0" cellpadding="0" width="100%">
        <tr>
          <td style="font-family: sans-serif; color: #5c5c5c; line-height: 20px; font-size: 16px; padding-bottom: 15px; padding-top: 0; padding-right: 10px;">{{itemText}}
        </tr>
      </table>
      {{#if empty(itemImage)}}
      {{.else}}
      <table border="0" cellspacing="0" cellpadding="0" width="100%">
        <tr>
          <td style="padding-bottom: 25px;">
            <a href="{{=RedirectTo(itemURL)}}"><img class="mobile" style="background: #eee; width:100%; height:auto!important; display: block;" border="0" src="{{itemImage}}" width="100%" height="auto"></a>
          </td>
        </tr>
      </table>
      {{/if}}
    </td>
  </tr>
</table>
{{#if @last}}
<table border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td style="font-size:1px; line-height:1px; border-top: 2px solid #d3d3d3; padding-bottom: 25px;">&nbsp;</td></tr></table>
{{/if}}
{{/each}}
  • Would you try removing {{#if @last }} block entirely and move the inside table after {{/each}} {{/each}} <table border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td style="font-size:1px; line-height:1px; border-top: 2px solid #d3d3d3; padding-bottom: 25px;">&nbsp;</td></tr></table> – Fayyaz A Mian Oct 9 '17 at 14:03
  • @FayyazAMian I have tried this and didn't seem to work – Brad Sapkota Oct 10 '17 at 4:16
  • @BradSapkota did you manage to solve this issue ? Thanks ! – zeljazouli Sep 21 '18 at 13:10
  • this is something I haven't had a chance to retest. I assume it hasn't been resolved since there is no mention of updates for GTL in release notes @zeljazouli – Brad Sapkota Sep 22 '18 at 13:20
2

If you parse the JSON using SSJS, you can use the .length function to determine if you are at the end of a JSON object and accomplish what you are trying to do in your example.

I don't believe GTL has a .length function, and I'm not sure what is populating the @last attribute.

This example below can work with GTL, but by doing this you will be looping the JSON object twice, which may add increased CPU handling when sending an email.

  • Set JSON object and AMPscript Variables
%%[

var @JSON
set @JSON = '
     [
          {
               "emailaddress":"john@example.com",
               "Region":"West",
               "State":"California",
               "City":"San Francisco"
          },
          {
               "emailaddress":"carla@example.com",
               "Region":"Central",
               "State":"Indiana",
               "City":"Indianapolis"
          },
          {
               "emailaddress":"jim@example.com",
               "Region":"West",
               "State":"Utah",
               "City":"Salt Lake City"
          }          
     ]
'

var @JSONLENGTH
var @COUNTER

set @JSONLENGTH = 0
set @COUNTER = 0

]%%
  • Set the dataobject using GTL
{{.dataobject JSONvar type=variable}}
     {{.data}}
          { "target" : "@JSON" }
     {{/data}}
{{/dataobject}}
  • Parse JSON, first pass, to get length
{{#each JSONvar}}
     %%[ set @JSONLENGTH = Add(1, @JSONLENGTH) ]%%
{{/each}}
  • Parse JSON, second pass, to dynamically set HTML and decide action to take or not take at the end of the JSON object
{{#each JSONvar}}
     %%[ set @COUNTER = Add(1, @COUNTER) ]%%

     {{emailaddress}}<br/>

     {{#if @COUNTER!=@JSONLENGTH}}
          <hr>
     {{/if}}
{{/each}}

This should result in the following, where the line break did not get added at the end of the JSON Object.

HTML Result

| improve this answer | |
  • I appreciate the workaround using AMPScript and GTL combined, however, the resolution is to only use GTL. – Brad Sapkota Jul 7 '19 at 22:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.