为什么Jinja在宏中逃避html? [英] Why does Jinja escape html in a macro?

查看:162
本文介绍了为什么Jinja在宏中逃避html?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Jinja宏来在模板中呈现一些表单数据,但是由于某种原因,表单数据将呈现为转义的文本而不是html!这是我的宏,表单的第一行名为:

  {%宏格式(字段)%} 
< tr>
< td> form。{{field}}。label | safe< / td>
< td>表单{{field}} | safe< / td>
< td> form。{{field}}。help_text | safe< / td>
< td>表单{{field}}。errors | safe< / td>
< / tr>
{%endmacro%}

{{formrow('item_name')}}

想法?我缺少什么?

解决方案

似乎你没有完全得到Jinja模板语法,不管我建议做什么像这样:

  {%宏格式(字段)%} 
< tr>
< td> form。{{field.label_tag()}}< / td>
< td> form。{{field.as_widget()}}< / td>
< td>表单。{{field.help_text | safe}}< / td>
< td> form。{{field.errors.as_ul()}}< / td>
< / tr>
{%endmacro%}

{{formrow('item_name')}}


I'm writing a Jinja macro to render some form data in a template, but for some reason the form data renders as escaped text instead of html! Here is my macro, with the first row of the form called:

{% macro formrow(field) %}
    <tr>
        <td>form.{{ field }}.label|safe</td>
        <td>form.{{ field }}|safe</td>
        <td>form.{{ field }}.help_text|safe</td>
        <td>form.{{ field }}.errors|safe</td>
    </tr>
{% endmacro %}

{{ formrow('item_name') }}

Ideas? What am I missing?

解决方案

It seems you are not getting the Jinja template syntax completely yet, regardless, I would recommend doing something like this:

{% macro formrow(field) %}
    <tr>
        <td>form.{{ field.label_tag() }}</td>
        <td>form.{{ field.as_widget() }}</td>
        <td>form.{{ field.help_text|safe }}</td>
        <td>form.{{ field.errors.as_ul() }}</td>
    </tr>
{% endmacro %}

{{ formrow('item_name') }}

这篇关于为什么Jinja在宏中逃避html?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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