创建 Symfony2 表单主题 - 字段集和列表样式

发布于 2024-12-05 06:36:56 字数 96 浏览 1 评论 0原文

我用的是symfony2。我正在尝试覆盖 twig 中默认的 div 样式表单块。

首先,是否有人拥有或知道字段集和列表(ul -> li)方法的可用实现?

I am using symfony2. I am trying to override the default div style form blocks in twig.

First, does any have or know of an available implementation of the fieldset and list (ul -> li) approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

走走停停 2024-12-12 06:36:56

目前,我实现了这样的fieldset支持:

在类型中:

public function buildView(FormView $view, FormInterface $form, array $options)
{
    $view->setAttribute('fieldsets',
            array(
                array(
                    'legend' => 'film.group.date',
                    'content'=> array(
                        'theaters_release_date',
                        'storage_media_release',
                        'storage_media_release_date',
                        'vod_release_date'
                        )),
                array(
                    'legend' => 'film.group.country',
                    'content'=> array('countries')),
                    ));
}

我有一个名为fieldSet.html.twig的模板,该模板使用视图的属性:

{% macro fieldset_block(fieldset, form) %}
<fieldset{% if fieldset.subform is defined %} class="{{ fieldset.subform }}"{% endif %}>
    <legend>{{fieldset.legend | trans }}</legend>
    {% if fieldset.content is defined%}
      {% for row in fieldset.content %}
          {{ form_row(form[row]) }}
      {% endfor %}
    {% endif %}
    {% if fieldset.subform is defined %}
        {# Couldn't get some recursivity (simply call form widget) here... too bad #}
        {% if form[fieldset.subform].get('attr').fieldsets is defined %}
            {% for subfieldset in form[fieldset.subform].get('attr').fieldsets %}
                {{ _self.fieldset_block(subfieldset, form[fieldset.subform]) }}
            {% endfor %}
        {% else %}
            {% for row in form[fieldset.subform] %}
                {{ form_row(row) }}
            {% endfor %}
        {% endif %}
    {% endif %}
    {% if fieldset.items is defined%}
      {% for fieldset in fieldset.items %}
          {{ _self.fieldset_block(fieldset, form) }}
      {% endfor %}
    {% endif %}
</fieldset>
{%  endmacro %}

{% block form_widget %}
    {% for fieldset in form.get('attr').fieldsets %}
        {{ _self.fieldset_block(fieldset, form) }}
    {% endfor %}
{% endblock %}

For the moment, I implemented fieldset support like this:

in the Type:

public function buildView(FormView $view, FormInterface $form, array $options)
{
    $view->setAttribute('fieldsets',
            array(
                array(
                    'legend' => 'film.group.date',
                    'content'=> array(
                        'theaters_release_date',
                        'storage_media_release',
                        'storage_media_release_date',
                        'vod_release_date'
                        )),
                array(
                    'legend' => 'film.group.country',
                    'content'=> array('countries')),
                    ));
}

I have a template named fieldset.html.twig, that uses the attributes of the view:

{% macro fieldset_block(fieldset, form) %}
<fieldset{% if fieldset.subform is defined %} class="{{ fieldset.subform }}"{% endif %}>
    <legend>{{fieldset.legend | trans }}</legend>
    {% if fieldset.content is defined%}
      {% for row in fieldset.content %}
          {{ form_row(form[row]) }}
      {% endfor %}
    {% endif %}
    {% if fieldset.subform is defined %}
        {# Couldn't get some recursivity (simply call form widget) here... too bad #}
        {% if form[fieldset.subform].get('attr').fieldsets is defined %}
            {% for subfieldset in form[fieldset.subform].get('attr').fieldsets %}
                {{ _self.fieldset_block(subfieldset, form[fieldset.subform]) }}
            {% endfor %}
        {% else %}
            {% for row in form[fieldset.subform] %}
                {{ form_row(row) }}
            {% endfor %}
        {% endif %}
    {% endif %}
    {% if fieldset.items is defined%}
      {% for fieldset in fieldset.items %}
          {{ _self.fieldset_block(fieldset, form) }}
      {% endfor %}
    {% endif %}
</fieldset>
{%  endmacro %}

{% block form_widget %}
    {% for fieldset in form.get('attr').fieldsets %}
        {{ _self.fieldset_block(fieldset, form) }}
    {% endfor %}
{% endblock %}
鲸落 2024-12-12 06:36:56

这是一个简单的fieldset示例:
https://gist.github.com/spcmky/8512371

用列表,列表,列表,查看form_widget_compound和form_rows。你可以:

{% block fieldset_widget %}
    {% spaceless %}
        <fieldset {{ block('widget_container_attributes') }}>
            {% if title is defined %}<legend>{{ title }}</legend>{% endif %}
                <ul>
                {% for child in form %}
                    <li>
                        {{ form_widget(child) }}
                    </li>
                {% endfor %}
                </ul>
        </fieldset>
    {% endspaceless %}
{% endblock %}

Here is a simple fieldset example:
https://gist.github.com/spcmky/8512371

To replace the divs with a list, look at form_widget_compound and form_rows. You can:

{% block fieldset_widget %}
    {% spaceless %}
        <fieldset {{ block('widget_container_attributes') }}>
            {% if title is defined %}<legend>{{ title }}</legend>{% endif %}
                <ul>
                {% for child in form %}
                    <li>
                        {{ form_widget(child) }}
                    </li>
                {% endfor %}
                </ul>
        </fieldset>
    {% endspaceless %}
{% endblock %}
执着的年纪 2024-12-12 06:36:56

默认情况下,TWIG在渲染表单时使用DIV布局。但是,您可以在表布局中渲染表单。使用form_table_layout.html.twig资源使用这种布局:

# app/config/config.yml

twig:
    form:
        resources: ['form_table_layout.html.twig']

By default, Twig uses a div layout when rendering forms.However, you can render forms in a table layout. Use the form_table_layout.html.twig resource to use such a layout:

# app/config/config.yml

twig:
    form:
        resources: ['form_table_layout.html.twig']
装迷糊 2024-12-12 06:36:56

http://symfony.com/doc/doc/2.0/cookbook/form/form/form/form/form_customization.html

我不知道Thoose的实施,但是欢迎您制定它们并打开拉动请求。

http://symfony.com/doc/2.0/cookbook/form/form_customization.html

I am not aware of a implementation of thoose, but you are welcome to make them and open a Pull Request.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文