Make a Twig variable available in the template

1,017 views
Skip to first unread message

Denis SORIANO

unread,
Jul 12, 2013, 8:38:28 AM7/12/13
to sonata...@googlegroups.com
Hi, In a Twig template I set a variable

{% block page_container %}
{% set myVar = true %}
{% endblock %}

And I would like using this variable in the layout. Today this not seems to work due to sonata structure. Is there another tip to do that ?

Thanks in advance

Sebastian Krebs

unread,
Jul 12, 2013, 9:04:47 AM7/12/13
to sonata...@googlegroups.com



2013/7/12 Denis SORIANO <denis....@gmail.com>
Are you sure? As far as I remember it is twig, that doesn't allow just leaving your scope and if you ask me it is good as it is (less side-effects and such).
 

Thanks in advance

--
You received this message because you are subscribed to the Google Groups "sonata-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonata-users...@googlegroups.com.
To post to this group, send email to sonata...@googlegroups.com.
Visit this group at http://groups.google.com/group/sonata-users.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
github.com/KingCrunch

Cassiano Tartari

unread,
Jul 12, 2013, 10:02:44 AM7/12/13
to sonata-users
You can create global variable via config.yml or variables in the main template that is extended by others.

config.yml
# Twig Configuration
twig:
    globals:
      your_variable: value


Via twig file like you did, but when you extend the block I think you should call parent(), like this:

{% block page_container %}
{{ parent() }}
The value of myVar is: {{ myVar }}
{% endblock %}

Cassiano Valle Tartari
MSc. Computer Engineer

Tel: +55.48.84474818
Email: fal...@cassianotartari.eng.br
Site: http://www.cassianotartari.eng.br

QR Code



--

Denis SORIANO

unread,
Jul 15, 2013, 8:31:22 AM7/15/13
to sonata...@googlegroups.com
Hi ! I tried with the twig globals and it doesn't work but I not tried with "parent()". I will make a test and tell you if it's ok. Thanks for your response !

Cassiano Tartari

unread,
Jul 15, 2013, 8:37:40 AM7/15/13
to sonata-users
Maybe to work the global variables you should clean the cache, I don't know if did it before, but always when you chance something in the config files is recommended to clean the cache.

Cassiano Valle Tartari
MSc. Computer Engineer

Tel: +55.48.84474818
Email: fal...@cassianotartari.eng.br
Site: http://www.cassianotartari.eng.br

QR Code


Thomas Rabaix

unread,
Jul 15, 2013, 8:43:48 AM7/15/13
to sonata...@googlegroups.com
If you are using PageBundle and BlockBundle then you cannot pass a variable from the block's template to the global layout.

The reason is simple: the scope is different. Why ? because a block can be cached. so you cannot rely on a variable defined inside a block::execute function, this variable might not be initialised if the block is retrieved from the cache.

How can you deal with this situation? You need to create a custom page service (similar to a controller) attached to the page. This custom page service act as a controller so you can declare new variables available inside the main layout. You can also used this custom page service to create custom metadata or seo entries.

There is no documentation for now for this feature, but here are some entries points:



You can associate a service type to the page by using the admin section (type field)


--
Thomas Rabaix



Reply all
Reply to author
Forward
0 new messages