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.

Archive

Add a dynamic variable in title var inside mardown header

Started by Muut Archive 10 years ago · 9 replies · 1659 views
10 years ago

Hi! I would like to set the title of my page using

TXT
title: My Titlte

But I would like to use the language translation system provided my Grav: {{ "HEADER.MAIN_TEXT"|t }}

10 years ago

Sorry, didn't finish my post - So is it possible to add dynamic variables inside the title property? Thank you!

10 years ago

Something like:

TXT
title:  {{ “HEADER.MYTITLE”|t }

Thanks

10 years ago

Hi @sebastienaxinte,

usually page titles are unique (site-wide) and thus it makes no sense to use the Twig translation filter. Usually, what you do is to create one page for each language. The docs tell you more about it.

However Grav has an option, called frontmatter.process_twig, which pre-renders certain frontmatter fields (https://github.com/getgrav/grav/blob/develop/system/config/system.yaml#L62-L64). If you turn this on, you above approach may work or may be not, because the processing may happen before the translation filter t is not loaded yet and only basic Twig variables like page are accessable.

10 years ago

Ok thanks, I will try but unfortunately I can't use hard coded titles because I'm using a translation service for that, so I will try your option.

9 years ago

Did you/someone else find a solution?

9 years ago

My suggestion is that you use a translation file in your theme folder.
user/themes/your_theme/languages.yaml

YAML
en:
  SITE:
    TITLE: Your title
    DESCRIPTION: 
    COPYRIGHT: © Copyright %s Your Company
  SUBSCRIBE: Subscribe
:
  SITE:
    TITLE: .....
    DESCRIPTION: ...
    COPYRIGHT: ...
  SUBSCRIBE: ...
nb:
  SITE:
    TITLE: ....
    DESCRIPTION: ...
    COPYRIGHT: ...
  SUBSCRIBE: ...

And use this in twig:

TWIG

{{ 'SITE.TITLE'|t }}
---
9 years ago

The evaluate_twig() Twig function should work. E.g. evaluate_twig(page.title)

9 years ago

Correct me if I'm wrong but this doesn't really solve the issue for individual pages that have different titles for different languages? Would be handy to have all the translations in a single file for external translation services.

9 years ago

You can create a translation overrides file and put there all your titles. Then you still need to associate each page title to a string, so yo could enter

YAML
PAGE_TITLE:
  BLOG: Blog
  HOME: Home

and in your pages, use the string, e.g. PAGE_TITLE.BLOG. In the theme, use the |t filter to translate the title: {{ page.title|t }}

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1279 9 years ago
Archive · by Muut Archive, 9 years ago
2 886 9 years ago
Archive · by Muut Archive, 9 years ago
2 4015 9 years ago
Archive · by Muut Archive, 9 years ago
1 2891 9 years ago
Archive · by Muut Archive, 9 years ago
3 1075 9 years ago