【发布时间】:2015-12-14 04:40:29
【问题描述】:
在渲染时如何阻止 Deform 在字段标题或描述中转义 HTML?我目前最好的解决方案是用我需要的搜索/替换返回的渲染 HTML 字符串。
默认情况下,变形会将所有 HTML 字符转义为 HTML 实体,我想在其中一个字段描述中添加标签。
【问题讨论】:
标签: python pyramid chameleon deform colander
在渲染时如何阻止 Deform 在字段标题或描述中转义 HTML?我目前最好的解决方案是用我需要的搜索/替换返回的渲染 HTML 字符串。
默认情况下,变形会将所有 HTML 字符转义为 HTML 实体,我想在其中一个字段描述中添加标签。
【问题讨论】:
标签: python pyramid chameleon deform colander
复制默认的小部件模板并对其进行修改以允许未转义的条目。
描述由mapping.pt 放置。它不能被每个小部件覆盖 - 映射模板对于表单中的所有项目都是相同的。您可以通过将item_template 传递给您的小部件容器(表单,表单部分)来覆盖映射。未经测试的例子:
# No .pt extension for the template!
schema = CSRFSchema(widget=deform.widget.FormWidget(item_template="raw_description_mapping"))
You can use TAL structure expression to unescape HTML。
例如变形 2 的示例 raw_description_mapping.pt:
<tal:def tal:define="title title|field.title;
description description|field.description;
errormsg errormsg|field.errormsg;
item_template item_template|field.widget.item_template"
i18n:domain="deform">
<div class="panel panel-default" title="${description}">
<div class="panel-heading">${title}</div>
<div class="panel-body">
<div tal:condition="errormsg"
class="clearfix alert alert-message error">
<p i18n:translate="">
There was a problem with this section
</p>
<p>${errormsg}</p>
</div>
<div tal:condition="description">
${structure: description}
</div>
${field.start_mapping()}
<div tal:repeat="child field.children"
tal:replace="structure child.render_template(item_template)" >
</div>
${field.end_mapping()}
</div>
</div>
</tal:def>
在使用 Pyramid 的 Configurator 构建 WSGI 应用程序时,您还需要修改 Pyramid 应用程序以加载覆盖的变形模板:
from pyramid_deform import configure_zpt_renderer
configure_zpt_renderer(["mypackage:templates/deform", "mypackage2.submodule:form/templates/deform"])
【讨论】: