Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony 带有细枝的自定义表单字段模板_Symfony_Twig - Fatal编程技术网

Symfony 带有细枝的自定义表单字段模板

Symfony 带有细枝的自定义表单字段模板,symfony,twig,Symfony,Twig,我想在twig中创建一个自定义模板来呈现表单字段 例如: {{ form_row(form.field) }} 这可以通过窗体主题化来覆盖 {% block form_row %} ... custom code {% endblock form_row %} 我想做的是: {% block custom_row %} ... custom code {% endblock custom_row %} 然后像这样使用它: {{ custom_row(form.field }} 但是,这会

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

例如:

{{ 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\u row

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

更新

我真正想要的是:

我使用twitter引导和一个包来覆盖所有表单主题。它在收音机周围渲染一个div,所以不能内联。所以我想这样做:

{{ custom_row(form.field }}
复制他们的模板并删除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 %}
我最终覆盖了整个主题,并在有问题的div周围添加了ifs,一个类(radio inline)。但我仍在想是否有办法让这一切顺利进行。似乎这让你为了这么简单的事情而努力工作

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


类似的功能也可以通过

实现。您可以对表单行的每个部分使用细枝功能:

例如:


{{form_label(form.field)}{{{#字段的名称}
{{form_errors(form.field)}{{the field}
{{form_小部件(form.field)}{{#与字段#关联的错误}
您可以使用

逐步:

1.表单类型类 请在您的类中检查名称

public function getName() {
  return 'hrQuestionResponse';
}
2.在模板中包含自定义主题 3.找到街区 这可能相当困难。对于引导包,正如您在./vendor/brainscrafted/bootstrap bundle/brainscrafted/bundle/BootstrapBundle/Resources/views/Form/bootstrap.html.twig中找到的一样,您已经找到了block radio_行。我一直在通过将输出放入源模板并覆盖比我需要的更多的块来查找块。在2.7中有一个主题“”

4.覆盖块 从主模板复制该块,并将其称为替换标准术语(在步骤1中找到的FormType中使用的名称)

别忘了还要更改endblock名称

e、 g


在您的情况下,因为您只能调用form_widget(),所以需要覆盖_widget。您可以只提取所需的内容,也可以将块链覆盖到单选行。

您想将其用于一个或多个字段吗?一个,尽管我不认为这有什么不同。个性化一个字段比定义自定义模板更容易。您的目标是什么?预期的结果是什么?我的目标是创建一个可以从twig调用的函数,就像form_row、form_widget、radio_row一样。。等等,这将呈现我的模板。我知道可能有解决办法。。。现在我已经覆盖了一个默认模板(在我的例子中是radio_行),并添加了一些if来实现我想要的。但我很好奇是否有一种方法可以定义自定义模板。在ASP.NET MVC中使用类似于部分视图的功能是很有用的。是的,我知道我可以做到。但这不是问题所在。这个例子很简单,但我需要解决更复杂的问题。
{% form_theme form 'InterlatedCamsBundle:Form:fields.html.twig' %}
{% block hrQuestionResponse_widget %}
hrQuestionResponse_row
{% spaceless %}
    {% set class = '' %}
...
 {% endspaceless %}
{% endblock hrQuestionResponse_widget %}