3

我正在使用以下代码渲染原型:

{{form_widget(form.get('prototype').myField, {'attr': {'value': '<%= myModelProperty %>'} }) }}

BackboneJS应该读取这个 twig 块生成的代码,并将 <%= myModelProperty %> 替换为一些模型属性值。

这不会发生,因为该值在 twig 中被转义,因此被替换为:

&lt;%= viewport %&gt;

我试图在 *form_div_layout.html* 文件中将值强制为 RAW:

> {% block field_widget %} {% spaceless %}
>     {% set type = type|default('text') %}
>     <input type="{{ type }}" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value|raw }}" {% endif %}/> {%
> endspaceless %} {% endblock field_widget %}

但没有成功。

所以我的问题是如何不逃避树枝中的字段值。

谢谢!

编辑

解决方案:所以实际上方法是正确的,我必须使用“原始”过滤器来使我的变量不被转义。我有一个自动转义块集,可以包含这个特定的输出,这就是为什么它必须“未转义”的原因。

Symfony 2 的 Twig 包提供了几个块来渲染表单数据,这些块使用一个特定的块来进行属性渲染,称为“{% block widget_attributes %}”。

我所做的是编辑这个块(我有一个包含所有自定义块的单独模板文件),所以我可以添加一层“是否应该转义这个值”:

{% block widget_attributes %}
{% spaceless %}
    id="{{ id }}" name="{{ full_name }}"{% if read_only %} disabled="disabled"{% endif %}{% if required %} required="required"{% endif %}{% if max_length %} maxlength="{{ max_length }}"{% endif %}{% if pattern %} pattern="{{ pattern }}"{% endif %}

    {% for attrname,attrvalue in attr %}
        {# Attribute value can be defined as an array. ['data']: contains the actual value, ['escape']: boolean (true if the value HAS to be escaped)#}
        {% if attrvalue.data is defined %}
            {% if not attrvalue.escape %}
                {{attrname}}="{{ attrvalue.data|raw }}"
            {% else %}
                {{attrname}}="{{ attrvalue.data|e }}"
            {% endif %}
        {% else %}
            {{attrname}}="{{attrvalue}}"
        {% endif %} 
    {% endfor %}

{% endspaceless %}
{% endblock widget_attributes %}

从我的树枝文件中调用:

{{ form_widget(form.displays.get('prototype').myField, {'attr': {'value': { 'data': myvalue, 'escape': false } } }) }}

转义是在 {{ }} twig 标签中打印值时完成的,所以我之前所做的是将未转义的值发送到实际调用 print 并且值因此被转义的块。

这对我有用!谢谢!

4

2 回答 2

3

Solution: So in fact the method was right, I have to use the raw filter to get my variable not escaped. I've an autoescape block set that englobe this particular output which is why the reason it has to be "un-escaped".

Twig bundle of Symfony 2 provided several block to render form data, and those uses a specific block for attribute rendering called {% block widget_attributes %}.

What I did is edit this block (I've a separated template file with all my customized blocks) so I can add a layer of "should this value be escaped or not":

{% block widget_attributes %}
  {% spaceless %}
      id="{{ id }}" name="{{ full_name }}"{% if read_only %} disabled="disabled"{% endif %}{% if required %} required="required"{% endif %}{% if max_length %} maxlength="{{ max_length }}"{% endif %}{% if pattern %} pattern="{{ pattern }}"{% endif %}

      {% for attrname,attrvalue in attr %}
        {# Attribute value can be defined as an array. ['data']: contains the actual value, ['escape']: boolean (true if the value HAS to be escaped)#}
        {% if attrvalue.data is defined %}
          {% if not attrvalue.escape %}
            {{ attrname }}="{{ attrvalue.data|raw }}"
          {% else %}
            {{ attrname }}="{{ attrvalue.data|e }}"
          {% endif %}
        {% else %}
          {{ attrname }}="{{ attrvalue }}"
        {% endif %} 
      {% endfor %}

  {% endspaceless %}
{% endblock widget_attributes %}

Called from my twig file:

{{ form_widget(form.displays.get('prototype').myField, {'attr': {'value': { 'data': myvalue, 'escape': false } } }) }}

The escape is done when printing the value so in the {{ }} twig tag, so what I was doing earlier was sending an unescaped value to a block where the print is actually called and where the value was thus escaped.

This works for me! thanks!

于 2012-08-02T11:37:44.337 回答
1

使用原始过滤器是执行此操作的正确方法。如果你没有成功,那么还有其他问题。尝试在 app/config/config.yml 中清除缓存或禁用树枝缓存

twig:
    cache: ~

如果这不是缓存问题,那么我不确定下一步该往哪里看。

于 2012-07-10T17:34:31.540 回答