{% include './partial/project.twig' with {'status': 'Past Project',
'statusClass': 'past',
'heroImage': "/dist/images/projects/project-south16th.jpg",
'logo': '/dist/images/logo-south16th.png',
'desc': '23 three- and four-bed townhomes',
'address': '15885 16 Avenue, South Surrey',
'showGallery': true,
'galleryID': 'south16th',
'link': '#',
'galleryImages': "
{% for i in range(1, 10) %}
<a data-fancybox='south16th' href='{{ theme.uri }}/dist/images/gallery/south16/{{ i }}.jpg'></a>
{% endfor %}
"
} %}
以上代码无效,因为看起来twig不允许在include标记中嵌套标记?或者我做错了什么?
还有另一种方法可以实现吗?我想重复这段代码X次并将其传递给模板:
{% for i in range(1, 10) %}
<a data-fancybox='south16th' href='{{ theme.uri }}/dist/images/gallery/south16/{{ i }}.jpg'></a>
{% endfor %}
答案 0 :(得分:2)
要实现这一目标,您需要切换到embed
而不是include
index.twig
{% embed 'include.twig' with { 'theme': { 'uri' : 'https://www.example.com', 'pictures': 10, }, } %}
{% block pictures %}
{% for i in 1..theme.pictures %}
<li><a data-fancybox='south16th' href='{{ theme.uri }}/dist/images/gallery/south16/{{ i }}.jpg'></a></li>
{% endfor %}
{% endblock %}
{% endembed %}
include.twig
<h1>Include</h1>
<h2>{{ theme.uri }} - {{ theme.pictures }}</h2>
<ul>
{% block pictures %}
{% endblock %}
</ul>
注意:您在include.twig
中定义的变量也可以在嵌入
答案 1 :(得分:0)
将其换行{% set %}
并使用变量:
{% set galleryImages %}
{% for i in range(1, 10) %}
<a data-fancybox='south16th' href='{{ theme.uri }}/dist/images/gallery/south16/{{ i }}.jpg'></a>
{% endfor %}
{% endset %}
{% include './partial/project.twig' with {'status': 'Past Project',
'statusClass': 'past',
'heroImage': "/dist/images/projects/project-south16th.jpg",
'logo': '/dist/images/logo-south16th.png',
'desc': '23 three- and four-bed townhomes',
'address': '15885 16 Avenue, South Surrey',
'showGallery': true,
'galleryID': 'south16th',
'link': '#',
'galleryImages': galleryImages
} %}