[Form] app variable is not available in form theme #3058

Closed
ma2thieu opened this Issue Jan 8, 2012 · 4 comments

5 participants

@ma2thieu

With symfony 2.0.9 this code :

{% form_theme form _self %}

{% block field_label %}
{{ app.session.locale }}
{% endblock %}

{% block container %}
    {{ form_widget(form) }}
{% endblock %}

generate an error :

Variable "app" does not exist in myBundle:Entity:view.html.twig at line 7
500 Internal Server Error - Twig_Error_Runtime 
@alexandresalome

It's not a problem from Symfony2, but from Twig:

When you render a template, the context (globals, especially) is provided via the environment.

When you render a block, the method is directly called on the template and the context is empty. So you have no access to Symfony2 globals (especially app). If you need to get it, you have to pass it via form options, override the buildView, etc.

This ticket can be closed, and it should be discussed more globally, concerning Twig: render just blocks from templates, with context from environment.

@vicb

@alexandresalome do you think it is really not possible to modify the bridge to pass global vars ? (I haven't check)

@alexandresalome

It's kind of possible, via the Symfony\Bridge\Twig\Extension\FormExtension : add a Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables as parameter, but it's a temporary solution.

I also have the case when I try to send mails with Twig: http://alexandre-salome.fr/blog/Generate-Mails-With-Twig

Globals are collected in the Twig environment. When you work directly with a template object (like in forms, and the article on my blog), you don't have this inherited context.

Maybe Twig should be refactored, concerning globals. It should be discussed with @fabpot I think.

@ArlingtonHouse

Issue #4448 is slightly different-- the global variable isn't available anywhere in the site template once form _self is used-- the impact is greater than just availability in the form block.

@fabpot fabpot closed this in 878e86d Jul 10, 2012
@leberknecht leberknecht referenced this issue in phiamo/MopaBootstrapBundle Apr 23, 2013
Merged

fixing missing globals problem in twig, closes #360 #427

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment