String interpolation in YAML

In Perl, I can do something like the following:

my $home = "/home"; my $alice = "$home/alice"; 

Can I do something like the following in YAML:

 Home: /home Alice: $Home/alice 

So, is Alice effective /home/alice at the end?

+6
yaml string-interpolation template-variables
Apr 03 '13 at 2:42 on
source share
3 answers

Unfortunately, you are out of luck. To do what you want, you need to pass $home from a view file (or anywhere) and interpolate it into a yaml entry, which might look something like this:

 Alice: ! '%{home}/Alice' 

See this StackOverflow Q&A for a detailed answer to almost exactly your question.

+4
Apr 03 '13 at 5:53 on
source share

You must use the ERB pattern.

you can write as below:

 Alice: <%=home%>/alice 

When using, you need to parse the home value with ERB before parse as YAML. if home is a local variable, you need to pass the local binding as an argument to the #result method. if you fail this, the TOP LEVEL binding will be used by default.

Like this:

 require 'erb' home = 'home' YAML.load(ERB.new(yaml_content).result(binding)) 
+7
Dec 09 '14 at 10:48
source share

I ended up using YAML::AppConfig , but admittedly this is not a YAML solution, but a specific Perl solution. This allows YAML to include $var , which are interpolated.

+1
Apr 04 '13 at 4:30
source share



All Articles