Skip to content
Grav 2.1 is out: every page speaks Markdown. Read the announcement →

Custom variable confusion

Started by Jim Doria 6 years ago · 2 replies · 848 views
6 years ago

I'm trying to follow the docs on setting up custom variables for use in my theme's Twig templates. It seems pretty straightforward:

You can easily add custom variables in a variety of ways. If the variable is a site-wide variable, you can put the variable in your user/config/site.yaml file and then access it via: {{ site.my_variable }}

Going into user/config/site.yaml I add:
bright-color: ffff00
at the end of the file.

Then in my Twig template:
<div style="background-color: #{{ site.bright-color }};"

When the page displays, however, I look at the source and see:
<div style="background-color: #0;"

So clearly the variable is not getting evaluated correctly and put into the template.

I've also tried following the instructions here and put my variable definitions in user/config/themes/themename.yaml and tried accessing them via {{ grav.theme.config.bright-color }} and {{ config.theme.bright-color }}

None of this has worked. Is there something I'm overlooking, or is my syntax just broken somehow?

6 years ago

Hello @jimdoria

I think the - is not allowed in the header formatter for variables. Try bright_color instead of bright-color and it should work.

6 years ago

That did the trick! I remembered reading about that limitation in the docs somewhere, but didn't know to apply it here.

Thanks so much!

Suggested topics

Topic Participants Replies Views Activity
General · by jeremycherfas, 4 days ago
3 95 2 hours ago
General · by Old Man Umby, 2 days ago
1 60 2 days ago
General · by Sebastian, 4 days ago
1 81 4 days ago
General · by Andy Miller, 2 weeks ago
0 216 2 weeks ago
General · by ready-4-IT, 6 months ago
1 716 3 weeks ago