如何在渲染表单时删除奇数属性

How to delete the odd attributes while rendering form

我重写了 bootstrap_3_layout.html.twig 中的一些小部件,我不希望表单字段在呈现时具有某些属性。

我找到了一些小部件的顺序 button_widgetbutton_rowform_widgetwidget_attributes

我做了一点改变

{% block widget_attributes %}
    {% spaceless %}
    {# bla-bla-bla #}
        {% for attrname, attrvalue in attr %}
            {% if attrname in ['placeholder', 'title'] %}
                {{ attrname }}="{{ attrvalue|trans({}, translation_domain) }}"
            {% elseif attrname not in ['first','last','data-help'] %}
                {{ attrname }}="{{ attrvalue }}"
            {% endif %}
        {% endfor %}
    {% endspaceless %}
{% endblock widget_attributes %}

但它不适用于按钮。

我不确定你在实现什么,但你可以截取树枝循环的奇数索引如下:

{% for attrname, attrvalue in attr %}
      {% if loop.index is odd %}
          odd 
      {% else %}
          even
      {% endif %}
{% endfor %}

有关 loop variable and odd test function 的更多信息。

希望对您有所帮助

很奇怪,但是 twig_bridge 中的表单布局有一些小部件可以处理属性。

1. 对于输入 `widget_attributes` 2. 对于按钮 `button_attributes` 3. 对于另一个元素 `widget_container_attributes`

对于每个小部件,在 twig-bridge 中有下一个块:

  1. {name_of_widget}_widget - 渲染元素的主块
  2. {name_of_widget}_label
  3. {name_of_widget}_row

对于属性,有

  1. widget_attributes
  2. widget_container_attributes
  3. button_attributes
  4. attributes

我只是用错了一个:) 感谢大家的耐心等待。