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

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Forms & Blueprints

How to show a defined form from PHP

Solved by Anna View solution

Started by Anna 5 years ago · 2 replies · 537 views
5 years ago

I have a plugin with some custom logic of what to display on a page, and I would like to show a form which I have defined for that page given a certain condition. Basically, how do I do this twig bit in PHP:

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

Dumping everything (grav), I can only find the form in the Page object… is it even possible?

5 years ago

@Netzhexe, Not exactly sure what you are trying to achieve, but..

You can do:

TWIG
# variable = set by plugin
{% include "forms/form.html.twig" with { form: forms(variable) } %}

or

TWIG
# variable = set by plugin
{% include "forms/form.html.twig" with { form: forms({'route': variable }) } %}
5 years ago Solution

Ah, I did not make myself clear. But I solved it by myself! :-) This is my page markdown (feels super inelegant, but I'm pressed for time and nobody has admin access apart from me):

MARKDOWN
[some text content here]
{% include "forms/form.html.twig" with { form: forms('form-vote') } %}

<div class="print">
    <h2>Your vote</h2>
{% set voted = showvotes(grav.user.username) %}
</div>
{% if voted %}
{% include "forms/form.html.twig" with { form: forms('form-conditional') } %}
{% endif %}

I simply made my showvotes function return true or false, and I can use that in the twig, so all is well! :-D

Suggested topics

Topic Participants Replies Views Activity
Forms & Blueprints · by Ton Haarmans, 5 years ago
13 1145 4 months ago
Forms & Blueprints · by Hugo Oliveira, 5 months ago
0 67 5 months ago
Forms & Blueprints · by Flachy Joe, 6 months ago
9 142 6 months ago
Forms & Blueprints · by Augustus, 7 months ago
7 118 7 months ago
Forms & Blueprints · by Julien, 7 months ago
10 136 7 months ago