Skip to main content

Passing a Custom Property to a Formatting Template

Sometimes you may need to dynamically control what is displayed in a formatting template, e.g. toggle display of the form title, etc. This can be done by passing a custom property to the formatting template, eliminating the need to have duplicate, mostly-identical formatting templates.

Instructions

1

Step 1

In your custom formatting template, add in a custom property. For the purpose of this example, we'll set up a conditional around the form title. We'll call that property showFormTitle. It can be accessed through form.properties.

{% if form.properties.showFormTitle %}
<h2>{{ form.name }}</h2>
{% endif %}

In a very basic formatting template, that might look something like this:

{# Opening <form> tag #}
{{ form.renderTag }}

{# Custom Property #}
{% if form.properties.showFormTitle %}
<h2>{{ form.name }}</h2>
{% endif %}

{# Display Error banner and general errors if applicable #}
{% if form.hasErrors %}
<div class="freeform-form-has-errors">
{{ "Error! Please review the form and try submitting again."|t('freeform') }}

{% if form.errors|length %}
<ul>
{% for error in form.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
</div>
{% endif %}

{# Render form fields #}
{% for row in rows %}
{% set width = (12 / (row|length)) %}
<div{{ form.attributes.row|raw }}>
{% for field in row %}
{{ field.render() }}
{% endfor %}
</div>
{% endfor %}

{# Closing </form> tag #}
{{ form.renderClosingTag }}
2

Step 2

{{ freeform.form("myFormHandle").render({ showFormTitle: true }) }}

OR

{{ form.render({ showFormTitle: true }) }}
Finished!

You can add as many custom properties as you like, but be sure not to use any reserved words such as class, id, etc (anything that is already a parameter/property name in the Form query).

Page Feedback