{% block someBlock %}
{% set foo = 'blah blah blah' %}
{% endblock %}
{% block otherBlock %}
{{ foo }}
{% endblock %}
这不能按预期工作(foo 在第二个块中不可见)。如何使其全局可见并使其按预期工作?
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')}}