Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7086363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:32:26+00:00 2026-05-28T07:32:26+00:00

I must be overlooking something very simple here but I can’t seem to figure

  • 0

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).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:32:26+00:00Added an answer on May 28, 2026 at 7:32 am

    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.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've made snippets before but I must be overlooking something really simple; I cannot
I think I'm overlooking something simple here, I can't imagine this is impossible to
This must be simple, but I can't seem to find the answer online nor
I am sure I am overlooking something very basic here, but I just don't
There has to be something utterly simple I'm overlooking here, but for the life
Ok I must be overlooking something extremely simple but I am lost. Given this
I must be overlooking something simple. I'm setting a variable from a query result
I must be overlooking something very obvious. What's wrong with this XPath expression? I
This might seem like a very simple question, but I am struggling with it.
I must be overlooking something terribly obvious. I need to execute a C program,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.