user2394156
user2394156

Reputation: 1840

DIsplaying set variables in other block in Twig

{% 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

Answers (2)

Ber
Ber

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

Dani Sancas
Dani Sancas

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

Related Questions