Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 "Alice" is effectively /home/alice in the end?

like image 761
Clinton Avatar asked Apr 03 '13 02:04

Clinton


2 Answers

Unfortunately, you're out of luck. To do what you want you'd need to pass in $home from a view file (or wherever) and interpolate it in your yaml entry, which could possibly look something like:

Alice: ! '%{home}/Alice' 

See this StackOverflow Q&A for the detailed answer to pretty much exactly your question.

like image 52
Paul Fioravanti Avatar answered Dec 12 '22 23:12

Paul Fioravanti


You should use ERB template.

you can write like following:

Alice: <%=home%>/alice

When use, you need parse home value with ERB before parse as YAML. if home is local variable, you need pass local binding in as #result method's argument. if you not pass this, will use TOP LEVEL binding as default.

Like this:

require 'erb'

home = 'home'
YAML.load(ERB.new(yaml_content).result(binding))
like image 25
zw963 Avatar answered Dec 12 '22 23:12

zw963