2012-01-06 11 views
7

Voglio tema mia forma in modo che l'etichetta del campo mostrano locale corrente, qualcosa comeCome passare una variabile a form_theme?

Nome (it):

quindi vorrei riscrivere generic_label blocco del genere:

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

e importarlo nel mio modello:

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

ma la variabile app non è accessibile nel modello di modulo. Come posso passare una variabile a un tema del modulo?

risposta

0

Se la variabile app non è disponibile nel tema del modulo, potrebbe trattarsi di un bug. Ti suggerisco create a ticket.

Nel frattempo è possibile utilizzare il modello corrente come tema. Qualcosa di simile ...

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

Se si sta utilizzando Symfony master (2.1) sostituire app.session.locale con app.request.locale.

+0

che non funziona neanche, '{% form_theme edit_form _self%} {% block field_label%} {{app.session.locale}} {% endblock%}' dà l'app 'Variable "" non esiste'. Creerò un ticket – Matthieu

+0

https://github.com/symfony/symfony/issues/3058 – Matthieu

3

Nella versione corrente di ramoscello (come per il 2016) è possibile. Nel modello utilizzare il seguente così:

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

Poi, nel file del tema, basta usare:

{{testvar}} 

naturalmente, invece di form.content verrà utilizzato il nome del campo è necessario. Chris

Problemi correlati