Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony 2, Twig: how not to escape field value (used with backbonejs & symfony 2)
    text
    copied!<p>I'm rendering a prototype using below code:</p> <pre><code>{{form_widget(form.get('prototype').myField, {'attr': {'value': '&lt;%= myModelProperty %&gt;'} }) }} </code></pre> <p><em>BackboneJS</em> is supposed to read the code generated by this twig block, and replace the &lt;%= myModelProperty %> by some model property value.</p> <p>And this doesn't happen because the value is escaped in twig and thus replaced by: </p> <pre><code>&amp;lt;%= viewport %&amp;gt; </code></pre> <p>I've tried to force the value to RAW in the *form_div_layout.html* file:</p> <pre><code>&gt; {% block field_widget %} {% spaceless %} &gt; {% set type = type|default('text') %} &gt; &lt;input type="{{ type }}" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value|raw }}" {% endif %}/&gt; {% &gt; endspaceless %} {% endblock field_widget %} </code></pre> <p>but without success.</p> <p><strong>So my question is how not to escape the field value in twig.</strong></p> <p>Thanks!</p> <p>EDIT</p> <p>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".</p> <p>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 %}".</p> <p>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":</p> <pre><code>{% 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 %} </code></pre> <p>Called from my twig file:</p> <pre><code>{{ form_widget(form.displays.get('prototype').myField, {'attr': {'value': { 'data': myvalue, 'escape': false } } }) }} </code></pre> <p>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.</p> <p>This works for me! thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload