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

Choose from YAML arrays by changing single YAML variable

Started by Alice 7 years ago · 2 replies · 670 views
7 years ago

Given some example yaml frontmatter:

YAML
choice: this
this:
  color: blue
that:
  color: red

I can place {{ this.color }} resulting in "blue" within a template, but would like to access the color by using something akin to {{ choice.color }}. This way, I can change one item (choice) and have that single change feed a different array of name/values.

7 years ago

Hello there Alice! I like your avatar ;-)

Well, you could just leave out the choice bit and simply write {{ this.color }} or {{ that.color }}, depending on your choice. But I think you probably have a reason for putting it in – maybe you can explain a little more what you are trying to do? There may well be another solution.

7 years ago

Hi Netzhexe and thanks!

I'm really trying to get much more swapped out than the example given, and just realized 'frontmatter' may have been the wrong word choice for a config file. The this and that arrays would have much more included. I would like to have a config file user/config/data-sets.yaml or similar with sets of data that may be switched out site-wide with one variable change pointing toward an array.

This way, I could have {{ config:data-set:choice:color }} and {{ config:data-set:choice:address }} and {{ config:data-set:choice:item-number }} on many pages throughout the site then switch them all out by changing one variable in the config file.

I've tried a few things that sounded promising from reading the documentation for Grav and TWIG such as {{ config:data-set."#{config:data-set:choice}".color }} and even {% set params = config:data-set:choice %} then {{ config:data-set:params:color }}.

Essentially, I'd like to know if there is some existing functionality that enables a set of shortcodes used site-wide to switch between YAML data sets in Grav/TWIG.

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 85 13 hours ago
General · by pamtbaau, 18 hours ago
1 60 18 hours ago
General · by Andy Miller, 1 day ago
0 47 1 day ago
General · by Marcel, 12 months ago
6 350 5 days ago
General · by Duc , 6 days ago
3 44 5 days ago