Render an ERB template with values from a hash

RubyErbTemplate Engine

Ruby Problem Overview


I must be overlooking something very simple here but I can't seem to figure out how to render a simple ERB template with values from a hash-map.

I am relatively new to ruby, coming from python. I have an ERB template (not HTML), which I need rendered with context that's to be taken from a hash-map, which I receive from an external source.

However, the documentation of ERB, states that the ERB.result method takes a binding. I learnt that they are something that hold the variable contexts in ruby (something like locals() and globals() in python, I presume?). But, I don't know how I can build a binding object out of my hash-map.

A little (a lot, actually) googling gave me this: http://refactormycode.com/codes/281-given-a-hash-of-variables-render-an-erb-template, which uses some ruby metaprogramming magic that escapes me.

So, isn't there a simple solution to this problem? Or is there a better templating engine (not tied to HTML) better suited for this? (I only chose ERB because its in the stdlib).

Ruby Solutions


Solution 1 - Ruby

require 'erb'
require 'ostruct'

def render(template, vars)
  ERB.new(template).result(OpenStruct.new(vars).instance_eval { binding })
end

e.g

render("Hey, <%= first_name %> <%= last_name %>", first_name: "James", last_name: "Moriarty")
# => "Hey, James Moriarty" 

Update:

A simple example without ERB:

def render(template, vars)
  eval template, OpenStruct.new(vars).instance_eval { binding }
end

e.g.

render '"Hey, #{first_name} #{last_name}"', first_name: "James", last_name: "Moriarty"
# => "Hey, James Moriarty

Update 2: checkout @adam-spiers comment below.

Solution 2 - Ruby

I don't know if this qualifies as "more elegant" or not:

require 'erb'
require 'ostruct'

class ErbalT < OpenStruct
  def render(template)
    ERB.new(template).result(binding)
  end
end

et = ErbalT.new({ :first => 'Mislav', 'last' => 'Marohnic' })
puts et.render('Name: <%= first %> <%= last %>')

Or from a class method:

class ErbalT < OpenStruct
  def self.render_from_hash(t, h)
    ErbalT.new(h).render(t)
  end

  def render(template)
    ERB.new(template).result(binding)
  end
end

template = 'Name: <%= first %> <%= last %>'
vars = { :first => 'Mislav', 'last' => 'Marohnic' }
puts ErbalT::render_from_hash(template, vars)

(ErbalT has Erb, T for template, and sounds like "herbal tea". Naming things is hard.)

Solution 3 - Ruby

Ruby 2.5 has ERB#result_with_hash which provides this functionality:

$ ruby -rerb -e 'p ERB.new("Hi <%= name %>").result_with_hash(name: "Tom")'
"Hi Tom"

Solution 4 - Ruby

If you can use Erubis you have this functionality already:

irb(main):001:0> require 'erubis'
#=> true
irb(main):002:0> locals = { first:'Gavin', last:'Kistner' }
#=> {:first=>"Gavin", :last=>"Kistner"}
irb(main):003:0> Erubis::Eruby.new("I am <%=first%> <%=last%>").result(locals)
#=> "I am Gavin Kistner"

Solution 5 - Ruby

The tricky part here is not to pollute binding with redundant local variables (like in top-rated answers):

require 'erb'

class TemplateRenderer
  def self.empty_binding
    binding
  end

  def self.render(template_content, locals = {})
    b = empty_binding
    locals.each { |k, v| b.local_variable_set(k, v) }

    # puts b.local_variable_defined?(:template_content) #=> false

    ERB.new(template_content).result(b)
  end
end

# use it
TemplateRenderer.render('<%= x %> <%= y %>', x: 1, y: 2) #=> "1 2"

# use it 2: read template from file
TemplateRenderer.render(File.read('my_template.erb'), x: 1, y: 2)

Solution 6 - Ruby

Simple solution using Binding:

b = binding
b.local_variable_set(:a, 'a')
b.local_variable_set(:b, 'b')
ERB.new(template).result(b)

Solution 7 - Ruby

If you want to do things very simply, you can always just use explicit hash lookups inside the ERB template. Say you use "binding" to pass a hash variable called "hash" into the template, it would look like this:

<%= hash["key"] %>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionsharat87View Question on Stackoverflow
Solution 1 - RubyMoriartyView Answer on Stackoverflow
Solution 2 - RubyDave NewtonView Answer on Stackoverflow
Solution 3 - RubyTom CopelandView Answer on Stackoverflow
Solution 4 - RubyPhrogzView Answer on Stackoverflow
Solution 5 - RubyLev LukomskyView Answer on Stackoverflow
Solution 6 - RubyasferView Answer on Stackoverflow
Solution 7 - RubyAlex DView Answer on Stackoverflow