0

我有问题。我想用标志 ico 做选择国家,但我不知道如何为表单选择文件创建自定义主题。

我创建了测试表格:

->add('name', 'choice', array('choices' =>array('en' => 'England', 'de' => 'Deutshland')))

接下来在我看来,我尝试

{% block _send_name_widget %}
    <select>
        {% for f in form %}
            {{ loop.index }}
        {%endfor%}
    </select>
{% endblock%}

{{ form_widget(form.name) }}

在我的 html 代码中,我得到了:

<select>
 1
 2
</select>

<select>
</select>

你能告诉我为什么吗?如何仅使用参数渲染一个选择?

4

2 回答 2

3

我的choice_widget_options 模板中不存在变量“options”。事实是“选项”不是变量的正确名称。如果你看一下\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Resources\views\Form\choice_widget_options.html.php,你会发现 Symfony 使用了一个名为 'choices' 的变量。

先前代码的更正版本是:

{% block choice_widget_options %}
{% spaceless %}
    {% for group_label, choice in choices %}
        {% if choice is iterable %}
            <optgroup label="{{ group_label|trans({}, translation_domain) }}">
                {% set options = choice %}
                {{ block('choice_widget_options') }}
            </optgroup>
        {% else %}
            <option value="{{ choice.value }}"{% if choice is selectedchoice(value) %} selected="selected"{% endif %}>
              <img src="/images/flags/{{ choice.label }}.jpg" />
              {{ choice.label|trans({}, translation_domain) }}
        </option>
    {% endif %}
{% endfor %}
{% endspaceless %}
{% endblock choice_widget_options %}
于 2014-03-19T15:24:20.977 回答
1

覆盖选择模板:

{% block choice_widget_options %}
{% spaceless %}
    {% for group_label, choice in choices %}
        {% if choice is iterable %}
            <optgroup label="{{ group_label|trans({}, translation_domain) }}">
                {% set options = choice %}
                {{ block('choice_widget_options') }}
            </optgroup>
        {% else %}
            <option value="{{ choice.value }}"{% if choice is selectedchoice(value) %} selected="selected"{% endif %}>
                  <img src="/images/flags/{{ choice.label }}.jpg" />
                  {{ choice.label|trans({}, translation_domain) }}
            </option>
        {% endif %}
    {% endfor %}
{% endspaceless %}
{% endblock choice_widget_options %}

Symfony2 文档中有关表单主题的更多信息

于 2013-10-22T11:11:54.667 回答