2

I am using Guide Template Language (GTL) in a HTML email to output data in a table format. The table alignment is off when displaying data from the attributes. I notice that an extra blank column value is added in case of the nested attributes(Color and Size). How can I get rid of these blank values? TIA.Output table Example: HTML Code

          {{.datasource attributesItem type=nested maxRows=10 global=false}}
              {{.data}}
              { "target" : "line_items.attributes" }
              {{/data}}
         <td align="left" valign="top"  style=" padding-top:0px; padding-bottom:0px;">{{#if attributesItem.name == "Color"}} {{attributesItem.value}} {{/if}}</td>
         <td align="left" valign="top"  style=" padding-top:0px; padding-bottom:0px; ">{{#if attributesItem.name == "Size"}} {{attributesItem.value}} {{/if}}</td>
          {{/datasource}}

Data Context:

[{....,"price":"$30.00","store_pickup_name":"","quantity":{},"brand":"Hurley","attributes":[{"name":"Color","value":"BLACK"},{"name":"Size","value":"MED SIZE"}]},
{....,"price":"$32.47","store_pickup_name":"","quantity":{},"brand":"Nike SB","attributes":[{"name":"Color","value":"RED"},{"name":"Size","value":"LRG SIZE"}]},
{"image":"<img src=\"xxx" alt=\"\" title=\"\"/>","item_upc":"","itemsku":"Box","producttitle":"Gift Box","price":"$3.00","store_pickup_name":"","quantity":1,"brand":"","attributes":[]}]

1 Answer 1

2

The issue here is not really GTL, but the way you have structured your JSON data.

This data structure, in its current form, isn't really suitable for GTL, or any template engine for that matter. It's not consistent. For a start:

  1. some objects define quantity as an object (an an empty one too), while others as a value
  2. Attributes are in name/value pairs as separate objects in a nested array which is redundant.
  3. You can't escape double quotes in your img element with \ in AMPscript/GTL.

I'd recommend normalising your JSON to something that can be parsed.

For example, based on your supplied data (which is only a snippet, so it's hard to see it in context and I've had to make assumptions), I'd recommend the following structure:

{
   "line_items":[
      {
         "price":"$30.00",
         "store_pickup_name":"",
         "quantity":null,
         "brand":"Hurley",
         "color":"BLACK",
         "Size":"MED SIZE"
      },
      {
         "price":"$32.47",
         "store_pickup_name":"",
         "quantity":null,
         "brand":"Nike SB",
         "color":"RED",
         "Size":"LARGE SIZE"
      },
      {
         "image":"x",
         "item_upc":"",
         "itemsku":"Box",
         "producttitle":"Gift Box",
         "price":"$3.00",
         "store_pickup_name":"",
         "quantity":1,
         "color":null,
         "Size":null
      }
   ]
}

The following HTML:

<!DOCTYPE html>
<html>
<head>
<title>GTL Example</title>

<style type="text/css">
    table, th, td {
      border: 1px solid black;
    }
</style>
</head>
<body>

<table style="width:100%">

  <tr>
    <th>Color</th>
    <th>Size</th>
    <th>Qty</th>
    <th>Price</th>
  </tr>

%%[
var @order
set @order = '{"line_items":[{"price":"$30.00", "store_pickup_name":"", "quantity":null, "brand":"Hurley", "color":"BLACK", "Size":"MED SIZE"}, {"price":"$32.47", "store_pickup_name":"", "quantity":null, "brand":"Nike SB", "color":"RED", "Size":"LARGE SIZE"}, {"image":"x", "item_upc":"", "itemsku":"Box", "producttitle":"Gift Box", "price":"$3.00", "store_pickup_name":"", "quantity":1, "color":null, "Size":null } ] }'

]%%

{{.datasource order type=variable source=@order}}
   {{.data}}
      {"target":"@order"}
   {{/data}}

       {{.datasource lineItems type=nested}}
           {{.data}}
              {"target":"order.line_items"}
           {{/data}}

  <tr>
    <td>{{Color}}</td>
    <td>{{Size}}</td>
    <td>{{quantity}}</td>
    <td>{{price}}</td>
  </tr>
        {{/datasource}}
    {{/datasource}}
</table>

</body>
</html>

Returns the output below:

output table

This still looks unusual, but considering that your first 2 objects don't have a quantity and your last doesn't have a color and size attribute, this is what I'd expect to see.

4
  • Thank you. JSON is coming from an external Order Management System which I have less control on. In my use case, the 'Color' and 'Size' are defined as nested attributes EX: { "line_items":[ { "price":"$30.00", "store_pickup_name":"", "quantity":null, "brand":"Hurley", "attributes": [ {"name":"Color","value":"BLACK"}, {"name":"Size","value":"MED SIZE"} ], "color":"BLACK", "Size":"MED SIZE" } } Commented Oct 5, 2019 at 18:31
  • And just for these two values, an extra blank column is added in the table. It all works fine if I define Color and Size not as nested attributes within line_items. Commented Oct 5, 2019 at 18:31
  • The GTL I am using: {{.datasource line_items type=variable maxRows=20}} {{.data}} { "target" : "@line_items" } {{/data}} {{foreach line_items}} Commented Oct 5, 2019 at 18:51
  • {{.datasource attributesItem type=nested maxRows=10 global=false}} {{.data}} { "target" : "line_items.attributes" } {{/data}} ............ {{/datasource}} {{/each}} {{/datasource}} Commented Oct 5, 2019 at 18:51

You must log in to answer this question.

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