0

I'm struggling with json in twig. I want to include a static json file to my twig file and pass it to a twig include inside that.

I have my index twig file where I have my STATIC json file include and twig include:

{% set json %}
    {% include "content.json" %}
{% endset %}

{% include '@partials/blocks/site-header.twig' with {json:json} %}

In My siteheader.twig I would like to render my navigation something like:

{% for headerNav in json %}

<a class="button button--nav" href="#">
{{headerNav.text}}
</a>

{% endfor %}

My content.json looks like this:


{
    "headerNav":[
        {
            "text": "Link 1"
        },
        {
            "text": "Link 2"
        }
    ]
}

The whole point is that the siteheader should be able to render different links dependant on which json file i pass into it.

jna
  • 41
  • 2
  • Does this answer your question? [Decoding JSON in Twig](https://stackoverflow.com/questions/14500698/decoding-json-in-twig) – DarkBee Oct 12 '21 at 11:46
  • Anyone? Every kind of answer about static json and twig would help :-) – jna Oct 18 '21 at 08:22
  • .. Did you read the question/answers in the link I've added? – DarkBee Oct 18 '21 at 08:39
  • Yes I did. It seems it is more targeted dynamic json created on the server by php or .net What I am looking for is how to pass json into a twig include and the json is a flat .json file in the file system. Did i miss something in the post you are reffering to? – jna Oct 18 '21 at 12:16
  • Twig is not able to work `JSON` directly, therefor you need to decode it first. Then you can loop it. – DarkBee Oct 18 '21 at 12:20

0 Answers0