Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


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

ma2thieu opened this Issue · 4 comments

5 participants

Matthieu Robillard Alexandre Salomé Victor Berchet ArlingtonHouse Fabien Potencier
Matthieu Robillard

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 
Alexandre Salomé

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.

Victor Berchet

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

Alexandre Salomé

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:

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.


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.

Fabien Potencier fabpot closed this in 878e86d
Delf Tonder leberknecht referenced this issue in phiamo/MopaBootstrapBundle

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
Something went wrong with that request. Please try again.