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

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

Custom variable confusion

Started by Jim Doria 6 years ago · 2 replies · 652 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 Jerry Hunt, 4 days ago
2 82 12 hours ago
General · by pamtbaau, 17 hours ago
1 55 17 hours ago
General · by Andy Miller, 1 day ago
0 45 1 day ago
General · by Marcel, 12 months ago
6 348 5 days ago
General · by Duc , 6 days ago
3 43 5 days ago