Symfony的2,嫩枝:怎么不逃字段值(与backbonejs和放大器使用; symfony的2) [英] Symfony 2, Twig: how not to escape field value (used with backbonejs & symfony 2)

查看:124
本文介绍了Symfony的2,嫩枝:怎么不逃字段值(与backbonejs和放大器使用; symfony的2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面code渲染的原型:

I'm rendering a prototype using below code:

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

BackboneJS 的应该阅读这树枝块所产生的code和更换的&lt;一些模型属性值(%)= myModelProperty%>

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

和这种情况不会发生,因为该值在树枝逃脱,因而改为:

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

&lt;%= viewport %&gt;

我试着值强制RAW在* form_div_layout.html *文件:

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

但没有成功。

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

谢谢!

修改

解决方案:
所以其实方法是正确的,我必须使用原始过滤器得到我的变量没有逃过。
我有一个autoescape块设置englobe这个特定的输出这就是为什么它是原因非逃。

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束提供了几种块渲染表单数据,以及那些用来属性的特定块渲染所谓{%块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 } } }) }}

打印值时,所以在{{}}树枝标签,所以我前面所做的是发送转义值块,其中印刷实际上是调用,其中值就这样逃脱逃亡完成。

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!

推荐答案

解决方案:所以其实方法是正确的,我必须使用原材料过滤得到我的变量没有逃过。我有一个 autoescape 块设置englobe这个特定的输出这就是为什么它是原因非逃。

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束提供了几种块渲染表单数据,以及那些用来属性的特定块渲染名为 {%块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 } } }) }}

打印值时,逃避是这样做的 {{}} 树枝标签,所以我在做什么早些时候发送一个转义值块,其中印刷实际上是调用,其中值就这样逃了出来。

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,嫩枝:怎么不逃字段值(与backbonejs和放大器使用; symfony的2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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