Escape html entities twig form widget

时间:2018-07-25 05:09:18

标签: symfony twig

I need to escape those double quotes in post_data.postCaption1 as below. But it seems not working. How to do it?

<div class="form-group">
      {% autoescape %}
        {{ form_widget(blog_form.post_caption1, { 'attr': {'class': 'form-control', 'placeholder': 'Enter caption for image 1', 'value': post_data.postCaption1|raw } }) }} {% endautoescape %}
        {{ form_errors(blog_form.post_caption1) }}
    </div>

1 个答案:

答案 0 :(得分:0)

If you use the raw filter, post_data.postCaption1 won't be escaped because raw is the last filter applied to it:

{% autoescape %}
    {{ var|raw }} {# var won't be escaped #}
{% endautoescape %}

You can try:

{% set strategy = 'html' %}
{% autoescape %}
    {{ form_widget(blog_form.post_caption1, { 'attr': {'class': 'form-control', 'placeholder': 'Enter caption for image 1', 'value': post_data.postCaption1|escape(strategy) } }) }} 
{% endautoescape %}
{{ form_errors(blog_form.post_caption1) }}
相关问题