如何将变量传递给form_theme?
我想为我的表单设置主题,以便字段的标签显示当前区域设置,例如
姓名(zh):
所以我想像这样重写块 generic_label :
{# 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 变量在表单模板中无法访问。 如何将变量传递给表单主题?
I want to theme my form so that the field's label show the current locale, something like
Name (en) :
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 %}
and import it in my template :
{% form_theme options 'myBundle:Object:form_theme.html.twig' %}
but the app variable is not accessible in the form template.
How can I pass a variable to a form theme ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在当前版本的 twig(2016 年)中这是可能的。
在您的模板中使用以下内容:
然后,在您的主题文件中,只需使用:
当然,您将使用您需要的字段名称,而不是 form.content 。
干杯,
克里斯
In current version of twig (as for 2016) it is possible.
In your template use the following like this:
Then, in your theme file, just use:
of course instead of form.content you will use the field name you need.
Cheers,
Chris
您需要创建一个表单扩展才能完成它。看看
http:// /toni.uebernickel.info/2011/11/25/how-to-extend-form-fields-in-symfony2.html
了解如何创建扩展。
要访问会话区域设置,请确保注入容器。之后您将能够获得您想要的任何 var 值。
You need to create a form extension in order to get it done. Take a look at
http://toni.uebernickel.info/2011/11/25/how-to-extend-form-fields-in-symfony2.html
to learn how to create the extension.
To have access to session locale, make sure to inject the container. After that you'll be able to get any var value you want.
如果
app
变量在表单主题中不可用,则可能是一个错误。我建议您创建票证。同时,您可以使用当前模板作为主题。类似...
如果您使用的是 Symfony master (2.1),请将
app.session.locale
替换为app.request.locale
。If the
app
variable is not available in the form theme it may be a bug. I suggest you create a ticket.In the meantime you can use the current template as a theme. Something like...
If you are using Symfony master (2.1) replace
app.session.locale
withapp.request.locale
.