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
Ben Aug 29 2018-11-21T00: 00Z
source share