ERuby
eRuby is a templating system that embeds Ruby into a text document. It is often used to embed Ruby code in an HTML document, similar to ASP, JSP and PHP and other server-side scripting languages. The templating system of eRuby combines the ruby code and the plain text to provide flow control and variable substitution, thus making it easy to maintain.
The View module of the rails is responsible to display the response or output on a browser. In its simplest form, a view can be a piece of HTML code which has some static content. For most applications, just having static content may not be enough. Many Rails applications will require dynamic content created by the controller to be displayed in their view. This is made possible by using Embedded Ruby to generate templates which can contain dynamic content. Embedded Ruby allows ruby code to be embedded in a view document. This code gets replaced with proper value resulted from the execution of the code at run time. But, by having the ability to embed code in a view document, we risk bridging the clear separation present in the MVC frame. It is thus the responsibility of the developer to make sure that there is a clear separation of responsibility among the model, view and controller modules of his/her application.
Usage
eRuby allows Ruby code to be embedded within a pair of<%
and %>
delimiters. These embedded code blocks are then evaluated in-place. Apart from creating web pages, eRuby can also be used to create XML Documents, RSS feeds and other forms of structured text files. eRuby dynamically generates static files based on templates. These functionalities of eRuby can be found in the ERB Library.Different types of tag markers used in ERB templates are:
- Expression tags
- Execution tags
- Comment tags
Expression tags
require 'erb'
x = 500
template = ERB.new
puts template.result
The resulting text looks like this: The value of x is: 500
Execution tags
- list item <% end %>
<% 4.times do %>
In the above example, the text list item gets printed four times. The scriptlet produces no text on its own, it only makes the enclosed statement to run multiple times. The output of above code:
- list item
- list item
- list item
- list item
Comments tags
<%# ruby code %>
This is the same as a comment in Ruby. All Ruby code after the # is ignored and generates nothing.
Other tags
Other things common in eRuby are simply common in Ruby, such as string substitution with#
, which is similar in languages such as Perl or PHP.Newlines in eRuby can be suppressed by adding a hyphen at the beginning of the end tag delimiter. For example:
<%2.times do -%>
<%= @name %>
<% end -%>
Implementations
There are several implementations of eRuby, namely:- ERB
- erubis
- ember
erb
A template can be generated by running a piece of code written using the object. A simple example is as shown below:
require 'erb'
x = 400
simple_template = "Value of x is: is <%= x %>."
renderer = ERB.new
puts output = renderer.result
The result looks as follows: Value of x is: 400
The same could be achieved using the below code which does not make use of an ERB object:
x = 400
string = "The value of x is: #"
puts string
require 'erb'
simple_template = "Value of x is: is <%= x %>."
x = 400
renderer = ERB.new
puts output = renderer.result
This still generates the same output. i.e., Value of x is: 400.
The second code snippet changes to the below code:
string = "The value of x is: #"
x = 400
puts string
class ERBExample
attr_accessor:variable1
# using bind to access class variables
def render
renderer.result
end
def initialize
@variable1 = variable1
end
# Expose private binding method.
def get_binding
binding
end
end
example = ERBExample.new
renderer = ERB.new
puts output = renderer.result
new() method of ERB
The new method of the ERB object takes two more parameters. The second parameter specifies a safety level. By giving a number in the second parameter one can make the template run in a different thread. The value of the number determines the safety level. At the maximum isolation level, unless the binding object is marked as trusted, ERB cannot use it. The third parameter specify optional modifiers. These can be used to control adding of newlines to the output. For example, to make sure that ERB does not output newlines after tag ends, we can create the ERB object as shown belowrenderer = ERB.new
To only provide the third parameter and ignore the second parameter, use 0 as the input for second parameter.
ERB has many other methods exposed which can be used to render a template. For full list of APIs exposed by the ERB object, refer to the given in the reference section.
Running ERB from Command-line
As it has been already explained in the previous sections, the erb is used to generate templates. This is often used to generate web pages or other text files. Usually needs erb to push the output to his or her desired file. To achieve this, we can use the redirection ability provided in the command-line and redirect the output to a file rather than making it print on the standard output.erb sample1.erb.txt > my_view.html.erb
Linking of third party libraries is achievable by making use of the -r option and providing the name of the library. To remember this functionality, one can remember the Ruby key word , which does the same functionality as the -r option. The below example uses the library.
erb -r IPAddr sample1.txt.erb > my_view.html.erb
As we have mentioned about the safety levels in the previous section, one can specify the safety level as a command line argument using the -S option
erb -S 4 sample1.erb.txt > my_view.html.erb
erubis
is an implementation of eRuby implemented in Ruby and also in Java. According to its home page, it runs faster than eRuby and ERb and has several useful options, including alternate tags allowing for valid XML.ember
is a pure Ruby implementation of eRuby for Linux. It allows debugging of eRuby templates, improves their composability, and provides powerful shorthand eRuby directives.Different implementation tags comparison
The below table compares the tags available in each of the above implementationsSimple expression tag | Simple execution tag | Simple comment tag | Ability to configure tag pattern | Short hand notation for tags | |||||
, %xy | |||||||||
, can change tag pattern to any thing. ex - etc | , as one can change tag patterns | ||||||||
, %xy | The content of the tag is evaluated as an eRuby template. | The content of the tag is evaluate as a ruby code and is expected to be a path pointing to an Ruby template file which is read, evaluated and rendered. | Same as | Treats the enclosed code as first block of ruby code and appends a do keyword to the body of the tag. |