Skip to content
Grav 2.0 is officially stable. Read the announcement →
General

Custom variable confusion

Started by Jim Doria 6 years ago · 2 replies · 761 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 Andy Miller, 2 days ago
0 69 2 days ago
General · by Veehem, 3 days ago
0 111 3 days ago
General · by milkboy, 4 days ago
0 112 4 days ago
General · by ian russell, 2 weeks ago
2 204 1 week ago
General · by pamtbaau, 1 month ago
2 462 2 weeks ago