Log in
updated 11:29 AM UTC, May 4, 2016

Simple example to explain Ruby's ERB templating system

  • Written by Madhu V Rao
  • Published in Ruby

ERB is an abbreviation for Embedded Ruby

ERB is a feature of Ruby that enables user to dynamically generate any kind of text from templates. The templates combine plain text with Ruby code for variable substitution and flow control, which makes them easy to write and maintain.

ERB is commonly used in Rails platform to generate the webpages, It is also used in generating XML files, Source code, etc.

Here I will be explaining a simple program which generate an HTML file from a template using a data file.

The below Ruby program produces an HTML file which shows the list of languages of India in a Table format. The input to the Ruby program is a template file (.erb) and a data.xml which has list of languages.



require 'rubygems'
require 'nokogiri'
require 'erb'

class LanguagesHTMLCreator
  attr_accessor :list_of_langauges

  def initialize
    @list_of_langauges = []

  def create_language_list
    doc = Nokogiri::XML(open("data.xml"))

    count = 1
    doc.xpath('//languages-list/*').each  do |node|
      if node.name.eql?("language") 
          node_name = "#{node.name}[#{count}]"
          puts node_name
          lang_name = doc.xpath("//languages-list/#{node_name}/name").first.content
          lang_users = doc.xpath("//languages-list/#{node_name}/users").first.content
          lang_family = doc.xpath("//languages-list/#{node_name}/family").first.content
          lang_region = doc.xpath("//languages-list/#{node_name}/region").first.content

          puts lang_name, lang_family, lang_users, lang_region
          @list_of_langauges << Language.new(lang_name, lang_family, lang_users, lang_region)

   def convert_erb_html_file
       template_file = File.open("converted.htm.erb", 'r').read
       erb = ERB.new(template_file)
       File.open("converted.htm", 'w+') { |file| file.write(erb.result(binding)) }


class Language
  attr_reader :name, :family, :users, :region
  def initialize(name, family, users, region)
    @name = name
    @family = family
    @users = users
    @region = region


creator = LanguagesHTMLCreator.new

Input files:

  1. Template file (converted.htm.erb)
	<h1>Indian Languages details</h1>
	<table border="1">
			<th>No. of speakers</th>
		 <% @list_of_langauges.each do |item| %>
			<td><%= item.name %></td>
			<td><%=item.family %></td>
			<td><%=item.users %></td>
			<td><%=item.region %></td>
		</tr><% end %>


    2. data.xml file which contains list of languages


 save this file to your local system:

it has list of all the languages in the format:


Code explanation:

Above Ruby code does 2 main things:

  1. Creates an Array of Languages "list_of_languages" by parsing through the data.xml (parsing language is done using Nokogiri, if you havenot installed it yet, follow the link : http://nokogiri.org/tutorials/installing_nokogiri.html )
  2. Uses the template file which references the "list_of_languages"variable and builds the rows of an HTML table provided in the template.

ERB looks through each line of the templating string for specific patterns. The most important patterns are

  • <% "ERB will evaluate this!" %>   in the above template , this is used for running a loop of all languages
  • <%= "ERB will evaluate and output this!" %>  this is used for evaluating and substituting the values.

In the code :


 binding is a kernel call which provides the binding - essentially the scope which code is being executed.

 Final output of the above ruby program is an html text file:



Powered by Bullraider.com
Follow Us on Twitter
Find Us on Facebook
Follow Us on Google
Follow Us on Pinterest