0

I am trying to pass a variable into a GTL datasource to get a set of nested values from a JSON so they can be used to dynamically theme an HTML module. In the below example I am able to get the values when I set:

{"target": "themes.hero"}

However, my attempts to use a variable have failed (Email compiler spits out blanks. Can anyone help please? Why does the first target allow an AMPScript variable but not the second?

%%[ 
var @global_themes, @theme
set @global_themes = '{
                       "hero": {
                        "backgroundColour": "#96151D",
                        "headerColour": "#FFFFFF",
                        "copyColour": "#FFFFFF",
                        "linkColour": "#FFFFFF"
                       },
                       "themeOne": {
                        "backgroundColour": "#FFFFFF",
                        "headerColour": "#1A1A1A",
                        "copyColour": "#666666",
                        "linkColour": "#96151D"
                       }
                      }'

set @theme = "hero"
]%%

    
{{.datasource themes type=variable}}
  {{.data}}
    {"target": "@global_themes"}
  {{/data}}
    {{.datasource themes type=nested}}
      {{.data}}
        {"target": "themes.@theme"}
      {{/data}}

      backgroundColour: {{backgroundColour}}<br>
      headerColour: {{headerColour}}<br>
      copyColour: {{copyColour}}<br>
      linkColour: {{linkColour}}<br>

     {{/datasource}}
{{/datasource}}

1 Answer 1

2

I managed to replicate the code and made some modifications - the end result is working.

Key changes:

  • added the source and referenced the AMPScript variable
  • removed the gtl escape OMM delimiters
  • added extra code to render the themeOne JSON object.
var @global_themes, @theme
set @global_themes = '{
                       "hero": {
                        "backgroundColour": "#96151D",
                        "headerColour": "#FFFFFF",
                        "copyColour": "#FFFFFF",
                        "linkColour": "#FFFFFF"
                       },
                       "themeOne": {
                        "backgroundColour": "#FFFFFF",
                        "headerColour": "#1A1A1A",
                        "copyColour": "#666666",
                        "linkColour": "#96151D"
                       }
                      }'

set @theme = "hero"
]%%


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

    {{.datasource hero source=@global_themes type=nested}}

        {{.data}}
          { "target" : "JSONVar.hero" }
        {{/data}}

         backgroundColour: {{backgroundColour}}<br>
        headerColour: {{headerColour}}<br>
        copyColour: {{copyColour}}<br>
        linkColour: {{linkColour}}<br>
    {{/datasource}}

    <br><br>

    {{.datasource themeOne source=@global_themes type=nested}}

        {{.data}}
          { "target" : "JSONVar.themeOne" }
        {{/data}}

         backgroundColour: {{backgroundColour}}<br>
        headerColour: {{headerColour}}<br>
        copyColour: {{copyColour}}<br>
        linkColour: {{linkColour}}<br>
    {{/datasource}}
{{/datasource}}

Result

backgroundColour: #96151D
headerColour: #FFFFFF
copyColour: #FFFFFF
linkColour: #FFFFFF


backgroundColour: #FFFFFF
headerColour: #1A1A1A
copyColour: #666666
linkColour: #96151D

Reference: GTL in cloudpages: #with

1
  • I am able to get the values just as you did when I am explicit about the name of the theme in the nested data source. Where you have this: {{.data}} { "target" : "JSONVar.hero" } {{/data}} I would like to be able to set the theme name via ampscript variable like this to only retrieve those specific values: {{.data}} { "target" : "JSONVar.@theme" } {{/data}} When I added the @theme variable the page just spits out blank again. Is this a limitation of GTL? Jun 12, 2021 at 1:39

You must log in to answer this question.

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