Skip to content
● Meet Grav Guard: keep every Grav site you run secure and up to date, all from one place. Free for up to 3 sites. Read more →

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 · 737 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 Anna, 2 days ago
2 53 16 hours ago
Forms & Blueprints · by Anna, 3 days ago
7 82 16 hours ago
Forms & Blueprints · by Anna, 1 week ago
5 186 6 days ago
Forms & Blueprints · by TomW, 1 month ago
3 379 1 month ago
Forms & Blueprints · by Japhy, 2 months ago
2 595 2 months ago