树枝和自蔓延 [英] Twig and autoescaping

查看:126
本文介绍了树枝和自蔓延的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Symfony2。目前,我试图在树枝模板中呈现表单标签。该标签包含一个html标签,在我的枝条文件中没有正确显示。



下面是创建表单字段的代码片段:

  $ builder-> add('zipcode','integer',array(
'label'=>'< abbr title =Zone Improvement Plan> CAP< / abbr> code',
));

在twig文件中,我渲染字段标签如下:

  {{form_label(form.zipcode)}} 


$ b $我尝试过raw,escape,e过滤器,但html页面中提供的结果始终是字符串

  < abbr title =区域改进计划> CAP< / abbr>代码

而不是相应的HTML代码!



<任何建议?
提前感谢!



稍后我找到了解决方案。
解决方案是禁用Symfony提供的标签块中的自动版本路径:
symfony / src / Symfony / Bridge / Twig / Resources / views / Form / form_div_layout.html.twig



所以,在你的twig文件中,你必须将以下行放在窗体之外:
{%form_theme form _self%}

  {%block generic_label%} 
{%spaceless%}
{%如果需要%}
{%set attr = attr | merge ({'class':attr.class | default('')〜'required'})%}
{%endif%}
< label {%for attrname,attrvalue in attr%} { {attrname}} ={{attrvalue}}{%endfor%}> {%autoescape false%} {{label | trans}} {%endautoescape%}< / label>
{%endspaceless%}
{%endblock%}


解决方案

JeanValjean 自己:

  {%autoescape false%} {{form.zipcode.vars.label | trans}} {%endautoescape%} 

为了将此行为推广到整个应用程序,您可以覆盖标签的表单块:

  {%block generic_label%} 
{%spaceless%}
{ %如果需要%}
{%set attr = attr | merge({'class':attr.class | default('')〜'required'})%}
{%endif%}
< label {%for attrname,attrvalue in attr%} {{attrname}} ={{attrvalue}}{%endfor%}>
{%autoescape false%} {{label | trans}} {%endautoescape%}
< / label>
{%endspaceless%}
{%endblock%}


I'm learning Symfony2. Currently, I'm trying to render a form label in a twig template. The label includes an html tag, that is not rendered correctly in my twig file.

Here follows the piece of code where the form field is created:

$builder->add('zipcode', 'integer', array(
        'label' => '<abbr title="Zone Improvement Plan">CAP</abbr> code',
        ));

In the twig file I render the field label as follows:

{{ form_label(form.zipcode) }}

I tried the raw, escape, e filters, but the results provided in my html page is always the string

 <abbr title="Zone Improvement Plan">CAP</abbr> code

and not the corresponding HTML code!

Any suggestion? Thanks in advance!

Later I found the solution. The solution is to disable the autoescape within the label block provided by Symfony at path: symfony / src / Symfony / Bridge / Twig / Resources / views / Form / form_div_layout.html.twig

So, in your twig file you have to put the following lines outside the form: {% form_theme form _self %}

{% block generic_label %}
{% spaceless %}
  {% if required %}
      {% set attr = attr|merge({'class': attr.class|default('') ~ ' required'}) %}
  {% endif %}
  <label{% for attrname,attrvalue in attr %} {{attrname}}="{{attrvalue}}"{% endfor %}>{% autoescape false %}{{ label|trans }}{% endautoescape %}</label>
{% endspaceless %}
{% endblock %}

解决方案

From JeanValjean himself :

{% autoescape false %}{{ form.zipcode.vars.label | trans }}{% endautoescape %}

And to generalize this behaviour to your whole app, you can override the form block for labels :

{% block generic_label %}
    {% spaceless %}
        {% if required %}
            {% set attr = attr|merge({'class': attr.class|default('') ~ ' required'}) %}
        {% endif %}
        <label{% for attrname,attrvalue in attr %} {{attrname}}="{{attrvalue}}"{% endfor %}>
            {% autoescape false %}{{ label|trans }}{% endautoescape %}
        </label>
    {% endspaceless %}
{% endblock %}

这篇关于树枝和自蔓延的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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