symfony2 表单模板
我想渲染一个表单。字段行的 HTML 应如下所示:
<li class="text">
<label for="fieldname">
<div>
<input type="text" ... />
</div>
</li>
当字段类型为文本时,li.class 必须相同。
我覆盖了 field_row 块:
{% block field_row %}
{% spaceless %}
<li class="text">
{{ form_label(form, label|default(null)) }}
{{ form_errors(form) }}
{{ form_widget(form) }}
</li>
{% endspaceless %}
{% endblock field_row %}
但是如何替换类值?
I want to render a form. HTML for a field row should be like this:
<li class="text">
<label for="fieldname">
<div>
<input type="text" ... />
</div>
</li>
when the field type is text the li.class have to be the same.
I overwrite the field_row block:
{% block field_row %}
{% spaceless %}
<li class="text">
{{ form_label(form, label|default(null)) }}
{{ form_errors(form) }}
{{ form_widget(form) }}
</li>
{% endspaceless %}
{% endblock field_row %}
but how to replace the class value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试将公共成员附加到您的 FormType 类(如果存在...)并从树枝模板中调用它。
也许表单的属性数组也可以在树枝模板中访问......
并且
You can try to attach a public member to your FormType class (If present...) and call it from the twig template.
Maybe also the attributes array of a form can be accessed in a twig template...
And
只需将“字段”一词替换为您要修改的类型的名称即可。
对于文本字段,您可以这样做,但对于任何类型都是相同的:
或者对于文本区域:
重要的部分是块名称,它应该与您要修改的类型的名称相同。如果没有完全匹配的名称,“field_row”是所有字段类型的默认值。
这也适用于您自己定义的表单类型(从 AbstractType 继承的表单类型,这就是为什么向表单类型添加名称很重要,请参阅 http://symfony.com/doc/2.0/book/forms.html#creating-form-classes)。
Just replace the "field" word with the name of the type you want to modify.
You do it like this for text fields, but it's the same for any type:
or like this for textareas:
The important part is the block name, it should be the same as the name of the type you want to modify. The "field_row" is the default for all field types if there is no exact matching name.
This also works for form-types you defined yourself (the ones that inherit from AbstractType, that's why it's important you add a name to your form-types, see http://symfony.com/doc/2.0/book/forms.html#creating-form-classes).