Php Symfony2:细枝自动转义
我遇到了一个关于Twig在Symfony2逃跑的问题 问题 我目前正在使用Symfony的form builder创建一个表单,用于管理项目的类别。 我当前创建表单的代码如下: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
$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 %}
{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 %}