带有树枝的自定义表单域模板 [英] Custom form field template with twig

查看:24
本文介绍了带有树枝的自定义表单域模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 twig 中创建一个自定义模板来呈现表单字段.

示例:

{{ form_row(form.field) }}

这可以通过表单主题覆盖

{% 块 form_row %}... 自定义代码{% endblock form_row %}

我想做的是:

{% 块 custom_row %}... 自定义代码{% endblock custom_row %}

并像这样使用它:

{{ custom_row(form.field }}

然而,这会引发一个异常,即找不到方法 custom_row.

我的理解是,这可以通过 Twig 扩展来完成,但我不知道如何将块注册为函数.

更新

我真正想要的:

我使用 twitter bootstrap 和一个覆盖所有表单主题的包.它在收音机周围呈现一个 div,所以它不能被内联.所以我想做这样的事情:

复制他们的模板并去掉 div:

{% 块 inline_radio_row %}{% 无空间 %}{% set col_size = col_size|default(bootstrap_get_col_size()) %}{% 如果定义了 attr.label_col 并且 attr.label_col 不为空 %}{% 设置 label_col = attr.label_col %}{% 万一 %}{% 如果定义了 attr.widget_col 并且 attr.widget_col 不为空 %}{% set widget_col = attr.widget_col %}{% 万一 %}{% 如果定义了 attr.col_size 且 attr.col_size 不为空 %}{% 设置 col_size = attr.col_size %}{% 万一 %}{% if label is not sameas(false) %}{% 如果不是复合 %}{% set label_attr = label_attr|merge({'for': id}) %}{% 万一 %}{% 如果需要的话 %}{% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}{% 万一 %}{% 如果标签为空 %}{% 设置标签 = 名称|人性化 %}{% 万一 %}{% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' radio-inline')|trim}) %}<label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ 块('radio_widget')}}{{ label|trans({}, translation_domain) }}{% 别的 %}{{ 块('radio_widget')}}{% 万一 %}{{ form_errors(form) }}{% endspaceless %}{% endblock inline_radio_row %}

然后

{{ inline_radio_row(form.field) }}

我最终只是覆盖了整个主题,并在有问题的 div 周围添加了 if,一个类(无线电内联).但我仍然想知道是否有办法使这项工作.似乎它让你为了这么简单的事情而努力工作.

更新 2

我找到了这个功能:

class FormExtension 扩展 \Twig_Extension{公共函数 getFunctions(){返回数组('inline_radio_row' =>新的 \Twig_Function_Node('Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode',数组('is_safe' => 数组('html'))),);}}

这正是我想要的,但它说它已被弃用.有人知道如何使用它的更新版本吗?

更新 3

使用 http://twig.sensiolabs.org/doc 也可以实现类似的功能/tags/include.html

解决方案

您可以对表单行的每个部分使用 twig 函数:

例如:

{{ form_label(form.field) }} {# 字段名称#}{{ form_errors(form.field) }} {# 字段#}{{ form_widget(form.field) }} {# 与字段相关的错误 #}

I'd like to create a custom template in twig to render a form field.

Example:

{{ form_row(form.field) }}

This can be overriden by form theming

{% block form_row %}
... custom code
{% endblock form_row %}

What I would like to do is this:

{% block custom_row %}
... custom code
{% endblock custom_row %}

and use it like this:

{{ custom_row(form.field }}

however, this throws an exception that method custom_row is not found.

My understanding is that this can be done with Twig extension, but I don't know how to register a block to be a function.

Update

what I actually want:

I use twitter bootstrap and a bundle which overrides all the form themes. And it renders a div around a radio, so it can't be inlined. So I wanted to do something like this:

copy their template and get rid of the 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 %}

and then

{{ inline_radio_row(form.field) }}

I ended up just overriding the whole theme, and added ifs around the div in question, a the class (radio-inline). But I'm still wondering if there's a way to make this work. Seems like it makes you work so hard for something so simple.

Update 2

I found the functionality:

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'))
            ),
        );
    }
}

This does exactly what I want, but it says it's deprecated. Anyone knows an updated version of how to use this?

Update 3

Similar functionality can be also achieved with http://twig.sensiolabs.org/doc/tags/include.html

解决方案

You can use the twig functions for each part of a form row:

For example:

<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>

这篇关于带有树枝的自定义表单域模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆