5

How would you stop Deform from escaping HTML in field titles or descriptions when rendering? My current best solution is to search/replace the returned rendered HTML string with what I need.

Deform by default will escape all HTML characters to HTML entities, I want to add an tag in one of the field descriptions.

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
Oli
  • 1,335
  • 1
  • 9
  • 10

1 Answers1

3

Copy the default widget template and modify it to allow unescaped entries.

Descriptions are placed by 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). 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.

E.g. example raw_description_mapping.pt for Deform 2:

<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:

    from pyramid_deform import configure_zpt_renderer

    configure_zpt_renderer(["mypackage:templates/deform", "mypackage2.submodule:form/templates/deform"])
Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435