首页 \ 问答 \ Symfony 2,Twig:如何不逃避字段值(与backbonejs和symfony 2一起使用)(Symfony 2, Twig: how not to escape field value (used with backbonejs & symfony 2))

Symfony 2,Twig:如何不逃避字段值(与backbonejs和symfony 2一起使用)(Symfony 2, Twig: how not to escape field value (used with backbonejs & symfony 2))

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

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

BackboneJS应该读取此twig块生成的代码,并用某个model属性值替换<%= myModelProperty%>。

并且这不会发生,因为值在树枝中被转义,因此被替换为:

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

但没有成功。

所以我的问题是如何避免在树枝中逃脱现场价值。

谢谢!

编辑

解决方案:事实上,该方法是正确的,我必须使用“原始”过滤器来获取我的变量未转义。 我有一个autoescape块集,它可以使这个特定的输出成为可能,这就是为什么它必须是“未转义”的原因。

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

从我的twig文件中调用:

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

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

这适合我! 谢谢!


I'm rendering a prototype using below code:

{{form_widget(form.get('prototype').myField, {'attr': {'value': '<%= 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;

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

but without success.

So my question is how not to escape the field value in twig.

Thanks!

EDIT

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!


原文:https://stackoverflow.com/questions/11417510
更新时间:2023-06-02 12:06

最满意答案

你得到的错误是(在toploop中键入以下内容)¹:

# (Obj.magic 43: Unix.error);;
- : Unix.error = Unix.EPROTOTYPE

这意味着: 协议错误类型的套接字 。 因此,您必须检查如何初始化套接字。

¹您还可以计算unix.mli的异常,因为知道第一个例外, E2BIG0 。 Emacs Cu 43 ↓帮助。


The error you get is (type the following in the toploop)¹:

# (Obj.magic 43: Unix.error);;
- : Unix.error = Unix.EPROTOTYPE

which means: Protocol wrong type for socket. So you have to examine how you initialize your socket.

¹ You can also count the exceptions in unix.mli, knowing that the first one, E2BIG, is 0. Emacs C-u 43 ↓ helps.

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)