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.

Support

How to use dynamic title in frontmatter

form

Solved by pamtbaau View solution

Started by Marco 5 years ago · 4 replies · 670 views
5 years ago

Hi! I found various questions and answers on the matter, however, none of them seems to work.

Here's my scenario: a form with a /thankyou/formdata.md embedded in other page(s).

YAML
---
title: '{{ CONTACTFORM.SENT_MESSAGE_PAGE_TITLE }}'
cache_enable: false
process:
    twig: true
---

## {{ 'CONTACTFORM.SENT_MESSAGE_PAGE_TEXT'|t }}

Body gets translated, however title is not, regardless of the syntax used (when not throwing error).

Any advice?

Thanks!

5 years ago

@phmg701,

I found various questions and answers on the matter, however, none of them seems to work.

Before I repeat what others have done already...

  • Which questions and answers have you found?
  • For each: What doesn't work, or doesn't meet your needs?

I guess using multiple formdata.<lang>.md has already been suggested?

5 years ago

@pamtbaau you're right, sorry!
Various combinations of {{ "STRING" }} with or without |t filter, quotes, double quotes. I didn't find a working combination for the frontmatter, but I could be wrong on interpreting syntax!
Yes, <lang>.md could be a solution, yet title: wouldn't be a string of languages.yaml which is my goal as form.md is language-agnostic and fully translatable (page has no title as it is a non published page, I then embed the form elsewhere, very powerful Grav feature).
Thanks!

5 years ago Solution

@phmg701, Did some playing and it seems the following is working:

  • Fresh install of Grav 1.17.24
  • Updated /user/config/system.yaml:

    YAML
    languages:
    supported: [en, de]
    
    pages:
    frontmatter:
      process_twig: true     # Should the frontmatter be processed to replace Twig variables?
    
  • Created Contact form in two languages + single thankyou page:

    TXT
    user/pages
    ├── 01.home
    │   └── default.md
    ├── 02.typography
    │   └── default.md
    └── 03.contact
      ├── form.de.md
      ├── form.en.md
      └── thankyou
          └── formdata.md
    

    03.contact/form.en.md + 03.contact/form.de.md

    YAML
    ---
    title: Contact Form
    
    form:
      name: contact
    
      fields:
          name:
            label: Name
            type: text
    
      buttons:
          submit:
            type: submit
            value: Submit
    
      process:
          message: Thank you for getting in touch!
          display: thankyou
    ---
    

    formdata.md

    TWIG
    ---
    title: "{{ 'TITLE'|t }}"
    process:
      twig: true
    ---
    
    ## {{ 'CONTENT'|t }}
    
  • Added translation to user/themes/quark/languages.yaml

    YAML
    en:
    TITLE: Title-EN
    CONTENT: Content-EN
    
    de:
    TITLE: Title-DE
    CONTENT: Content-DE
    
  • Browsed to localhost/en/contact, filled in the form and hit submit =>
    Page title is English version + content is English version
  • Browsed to localhost/de/contact, filled in the form and hit submit =>
    Page title is German version + content is German version

Hope this is what you were looking for... If not, I've had some fun anyway.

👍 2
last edited 10/06/21 by pamtbaau
5 years ago

Thank you! Works flawlessly!
I also managed to put a failsafe in the Twig template in case page title is missing, just in case...
Thanks again!

👍 1

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
2 57 15 hours ago
Support · by Anna, 3 days ago
2 65 18 hours ago
Support · by Justin Young, 19 hours ago
1 33 18 hours ago
Support · by Duc , 1 week ago
2 68 5 days ago
Support · by Colin Hume, 1 week ago
2 60 6 days ago