JSON 不替换变量
JSON not replacing variables
我正在使用 Twig PatternLab。
我遇到了 JSON 和 Twig for 循环的小问题。
原子 00-h3-black.twig:
<h3 class="A-ChevronBlack"><a href="">{{ text.chevron }}</a></h3>
分子 00-mastertile.twig:
<div class="M-DMasterTile">
<div class="image"></div>
<div class="content">
{% include "atoms-h3-black" %}
</div>
</div>
有机体 00-default2.twig:
{% for post in default2 %}
{% include "molecules-mastertile" %}
{% endfor %}
和 JSON 在 Organism 文件夹 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"
}
}
]
}
我期望 "default2" 循环两次,因为我在 JSON 中有一个包含 2 个项目的数组并推送 JSON 内容。如果我从 JSON 数组中取出变量,它会显示更改(但显然是重复的)。
我做错了什么?
感谢您的帮助
include 使用全局作用域并且其中没有变量 text
。使用 include with
语法将变量传递到内部范围。
您的 Organisms 00-default2.twig
应如下所示:
{% for post in default2 %}
{% include "molecules-mastertile" with {'text': post.text} %}
{% endfor %}
我正在使用 Twig PatternLab。
我遇到了 JSON 和 Twig for 循环的小问题。
原子 00-h3-black.twig:
<h3 class="A-ChevronBlack"><a href="">{{ text.chevron }}</a></h3>
分子 00-mastertile.twig:
<div class="M-DMasterTile">
<div class="image"></div>
<div class="content">
{% include "atoms-h3-black" %}
</div>
</div>
有机体 00-default2.twig:
{% for post in default2 %}
{% include "molecules-mastertile" %}
{% endfor %}
和 JSON 在 Organism 文件夹 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"
}
}
]
}
我期望 "default2" 循环两次,因为我在 JSON 中有一个包含 2 个项目的数组并推送 JSON 内容。如果我从 JSON 数组中取出变量,它会显示更改(但显然是重复的)。
我做错了什么?
感谢您的帮助
include 使用全局作用域并且其中没有变量 text
。使用 include with
语法将变量传递到内部范围。
您的 Organisms 00-default2.twig
应如下所示:
{% for post in default2 %}
{% include "molecules-mastertile" with {'text': post.text} %}
{% endfor %}