Obtaining a form object for use in a twig template

I have had great difficulty getting the functionality given in the documentation on Forms to work. I have tried a number of different ways, but cannot either of two things to work. I’m sure that I’m doing something wrong, but I have run out of ideas and I can’t get sufficient debugging information to work out what might work.

So, the part in the documentation I am trying to get to work is:

There is however a more powerful method of displaying forms that can take advantage of 
the new multi-forms support. With this method you actually pass a form: parameter to the 
Twig template specifying the form you wish to display:

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

Using this method, you can choose a specific name of a form to display. You can even 
provide the name of a form defined in other pages. As long as all your form names are 
unique throughout your site, Grav will find and render the correct form!

An alternative way to display a form is to reference the page route rather than the form 
name using an array, for example:

# Contact Form
{% include "forms/form.html.twig" with { form: forms( {route:'/forms/contact'} ) } %}

This will find the first form from the page with route /forms/contact

In the website I have a large number of items, and for each item I have a default.md file and a picture of the item in a folder named for the item under the folder ‘user/pages/stock’.

I have a gallery.htm.twig template that gathers these into a collection and creates a gallery page. For each item, I would like to create a <div> containing a form, which can then be shown in a modal popup when a user clicks on an item. But the form needs to have information from the item page.

In my gallery.html.twig, I have

<div id="modal-content-{{ elemdata.id }}" style="visibility: hidden;"> {% include 'partials/commission.html.twig' %} </div>

where elemdata is header object for the page in a for loop iteration, ie.

{% for elem in page.contents %}{% set elemdata = elem.header %}

What I want in principle is for the template ‘partials/commission.html.twig’ to be ‘partials/order.html.twig’ depending on whether the item for sale or not. However, I cannot get commissions.html.twig to work. Here it is

{% include "forms/form.html.twig" with { form: forms('commissionform'), elemdata: elemdata } %}

I want to have a form that uses data from elemdata, eg.

             -   name: id
                  type: display
                  contents: {{ elemdata.id }}

If i define ‘commissionform’ in the file ‘gallery.md’ that is processed, then form data is transfered to the ‘div’ but without any information from elemdata. I have tried to turn on twig processing and to prevent page caching. This does not work.

I think that by the time gallery.html.twig is processed, the form in gallery.md has already been processed and so the data from elemdata was not available.

But I cannot find anywhere to put a definition of commissionform that will be processed when gallery.md is processed. I have created a form in user/pages/popups/commission/default.md' and used with { form: forms(’/popups/commission’) }in the gallery template, but I get zero response, not even the<form … content I get from putting in the form in thegallery.md file`. I know that the form is processed, because if there is an error in the yaml syntax for the header, I get a Failure message.

I have tried putting {{ dump(forms) }} in the gallery template, but I get null. So I don’t know if the form I have created in /popups/commission/default.md exists for the template to access, but I haven’t found the correct reference.


  1. I have made an error in referencing the form by Grav, or
  2. the gallery template is being processed before the forms, in which the elemdata is not available, or
  3. gallery comes after the forms, which have been processed.

What I need is a way of getting the unprocessed form object needed by the form template.

I also think that perhaps a bit more information is needed in the documentation on multiple forms.

Any suggestions?