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

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:


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!

When the event fires, hook into the instance and push the variable. Eg: grav-theme-scholar/scholar.php at 3.0.4 · OleVik/grav-theme-scholar · GitHub