4

I have a contact form in my footer which will be displayed on all pages. How do I do that? The documentation thinks that I always have one specific page responsible for the form. The form currently looks like this in twig:

        <form method='POST'>
            <label for='name'>{{ "o.footer.form.name"|t }}</label>
            <input type='text' id='name' name='name' required='required'/>
            <label for='email'>{{ "o.footer.form.email"|t }}</label>
            <input type='text' id='email' name='email'/>
            <label for='phone'>{{ "o.footer.form.phone"|t }}</label>
            <input type='tel' id='phone' name='phone'/>
            <label for='message'>{{ "o.footer.form.message"|t }}</label>
            <textarea id='message' name='message'></textarea>
            <input type='submit' value="{{ "o.footer.form.submit"|t }}"/>
        </form>
Joe Eifert
  • 1,306
  • 14
  • 29

1 Answers1

4

You can do that since the latest release of Form plugin.

Just create a page with a form defined and then call it from other page, example:

{% include "forms/form.html.twig" with {form: forms( {route: '/newsletter-signup'} ) } %}

More information: https://learn.getgrav.org/forms/forms#displaying-forms-in-page-content

Alex Ljamin
  • 737
  • 8
  • 31
Paul Massendari
  • 417
  • 3
  • 6
  • Thank you! The page should/could be named `form/default.md` and instead of defining the route, you can just call it by name. – Joe Eifert Oct 03 '17 at 14:05
  • I’m confronted with something similar and thought about using the _user/config/site.yaml_ file to add a form `form-contact`. I then call this form from my base.html.twig template with `{% include 'forms/form.html.twig' with {form: site.forms['form-contact']} %}` but I wonder if that’s a bad practice. – arkhi May 30 '18 at 17:32
  • So then are what folks doing to tackle this is creating a form page that is non-visible? True you could just have the form in one arbitrary page's frontmatter, but it would be nice to just have the form somewhere unique. However, I'm not sure @arkhi 's idea is good practice. – Trevor Jul 20 '19 at 02:19