1
{% block someBlock %}
{% set foo = 'blah blah blah' %}
{% endblock %}

{% block otherBlock %}
{{ foo }}
{% endblock %}

这不能按预期工作(foo 在第二个块中不可见)。如何使其全局可见并使其按预期工作?

4

2 回答 2

1

这似乎是一个 TWIG 或 Symfony2 问题:

https://github.com/fabpot/Twig/issues/735

也许在不久的将来...

于 2013-07-15T07:19:14.630 回答
0

foo必须在外部范围内初始化。

{# begin template "foo.html" #}

{% set foo = 'default value' %}  {# defined bar.foo #}

{% block someBlock %}
{% set foo = 'blah blah blah' %} {# changes bar.foo #}
{{ foo }}                        {# outputs bar.foo #}
{% set baz = 'other' %}          {# defined #}
{% endblock %}

{% block otherBlock %}
{{ foo }}                        {# outputs bar.foo #}
{{ baz }}                        {# undefined #}
{% endblock %}

{# template "main.html" #}

{% include "foo.html" as bar %}

{{ block('someBlock') }}
{{ block('otherBlock')}}
于 2016-06-28T22:07:00.447 回答