How to create new binding and assign instance variables to it for availability in ERB?
问题 I'm implementing HTML templating in a Ruby project (non-rails). To do this I'll be using ERB, but I have some concerns about the binding stuff. First out, this is the method I got so far: def self.template(template, data) template = File.read("#{ENV.root}/app/templates/#{template}.html.erb") template_binding = binding.clone data.each do |k, v| template_binding.local_variable_set(k, v) end ERB.new(template).result(template_binding) end To call it I'll just do Email.template('email/hello', {