Use YAML with variables

Are variables possible in YAML files? 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?

+56
variables ruby ruby-on-rails yaml string-interpolation
Nov 11 '10 at 1:50
source share
3 answers

I had the same question, and after many studies, it looks like it's not possible .

The answer from cgat is on the right track, but you cannot actually concatenate such links.

Here is what you can do with the “variables” in YAML (which are officially called “node anchors” when you set them and “links” when you use them later):

Define the 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 information, see this section of the YAML wiki page: http://en.wikipedia.org/wiki/YAML#References

Define an object and use it with changes 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 large demo here: https://gist.github.com/bowsers senior / 979804

+81
Sep 18 '13 at 16:03
source share

After some searching, I found a cleaner solution using 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 result:

 This is the foobar var : Hello World ! 



As pointed out in a comment by @jschorr, you can also add several variables to a value in a 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 } 

Exit:

 The foo var is FOO and the bar var is BAR ! 
+34
Dec 09 '13 at 10:05
source share

This is an old post, but I had a similar need, and this is the solution I came across. This is a bit hacky, but it works and can be improved.

 require 'erb' require 'yaml' doc = <<-EOF theme: name: default css_path: compiled/themes/<%= data['theme']['name'] %> layout_path: themes/<%= data['theme']['name'] %> image_path: <%= data['theme']['css_path'] %>/images recursive_path: <%= data['theme']['image_path'] %>/plus/one/more EOF data = YAML::load("---" + doc) template = ERB.new(data.to_yaml); str = template.result(binding) while /<%=.*%>/.match(str) != nil str = ERB.new(str).result(binding) end puts str 

The big disadvantage is that it builds a variable name (in this case, “data”) in the yaml document, which may or may not exist. Perhaps the best solution would be to use $ and then substitute it with the variable name in Ruby before ERB. In addition, it is simply tested with hashes2ostruct , which allows you to enter an entry like data.theme.name, which is much easier before our eyes. All that is required is to wrap YAML :: load with this

 data = hashes2ostruct(YAML::load("---" + doc)) 

Then your YAML document might look like this

 doc = <<-EOF theme: name: default css_path: compiled/themes/<%= data.theme.name %> layout_path: themes/<%= data.theme.name %> image_path: <%= data.theme.css_path %>/images recursive_path: <%= data.theme.image_path %>/plus/one/more EOF 
+3
Aug 29 2018-11-21T00:
source share



All Articles