Search code examples
jsontwigpatternlab.io

JSON not replacing variables


I'm using Twig PatternLab.

I got a small issue with JSON and a Twig for loop.

Atom 00-h3-black.twig:

<h3 class="A-ChevronBlack"><a href="">{{ text.chevron }}</a></h3>

Molecule 00-mastertile.twig:

    <div class="M-DMasterTile">
      <div class="image"></div>
      <div class="content">
        {% include "atoms-h3-black" %}
      </div>
    </div>

Organisms 00-default2.twig:

    {% for post in default2 %}
          {% include "molecules-mastertile" %}
    {% endfor %}

And JSON inside Organism folder 00-default2.json

    {
      "default2" : [
        { 
          "text" : {
            "chevron" : "How to build a campfire",
            "body" : "This is the body copy"
          }
        },
        {
          "text" : {
            "chevron":"Lorem Ipsum",
            "body" : "This is the body copy"
          }
        }
      ]
    }

My expectation is for the "default2" to loop twice because I've got an array with 2 items in the JSON and push the JSON content. If I take the variables out of the JSON array it shows the changes(but obviously duplicated).

What am I doing wrong here?

I appreciate your help


Solution

  • include uses global scope and there is no variable text in it. Use include with syntax to pass variable into inner scope.

    Your Organisms 00-default2.twig should look like this:

    {% for post in default2 %}
          {% include "molecules-mastertile" with {'text': post.text} %}
    {% endfor %}