如何将变量传递给form_theme? [英] How to pass a variable to form_theme?
本文介绍了如何将变量传递给form_theme?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想为我的表单设置主题,以便该字段的标签显示当前的语言环境,例如
I want to theme my form so that the field's label show the current locale, something like
姓名(英文):
所以我想像这样重写块 generic_label :
So I would like to rewrite block generic_label like that :
{# 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 %}
并将其导入我的模板:
{% form_theme options 'myBundle:Object:form_theme.html.twig' %}
但无法在表单模板中访问 app 变量.如何将变量传递给表单主题?
but the app variable is not accessible in the form template. How can I pass a variable to a form theme ?
推荐答案
在当前版本的 twig(至于 2016)中是可能的.在您的模板中使用以下内容:
In current version of twig (as for 2016) it is possible. In your template use the following like this:
{{ form_row(form.content, {'testvar' : 'this is test variable'}) }}
然后,在您的主题文件中,只需使用:
Then, in your theme file, just use:
{{testvar}}
当然,您将使用您需要的字段名称而不是 form.content.干杯,克里斯
of course instead of form.content you will use the field name you need. Cheers, Chris
这篇关于如何将变量传递给form_theme?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文