Skip to content
● Grav 2.2 is out: 2x faster cold starts and 96% less memory on big sites. Read the announcement →

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 · 643 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 Japhy, 7 days ago
0 167 7 days ago
Forms & Blueprints · by FrViPofm, 3 weeks ago
5 309 3 weeks ago
Forms & Blueprints · by Ton Haarmans, 6 years ago
13 2296 8 months ago
Forms & Blueprints · by Hugo Oliveira, 9 months ago
0 759 9 months ago
Forms & Blueprints · by Flachy Joe, 10 months ago
9 844 10 months ago