Symfony 2,Twig:如何不转义字段值(与backbonejs & symfony 2 一起使用) [英] Symfony 2, Twig: how not to escape field value (used with backbonejs & symfony 2)

查看:16
本文介绍了Symfony 2,Twig:如何不转义字段值(与backbonejs & symfony 2 一起使用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm rendering a prototype using below code:

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

BackboneJS 应该读取此树枝块生成的代码,并将 <%= myModelProperty %> 替换为某些模型属性值.

BackboneJS is supposed to read the code generated by this twig block, and replace the <%= myModelProperty %> by some model property value.

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

And this doesn't happen because the value is escaped in twig and thus replaced by:

&lt;%= viewport %&gt;

我已尝试将 *form_div_layout.html* 文件中的值强制设为 RAW:

I've tried to force the value to RAW in the *form_div_layout.html* file:

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

但没有成功.

所以我的问题是如何不转义树枝中的字段值.

谢谢!

编辑

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

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".

Symfony 2 的 Twig bundle 提供了几个用于渲染表单数据的块,这些块使用称为{% block widget_attributes %}"的特定块进行属性渲染.

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 } } }) }}

在 {{ }} twig 标签中打印值时完成转义,所以我之前所做的是将未转义的值发送到实际调用打印的块,并在那里转义该值.

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.

这对我有用!谢谢!

推荐答案

Solution: 所以实际上方法是对的,我必须使用 raw 过滤器来让我的变量不被转义.我有一个 autoescape 块集,可以将这个特定的输出包围起来,这就是为什么它必须未转义"的原因.

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".

Symfony 2 的 Twig bundle 提供了几个用于渲染表单数据的块,这些块使用称为 {% block widget_attributes %} 的特定块进行属性渲染.

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 } } }) }}

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

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.

这对我有用!谢谢!

这篇关于Symfony 2,Twig:如何不转义字段值(与backbonejs &amp; symfony 2 一起使用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆