我想在树枝中创建一个自定义模板来渲染表单字段.
例:
{{ form_row(form.field) }}
这可以通过形式主题来覆盖
{% block form_row %} ... custom code {% endblock form_row %}
我想做的是:
{% block custom_row %} ... custom code {% endblock custom_row %}
并像这样使用它:
{{ custom_row(form.field }}
但是,这会引发异常,即找不到方法custom_row.
我的理解是这可以通过Twig扩展来完成,但我不知道如何将块注册为一个函数.
更新
我真正想要的是:
我使用twitter bootstrap和一个覆盖所有表单主题的包.并且它在收音机周围呈现div,因此无法内联.所以我想做这样的事情:
复制他们的模板,摆脱div:
{% block inline_radio_row %} {% spaceless %} {% set col_size = col_size|default(bootstrap_get_col_size()) %} {% if attr.label_col is defined and attr.label_col is not empty %} {% set label_col = attr.label_col %} {% endif %} {% if attr.widget_col is defined and attr.widget_col is not empty %} {% set widget_col = attr.widget_col %} {% endif %} {% if attr.col_size is defined and attr.col_size is not empty %} {% set col_size = attr.col_size %} {% endif %} {% if label is not sameas(false) %} {% if not compound %} {% set label_attr = label_attr|merge({'for': id}) %} {% endif %} {% if required %} {% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %} {% endif %} {% if label is empty %} {% set label = name|humanize %} {% endif %} {% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' radio-inline')|trim}) %} <label{% for attrname,attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}> {{ block('radio_widget') }} {{ label|trans({},translation_domain) }} </label> {% else %} {{ block('radio_widget') }} {% endif %} {{ form_errors(form) }} {% endspaceless %} {% endblock inline_radio_row %}
然后
{{ inline_radio_row(form.field) }}
我最终只是覆盖整个主题,并在问题的div周围添加了ifs,一个类(无线电内联).但我仍然想知道是否有办法让这项工作成功.似乎它会让你如此努力地工作如此简单.
更新2
我找到了功能:
class FormExtension extends \Twig_Extension { public function getFunctions() { return array( 'inline_radio_row' => new \Twig_Function_Node( 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode',array('is_safe' => array('html')) ),); } }
这完全符合我的要求,但它表示已弃用.有谁知道如何使用它的更新版本?
更新3
使用http://twig.sensiolabs.org/doc/tags/include.html也可以实现类似的功能
解决方法
您可以为表单行的每个部分使用twig函数:
> form_label(form.field)
> form_widget(form.field)
> form_errors(form.field)
例如:
<div class="form_row"> {{ form_label(form.field) }} {# the name of the field #} {{ form_errors(form.field) }} {# the field #} {{ form_widget(form.field) }} {# the errors associated to the field #} </div>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。