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 = []
  end

  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)
          count+=1
        end
      end
    end

   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)) }
   end

end

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

end

creator = LanguagesHTMLCreator.new
creator.create_language_list
creator.convert_erb_html_file

Input files:

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

 

    2. data.xml file which contains list of languages

www.ictforu.com/xml/data.xml

 save this file to your local system:

it has list of all the languages in the format:

	<language>
		<name>Kannada</name>
		<region>Karnataka</region>
		<users>38M</users>
		<family>Dravidian</family>
	</language>

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 :

 erb.result(binding)

 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:

http://ictforu.com/data/converted.html

 


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