3

I have an Order Confirmation email that lists out the order items. The DE for the email send has a text field for the order items, which stores a json array of the items, as below:

[
{"ProductImageUrl":"www.xyz.com/productaimage","ProductName":"Product A","ProductPrice":"$20"},
{"ProductImageUrl":"www.xyz.com/productbimage","ProductName":"Product B","ProductPrice":"$30"},
{"ProductImageUrl":"www.xyz.com/productcimage","ProductName":"Product C","ProductPrice":"$40"},
{"ProductImageUrl":"www.xyz.com/productdimage","ProductName":"Product D","ProductPrice":"$50"}
]

AMPscript using DataObject parses these out into items to be repeated in the email. All is working fine.

Importantly I am using a pre-built custom content block for the repeat, which is referenced within the #each tags. This content block has the image on the left and text details on the right:

Current state:

{{#each ItemsData}} 
    
%%=ContentBlockById("1234")=%%
    
{{/each}}

Which is effectively this:

%%=ContentBlockById("1234")=%%
%%=ContentBlockById("1234")=%%
%%=ContentBlockById("1234")=%%
%%=ContentBlockById("1234")=%%

Which renders like...

 ____________________________________
|                 |                  |
| PRODUCT A IMAGE | Product A Name   |
|                 | Product A Price  |
|_________________|__________________|

 ____________________________________
|                 |                  |
| PRODUCT B IMAGE | Product B Name   |
|                 | Product B Price  |
|_________________|__________________|

 ____________________________________
|                 |                  |
| PRODUCT C IMAGE | Product C Name   |
|                 | Product C Price  |
|_________________|__________________|

 ____________________________________
|                 |                  |
| PRODUCT D IMAGE | Product D Name   |
|                 | Product D Price  |
|_________________|__________________|

However, I now want to use two blocks, where the second one is reversed (ie text details on the left and image on the right) - basically the list of products alternates between block A and block B, so on. Eg:

{{#each ItemsData}} 
    
%%=ContentBlockById("1234")=%%
%%=ContentBlockById("5678")=%%
    
{{/each}} 

Which is effectively this:

%%=ContentBlockById("1234")=%%
%%=ContentBlockById("5678")=%%
%%=ContentBlockById("1234")=%%
%%=ContentBlockById("5678")=%%

To render this:

 ____________________________________
|                 |                  |
| PRODUCT A IMAGE | Product A Name   |
|                 | Product A Price  |
|_________________|__________________|

 ____________________________________
|                 |                  |
| Product B Name  | PRODUCT B IMAGE  |
| Product B Price |                  |
|_________________|__________________|

 ____________________________________
|                 |                  |
| PRODUCT C IMAGE | Product C Name   |
|                 | Product C Price  |
|_________________|__________________|

 ____________________________________
|                 |                  |
| Product D Name  | PRODUCT D IMAGE  |
| Product D Price |                  |
|_________________|__________________|

Is this possible to do using my current setup? I'm not sure how to approach this. I have a feeling I might need to count rows and then check for an odd/even rowCount number, but hoping there is an easier way.

Thanks

2 Answers 2

2

I'd count the rows as you suggested -- then you can alternate them using the mod AMPscript function:

%%[ set @count = 0 ]%%

{{#each ItemsData}} 

  %%[ set @count = add(@count,1) ]%%

  %%[ if mod(@count,2) == 0 then ]%%

    %%=ContentBlockById("1234")=%%
    %%=ContentBlockById("5678")=%%

  %%[ else ]%%

    %%=ContentBlockById("5678")=%%
    %%=ContentBlockById("1234")=%%

  %%[ endif ]%%

{{/each}} 

I echo @Rafel's sentiment -- I avoid GTL for limitations such as this. If it's a JSON payload I'm using SSJS.

Update

Here's my test:

%%[

set @json = '[{"sku":"123","name":"Square","url":"https://example.com?sku=123"},{"sku":"456","name":"Circle","url":"https://example.com/?sku=456"},{"sku":"789","name":"Triangle","url":"https://example.com/?sku=789"}]'

set @count = 0

]%%

%%[ if not empty(@json) then ]%%

{{.dataobject JsonVar type=variable source=@json maxrows=20}}
     {{.data}}
          {"target":"@json"}
     {{/data}}
{{/dataobject}}
{{#each JsonVar}}

  %%[ set @count = add(@count,1) ]%%
  <br>count:%%=v(@count)=%%&nbsp;

  %%[if mod(@count,2) == 0 then ]%%

    %%=ContentBlockByKey("whee")=%%
    %%=ContentBlockByKey("whoa")=%%

  %%[ else ]%%

    %%=ContentBlockByKey("whoa")=%%
    %%=ContentBlockByKey("whee")=%%

  %%[ endif ]%%

{{/each}}

%%[ endif ]%%

Output

GTL odd/even test

4
  • Thanks Adam, spot on. I've just done the test for myself and that works as expected. Given it worked, would you still recommend SSJS for this?
    – Ben
    Apr 4, 2022 at 0:11
  • Yes, I don't recommend GTL in SFMC, but it depends on the use-case. For order confirmation email of any complexity I'd start with SSJS. Here's the structure I use. Apr 4, 2022 at 12:19
  • Cool thanks for the link
    – Ben
    Apr 5, 2022 at 1:11
  • I'm playing with your SSJS now. Can you help me with how to do the MOD bit with SSJS? Thanks
    – Ben
    Apr 5, 2022 at 4:57
2

I have a feeling I might need to count rows and then check for an odd/even rowCount number, but hoping there is an easier way.

I would honestly recommend doing that either by:

  • dropping GTL entirely and switching to SSJS to loop through the JSON which will automatically count which row it is
  • or by adding Ampscript inside of the content block

The only other solution I can think of is using CSS :nth-child() selectors which would work this way:

  • if the block element has an odd number do the following to the elements inside it:
    • hide <div 1> using display: none,
    • display <div 3>
  • if the block element has an even number do the following to the elements inside it
    • display <div 1>
    • hide <div 3> using display: none

Example:

enter image description here

The writing of the actual selector is on you and I would rather recommend tp use the row counting approach as it's safe from any CSS issues with various inboxes which you would need to test out.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .