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.

General

Putting twig into a form

Started by Richard Hainsworth 6 years ago · 2 replies · 507 views
6 years ago

I wanted to display some text on a form including the date.
In the fields section, I put

YAML
        - name: date
          label: 'Today is {{ "now"|date("d M Y") }}'
          type: display
          content: ''

But this was not transformed by Twig.

However, in other parts of a form, I do get a Twig transform. Eg. the following works (I get a file saved including the date.)

TWIG
    process:
        - save:
            fileprefix: feedback-
            dateformat: Ymd-His-u
            extension: txt
            body: >
               {{ "now"|date("d M Y") }}
               {% include 'forms/data.txt.twig' %}
               A whole load more of string

I realise that the save process goes through another Twig transformation.

I tried to set the site variable 'Process frontmatter Twig' to true, but I got an error originating out of page.php. Unfortunately, I could not get enough debug info to work where the error was, so I turned off this site variable.

I noted that the display field has a markdown option, so I tried twig: true as an option for display but this did not work.

Perhaps a twig: true option could be added to the display field, thus allowing twig processing on the content data?

6 years ago

By using the Form Prefiller Plugin you can set a frontmatter variable like this:

TWIG
today_date_label: 'Today is {{ now|date(''M d Y'') }}'

(note there are no double quotes!) and change the form field into:

YAML
-
    name: date
    data-label@:
        - '\Grav\Plugin\FormPrefillerPlugin::getFrontmatter'
        - today_date_label
    type: display

The only requirement is to turn ‘Process frontmatter Twig’ On in the Admin Plugin or set it to true in system.yaml:

YAML
pages:
    frontmatter:
        process_twig: true

So I hope you can find your problem with processing frontmatter Twig in your site.

6 years ago

Hi Ron (@bleutzinn )

Thanks for the response.

a) I have not looked at that plugin.
b) I tried to set site.frontmatter.process_twig to true, and the whole site bombed! I did not want to debug the whole site to find where the problem was.

The problem with a site setting is that mostly I don't want twig processing for all pages, only some.

I think it would be useful to have a mechanism in admin for specifying which pages have twig processing, or put it on the page itself. There is a mechanism for page specific processing switches that is used for security - some pages are not visible unless you have logged in and the user has a certain level of security.

c) The form plugin allows for twig processing of certain fields, but not all of them. It happened that the field/attribute I wanted weren't processed. viz., content in type: display.

However, I was looking at the code for Form, and I have discovered a number of fields that are not documented, but may serve my use case without any other change (which may be why the Grav developers haven't changed Form because they know work arounds ??? )

Thanks for the help.

Richard

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 95 14 hours ago
General · by pamtbaau, 20 hours ago
1 61 19 hours ago
General · by Andy Miller, 1 day ago
0 47 1 day ago
General · by Marcel, 12 months ago
6 356 5 days ago
General · by Duc , 6 days ago
3 44 6 days ago