Php Symfony2:细枝自动转义

Php Symfony2:细枝自动转义,php,symfony,twig,Php,Symfony,Twig,我遇到了一个关于Twig在Symfony2逃跑的问题 问题 我目前正在使用Symfony的form builder创建一个表单,用于管理项目的类别。 我当前创建表单的代码如下: $Form ->add('title', 'text', array('label' => 'Title', 'attr' => array('class' => 'span8'))) ->add('parent', 'entity', array( 'lab

我遇到了一个关于Twig在Symfony2逃跑的问题

问题 我目前正在使用Symfony的form builder创建一个表单,用于管理项目的类别。 我当前创建表单的代码如下:

$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\u value”键旁边添加了一个简单的标记。因此,我在选择列表中的第一个选项是“新建类别”

我试过的
  • 小枝自动逃逸

    {% autoescape false %}
        {{ form_row(form.parent) }}
    {% endautoescape %}
    
  • 小枝延伸

  • 我试着编写自定义的细枝扩展,唯一的目的是转义(html_解码)我传递给它的整个对象集,但仍然不行。不幸的是,我没有保存代码将其粘贴到这里,因此我将提供一个链接,其中另一位用户提出了与我相同的方法(实际上是针对JSON的,但概念是相同的)

    那么,简单地说,作为我的最后一个想法——为了在我的选择列表中使用“strong”或“ ”之类的HTML而不转义,我必须做什么

    提前谢谢。

    在这种情况下,您是不是更好的选择

    你可以在小树枝上试试。实际上,您可以在模板中创建一个具有特殊名称的块,并在其中自定义显示

    块命名约定是
    {field\u id}\u行
    {field\u id}\u小部件
    。比如说:

    {% block _parent_widget %}
        {# spit out the select field here with whatever you need #}
    {% endblock %}
    
    查看以了解如何输出选择:

    {% 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 %}
    

    起初我想到了选项组,但在我的例子中,我使用嵌套树集,在我的例子中,我有级别3和级别4的类别,选项组在这里不适合。稍后我将尝试您展示的另一种方法。如果我设法让它像那样工作,或者没有任何其他响应代码更少,我会将该答案标记为正确。谢谢你。你应该这样做。{code>{{htmlvar|raw}raw将打印html更正。是的,我也忘了提到这一点。我确实设置了原始值——仍然是相同的输出。
    {% form_theme your_form_name _self %}