Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

A form on every page using a template

Started by Muut Archive 10 years ago · 8 replies · 517 views
10 years ago

Hello,

I have a series of pages which all end with the same form. I created the form in a separate page, and it works alone. Now I want to insert the form at the end of every page using that certain template.

For now, I added at the bottom of the template:

TWIG
{% for form in page.collection() %}
    {{ form.content }}
    {% include "forms/form.html.twig" %}
{% endfor %}

And this to my pages:

YAML
content:
    items:
        '@page.self': /path/to/form

It’s probably not the cleanest way to do it because there’s only one form to import and not a collection, and ideally I would not need to add something to the front-matter of each page.

Anyway it doesn’t work. Only the content of the form page appears, and not the form itself.

How can I manage to do this?

Thanks :)

10 years ago

Thanks for your answer. It’s actually the first thing I tried, but I couldn’t find a way to make it work on several pages.

I would like to define the form just once, and display it in several pages.

10 years ago

There is currently no way to do it with the Form plugin.
I would like to add a shortcode-based form you can embed in pages, but currently it's not there yet.

You can use a js-based form from an external service (e.g. Wufoo or others) in the meantime.

10 years ago

Thanks, okay.

Another solution for me would be to have one form only, but with a select element to choose ‘what page your message is about’. In which way can I list, say, all the subpages of one page as the options of a select element in a form?

10 years ago

Hi again,
Wouldn’t this be possible to use import@ for this? By putting the whole form into an external yaml and calling it from each frontmatter?

10 years ago

First import@ only works when defining a form. It is not actually for importing yaml data from one place to another.

Overall this problem is not that trivial to solve. I was looking at this and it's quite complicated. Currently the form data is assigned to the Twig object when viewing the current page. That's why you currently need to add the form definition to the frontmatter of the current page.

For this to work, the form data will need to be parsed and associated with the page it was defined on. After the form is rendered, it still will have to process via a dedicated page because the current page wouldn't know about the form coming from some other 'included' page.

Anyway, needless to say it's a bit complicated. It's something we want to add, but it might require a bit of a rewrite in order to support this properly.

10 years ago

Just browsing and haven't tested, but what about something like this:

TXT

Since you only have one form but want it on several pages...again, just a thought, not sure if it will work.

10 years ago

No it won't work because the process of parsing that form data is handled onPageInitialized, and that is only run for the overall page, it does not get kicked off when you simply include a a twig template of another page with a form.

This is why we need to process the form and assign the form properties to the page itself. Then it could be available whenever you call the page's content()method.

This is the heart of the problem, it can be solved, but it's quite a major rewrite that I hesitate to undertake while we're trying to finish up Grav/Admin 1.1

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1366 9 years ago
Archive · by Muut Archive, 9 years ago
2 940 9 years ago
Archive · by Muut Archive, 9 years ago
2 4069 9 years ago
Archive · by Muut Archive, 9 years ago
1 2960 9 years ago
Archive · by Muut Archive, 9 years ago
3 1124 9 years ago