How to pass form_theme variable?

I want to use a theme of my form so that the field label displays the current language, something like

Name (en):

So, I would like to rewrite the generic_label block as follows:

{# form_theme.html.twig #} {% block generic_label %} {% spaceless %} {% if required %} {% set attr = attr|merge({'class': attr.class|default('') ~ ' required'}) %} {% endif %} <label{% for attrname,attrvalue in attr %} {{attrname}}="{{attrvalue}}"{% endfor %}>{{ label|trans }} (app.session.locale)</label> {% endspaceless %} {% endblock %} 

and import it into my template:

 {% form_theme options 'myBundle:Object:form_theme.html.twig' %} 

but the application variable is not available in the form template. How to pass a variable to a form theme?

+7
source share
3 answers

In the current version of the branch (as in 2016), this is possible. In the template, use the following:

 {{ form_row(form.content, {'testvar' : 'this is test variable'}) }} 

Then in the theme file just use:

 {{testvar}} 

Of course, instead of form.content you will use the desired field name. Hooray, Chris

+3
source

You need to create a form extension to do this. Take a look at

http://toni.uebernickel.info/2011/11/25/how-to-extend-form-fields-in-symfony2.html

to learn how to create an extension.

To have access to the localization language, make sure that you enter the container. After that, you can get any var value you want.

+1
source

If the app variable is not available in the form theme, this may be an error. I suggest you create a ticket .

In the meantime, you can use the current template as a theme. Something like...

 {% form_theme form _self %} {% block field_label %} {% set attr = attr|merge({ 'for': id }) %} {% if required %} {% set attr = attr|merge({ 'class': attr.class|default('') ~ ' required' }) %} {% endif %} <label{% for attrname, attrvalue in attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ label|trans }} ({{ app.session.locale }})</label> {% endblock %} 

If you are using the Symfony (2.1) app.session.locale , replace app.session.locale with app.request.locale .

0
source

All Articles