我想在循环中使用模板变量(作为字符串)。
{# Define my template #}
{% set my_template %}
<span>{{ job.title }}</span>
...
{% endset %}
{# Using the template inside a loop #}
{% for job in jobs %}
{{ include(template_from_string(my_template), { 'job', job }) }}
{% endfor %}
我希望它显示具有“作业”相应值的模板内容,但出现错误:“未定义变量作业”
使用宏https://twig.symfony.com/doc/2.x/tags/macro.html
{% macro my_template(job) %}
<span>{{ job.title }}</span>
...
{% endset %}
{# Using the template inside a loop #}
{% for job in jobs %}
{{ include(template_from_string(_self.my_template(job)), { 'job', job }) }}
{% endfor %}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句