Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use YAML with variables

Are variables within YAML files possible? For example:

theme:   name: default   css_path: compiled/themes/$theme.name   layout_path: themes/$theme.name 

In this example, how can theme: name: default be used in other settings? What is the syntax?

like image 364
brewster Avatar asked Nov 11 '10 01:11

brewster


People also ask

How do I refer a variable in YAML?

YAML example¶ We can use the ampersand & character to create a named anchor, that we can then reference later on with an asterisk *. Anchor names must not contain the [, ], {, } and , characters.

What is {{ }} In YAML?

If a value after a colon starts with a “{”, YAML will think it is a dictionary, so you must quote it, like so: foo: "{{ variable }}" If your value starts with a quote the entire value must be quoted, not just part of it.

What is difference between YAML and yml?

yml has more pages than . yaml " are not arguments, they are just statistics about the popularity of project(s) that have it wrong or right (with regards to the extension of YAML files). You can try to prove that some projects are popular, just because they use a . yml extension instead of the correct .


2 Answers

I had this same question, and after a lot of research, it looks like it's not possible.

The answer from cgat is on the right track, but you can't actually concatenate references like that.

Here are things you can do with "variables" in YAML (which are officially called "node anchors" when you set them and "references" when you use them later):

Define a value and use an exact copy of it later:

default: &default_title This Post Has No Title title: *default_title 

{ or }

example_post: &example   title: My mom likes roosters   body: Seriously, she does. And I don't know when it started.   date: 8/18/2012 first_post: *example second_post:   title: whatever, etc. 

For more info, see this section of the wiki page about YAML: http://en.wikipedia.org/wiki/YAML#References

Define an object and use it with modifications later:

default: &DEFAULT   URL:          stooges.com   throw_pies?:  true     stooges:  &stooge_list     larry:  first_stooge     moe:    second_stooge     curly:  third_stooge  development:   <<: *DEFAULT   URL:      stooges.local   stooges:      shemp: fourth_stooge  test:   <<: *DEFAULT   URL:    test.stooges.qa   stooges:      <<: *stooge_list     shemp: fourth_stooge 

This is taken directly from a great demo here: https://gist.github.com/bowsersenior/979804

like image 174
benrugg Avatar answered Oct 17 '22 11:10

benrugg


After some search, I've found a cleaner solution wich use the % operator.

In your YAML file :

key : 'This is the foobar var : %{foobar}' 

In your ruby code :

require 'yaml'  file = YAML.load_file('your_file.yml')  foobar = 'Hello World !' content = file['key'] modified_content = content % { :foobar => foobar }  puts modified_content 

And the output is :

This is the foobar var : Hello World ! 

As @jschorr said in the comment, you can also add multiple variable to the value in the Yaml file :

Yaml :

key : 'The foo var is %{foo} and the bar var is %{bar} !' 

Ruby :

# ... foo = 'FOO' bar = 'BAR' # ... modified_content = content % { :foo => foo, :bar => bar } 

Output :

The foo var is FOO and the bar var is BAR ! 
like image 39
onionpsy Avatar answered Oct 17 '22 13:10

onionpsy