1

I want to be able to toggle the display of a form's Title on or off, without having to resort to using a mostly-identical copy of the formatting template.

Freeform formatting template:

{% if showFormTitle %}{{ form.name }}{% endif %}

How can I control the value of showFormTitle from the Twig template that renders the form? This (and all the permutations of it that I've tried) doesn't work:

{% set showFormTitle = true %}
{{ craft.freeform.form(myForm).render() }}
{% set showFormTitle = true %}
{{ craft.freeform.form(myForm).render() with { showFormTitle: showFormTitle } }}

1 Answer 1

3

This could potentially be handled a few different ways...

1. Move the Form Title location

Remove the Title from the formatting template and set up your main template loading the form to look something like this:

{% set form = craft.freeform.form('myForm')  %}

{% if showFormTitle %}
   {{ form.name }}
{% endif %}

{{ form.render }}

2. Wait for Freeform 3.12

Freeform 3.12 will be going into beta very soon and will have the ability to add something like this to the form payload:

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

And in your formatting template, use:

{% if form.properties.showFormTitle %}
   {{ form.name }}
{% endif %}

3. Use Conditional Rules feature

You could use a combination of using the overrideValues parameter, Conditional Logic, and an HTML field with Twig parsing on.

  1. Go to the form builder for this form.
  2. Add an HTML field.
  3. Add the value {{ form.name }} and any other formatting to the HTML block field.
  4. Check off the Allow Twig checkbox.
    • If you don't see this option, be sure to upgrade to the latest version of Freeform and go to the Form Builder Settings area (Freeform -> Settings -> Form Builder Settings) and enable the Allow Twig to be Enabled for each HTML block? toggle.
  5. Create a hidden field (for purpose of this example, let's give it a handle of showTitleField).
  6. Go to the Conditional Rules tab in the form builder and add some logic for this. Something like: Show the HTML field only when showTitleField contains a value of show.
  7. Save the form.
  8. In your templates, where you call the form, add the overrideValues parameter and add a line for the hidden field. Something like:
    overrideValues: {
       showTitleField: "show"
    }
    
8
  • Wow. That's, uhh, quite a workaround! If I'm understanding it correctly, that's shifting the onus of rendering the form name from the formatting template itself, on to an HTML field inside the form. While quite imaginatave, this isn't practical for the use-case I have as the title and the form fields are somewhat separated, HTML-wise. Also, it's a complex-enough solution that I might as well just clone the formatting template, minus the code for the form title. :-/ Might I submit the ability to pass Twig variables to formatting templates as a feature request? ;)
    – Sandwich
    Aug 24, 2021 at 10:24
  • The other option you have would be rather simple... see next answer...
    – Solspace
    Aug 24, 2021 at 15:22
  • 1
    @Sandwich I have updated the answer to include some other options.
    – Solspace
    Aug 24, 2021 at 15:29
  • 1
    Sure, I'll make a note about that for FR list - thanks! :)
    – Solspace
    Aug 24, 2021 at 17:28
  • 1
    Sorry @Sandwich, I can see it isn't working correctly either. We'll look into it and get a fix out tomorrow. :)
    – Solspace
    Dec 22, 2021 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.