首页 > 解决方案 > 使用 twig include 将静态 json 文件中的 json 添加到 index.twig

问题描述

我在树枝上与 json 作斗争。我想在我的 twig 文件中包含一个静态 json 文件并将其传递给其中的一个 twig 包含。

我有我的索引树枝文件,其中有我的 STATIC json 文件包括和树枝包括:

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

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

在我的 siteheader.twig 中,我想将我的导航呈现为:

{% for headerNav in json %}

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

{% endfor %}

我的 content.json 看起来像这样:


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

重点是,siteheader 应该能够根据我传入的 json 文件呈现不同的链接。

标签: jsontwig

解决方案


推荐阅读