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

Add a site-wide variable to twig based on a config setting

Solved by Ole Vik View solution

Started by Anna 4 years ago · 1 replies · 435 views
4 years ago

Hey everybody,

I would like to add a global variable to my twig templates based on a config setting. I'm aware that I could just check the config setting in each template, but I would like a solution that's easier to use and maintain.

I found this bit in the Event Hooks page in the docs:

onTwigSiteVariables

Where Twig processes the full site template hierarchy. This is where you should add any variables to Twig that need to be available to Twig during this process.

Sounds like this is what I want, and I have put a function in my theme.php file – but how do I actually add a variable to twig here?

Thanks for your ideas and time!

Suggested topics

Topic Participants Replies Views Activity
Forms & Blueprints · by Ton Haarmans, 5 years ago
13 1135 4 months ago
Forms & Blueprints · by Hugo Oliveira, 5 months ago
0 61 5 months ago
Forms & Blueprints · by Flachy Joe, 6 months ago
9 134 6 months ago
Forms & Blueprints · by Augustus, 7 months ago
7 108 7 months ago
Forms & Blueprints · by Julien, 7 months ago
10 128 7 months ago