简体   繁体   English

在Deform表单描述字段中允许原始HTML

[英]Allow raw HTML in Deform form description fields

How would you stop Deform from escaping HTML in field titles or descriptions when rendering? 在渲染时,如何阻止Deform在字段标题或描述中转义HTML? My current best solution is to search/replace the returned rendered HTML string with what I need. 我目前最好的解决方案是使用我需要的内容搜索/替换返回的呈现HTML字符串。

Deform by default will escape all HTML characters to HTML entities, I want to add an tag in one of the field descriptions. 默认情况下,变形会将所有HTML字符转义为HTML实体,我想在其中一个字段描述中添加标记。

Copy the default widget template and modify it to allow unescaped entries. 复制默认小部件模板并对其进行修改以允许未转义的条目。

Descriptions are placed by mapping.pt . 描述由mapping.pt放置。 It cannot be overridden per widget basis - the mapping template is the same for all the items in the form. 它不能基于每个小部件覆盖 - 映射模板对于表单中的所有项都是相同的。 You can override mapping by passing item_template to your widget container (Form, Form section). 您可以通过将item_template传递到窗口小部件容器(窗体,窗体部分)来覆盖映射。 Non-tested example: 未经测试的例子:

  # 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 . 您可以使用TAL structure表达式来转换HTML

Eg example raw_description_mapping.pt for Deform 2: 例如,变形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>

You also need to modify your Pyramid application to load overridden Deform templates when constructing WSGI application with Pyramid's Configurator: 在使用Pyramid的Configurator构建WSGI应用程序时,您还需要修改Pyramid应用程序以加载重写的Deform模板:

    from pyramid_deform import configure_zpt_renderer

    configure_zpt_renderer(["mypackage:templates/deform", "mypackage2.submodule:form/templates/deform"])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM