Reputation: 1840
{% block someBlock %}
{% set foo = 'blah blah blah' %}
{% endblock %}
{% block otherBlock %}
{{ foo }}
{% endblock %}
This doesn't work as expected (foo is not visible in the second block). How can I make it globally visible and make it work as expected?
Upvotes: 2
Views: 1068
Reputation: 123
foo
has to be initialized in the outer scope.
{# 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')}}
Upvotes: 0
Reputation: 1375
It seems to be a TWIG or Symfony2 issue:
https://github.com/fabpot/Twig/issues/735
Maybe in a close future...
Upvotes: 1