Skip to content

Instantly share code, notes, and snippets.

@troykinsella
Created February 2, 2017 23:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save troykinsella/a8c1bb5634a2bb624abb848e36c483a6 to your computer and use it in GitHub Desktop.
Save troykinsella/a8c1bb5634a2bb624abb848e36c483a6 to your computer and use it in GitHub Desktop.
Evaluate an ERB template, injecting a YAML file of variables.
#!/usr/bin/env ruby
require 'erb'
require 'yaml'
class HashBinding
def initialize(hash)
@hash = hash.dup
end
def method_missing(m, *args, &block)
@hash[m.to_s]
end
def get_binding
binding
end
end
t = ERB.new(File.read(ARGV[0]))
vars = YAML.load_file(ARGV[1])
b = HashBinding.new(vars)
puts t.result(b.get_binding)
@troykinsella
Copy link
Author

troykinsella commented Feb 2, 2017

Usage: ./process-erb.rb template.erb vars.yml > out.txt

@palladius
Copy link

Doesnt work for me - can you attach two generic inputs?
For instance are you calling the var with <%= var %> or <%= @var %> ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment