Putting twig into a form

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

        - 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.)

    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?

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

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

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

-
    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:

pages:
    frontmatter:
        process_twig: true

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

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