My yml file looks like:
defaults: &defaults
key1: value1
key2: value2
..
..
My template files have the following:
<%= key1 %>
<%= key2 %>
So my script has a list of files, loops through them and I want to pass the yml object to my erb for parsing:
config = YAML::load(File.open('config.yml'))ENV['ENV']
file_names.each do |fn|
file = File.new "#{fn}", "r"
template = ERB.new file
result = template.result
# save file here
end
How do I pass my config object to the erb templating system?
With help from http://ciaranm.wordpress.com/2009/03/31/feeding-erb-useful-variables-a-horrible-hack-involving-bindings/
Not super pretty, but if you hid the class away then it’s not so bad. Downside is you’ll probably run into problems calling other methods that don’t exist in the ThingsForERB class so you’ll want to think about that before just changing things over to use
config['key1']as Sergio suggested.When run the output is: