繁体   English   中英

Symfony2:Twig自动转义

[英]Symfony2: Twig autoescaping

我遇到了有关Twig在Symfony2中逃脱的问题。

问题

我目前正在使用Symfony的表单生成器来创建用于管理项目类别的表单。 我当前用于创建表单的代码如下:

$Form
    ->add('title', 'text', array('label' => 'Title', 'attr' => array('class' => 'span8')))
    ->add('parent', 'entity', array(
        'label' => 'Category',
        'attr' => array('class' => 'selectPicker span8'),
                'property' => 'indentedTitle',
                'empty_value' => ' -- New Category --',
                'required' => false,
                'class' => 'News\Entity\Category',
                'query_builder' => function(EntityRepository $Repository) {
                    return $Repository->createQueryBuilder('c')
                            ->orderBy('c.root, c.left');
                    }
                ))
    ->add('commit', 'submit', array('label' => 'Save', 'attr' => array('class' => 'btn btn-info')));

我在实体“ indentedTitle”中添加的回调仅在标题之前添加了两行,具体取决于树集中的类别级别。

public function getIndentedTitle() {
    return str_repeat("--", $this->level) . $this->title;
}

到目前为止,一切工作正常,除了当我尝试添加一些HTML代码来修改我在选择列表中输出的类别名称时,它会自动转义。 例如,您可以看到我在表单生成器中的“ empty_value”键旁边添加了一个简单的&nbsp标记。 因此,我在选择列表中将“&nbsp-新建类别-”作为第一个选项。

我尝试了什么

  1. 树枝自动转义

     {% autoescape false %} {{ form_row(form.parent) }} {% endautoescape %} 
  2. 树枝延伸

我试图编写自定义的Twig扩展名,其目的仅仅是转义(html_decode)我传递给它的整个对象集-仍然不好。 不幸的是,我没有保存代码以将其粘贴到此处,因此我将提供一个链接,其中另一个用户提出了与我相同的方法(实际上是针对JSON,但概念相同)。 链接到答案

因此,简单地说来就是我的最终想法-为了在我的选择列表中使用诸如“ strong”或“&nbsp”之类的HTML而不逃脱它,我该怎么办?

提前致谢。

在这种情况下, 选项组也许是更好的选择?

您可以尝试在树枝中自定义单个表单字段 本质上,您可以在模板中使用特殊名称创建一个块,然后自定义显示。

块命名约定为_{field_id}_row_{field_id}_widget 所以像这样:

{% block _parent_widget %}
    {# spit out the select field here with whatever you need #}
{% endblock %}

查看Twig桥代码 ,看看如何输出select:

{% block choice_widget_collapsed %}
{% spaceless %}
    {% if required and empty_value is none and not empty_value_in_choices %}
        {% set required = false %}
    {% endif %}
    <select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %}>
        {% if empty_value is not none %}
            <option value=""{% if required and value is empty %} selected="selected"{% endif %}>{{ empty_value|trans({}, translation_domain) }}</option>
        {% endif %}
        {% if preferred_choices|length > 0 %}
            {% set options = preferred_choices %}
            {{ block('choice_widget_options') }}
            {% if choices|length > 0 and separator is not none %}
                <option disabled="disabled">{{ separator }}</option>
            {% endif %}
        {% endif %}
        {% set options = choices %}
        {{ block('choice_widget_options') }}
    </select>
{% endspaceless %}
{% endblock choice_widget_collapsed %}

{% block choice_widget_options %}
{% spaceless %}
    {% for group_label, choice in options %}
        {% 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 %}>{{ choice.label|trans({}, translation_domain) }}</option>
        {% endif %}
    {% endfor %}
{% endspaceless %}
{% endblock choice_widget_options %}

然后,您告诉twig当前模板也是表单主题:

{% form_theme your_form_name _self %}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM