User:Sweekrut.joshi/sandbox
Introduction
[edit]eRuby (Embedded Ruby) 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. 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.
Usage
[edit]eRuby allows Ruby code to be embedded within a pair of <%
and %>
delimiters. These embedded code blocks are then evaluated in-place (they are replaced by the result of their evaluation). 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
[edit]<%= %> : This indicates that the tag encloses an expression. Such a tag starts with an opening tag delimiter followed by an equal to symbol and ends with an end tag delimiter. During the rendering of the template, this piece of code gets substituted with the result of the code. If the evaluated result is not a string, it gets converted to a string before it is rendered. For example:
require 'erb'
x = 50
template = ERB.new("The value of x is: <%= x %>")
puts template.result(binding)
The resulting text looks like this: The value of x is: 50[1]
Execution Tags
[edit]<% %> : Code enclosed in such tags is called as scriplet. The code in such a tag gets executed and its result gets replaced in place of the scriplet. Such tags must have a matching <% end %> tag to denote the end of a functional block. For example:
<ul>
<% 3.times do %>
<li>list item</li>
<% end %>
</ul>
In the above example, the text list item gets printed three times. The scriplet produces no text on it's own, it only makes the enclosed statement to run multiple times. The output of above code:
- list item
- list item
- list item
Comments Tags
[edit]<%# %> : Contents of comment tags don't get rendered in the output. Such tags start with an open tag delimiter followed by a hash symbol and end with an end tag delimiter. Example of a comment tag is shown below:
<%# ruby code %>
This is the same as a comment in Ruby. All Ruby code after the # is ignored and generates nothing.
Implementations
[edit]There are several embedded ruby implementations available and one can choose the one best suited for him/her. Some of the important implementations are:
- erb - erb is an implementation of eRuby written purely in the Ruby programming language and included in the Ruby standard library.
- erubis - erubis is an implementation of eRuby implemented in Ruby and also in Java. According to its home page, it runs faster than erbb and has several useful options.
- ember - It is a Linux implementation of eRuby implemented in pure ruby.
The below table compares the tags available in each of the above implementations
Implementation | Simple expression tag <%= %> | Simple execution tag <% %> | Simple comment tag <%# %> | Ability to configure tag pattern | Short hand notation for tags | <%~ %> | <%+ %> | <%< > | <%| > |
---|---|---|---|---|---|---|---|---|---|
erb | yes | yes | yes | no | yes,
<%xy%> can be written as %xy |
no | no | no | no |
erubis | yes | yes | yes | no | yes,
as one can change tag patterns |
no | no | no | no |
ember | yes | yes | yes | yes, can change tag
pattern to any thing. ex - [% %] etc |
yes, <%xy%> can be written as %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 <%+ %> but file contents are simply rendered into the output | Treats the enclosed code as first block of ruby code and appends a 'do' keyword to the body of the tag. |
![]() | This is a user sandbox of Sweekrut.joshi. You can use it for testing or practicing edits. This is not the place where you work on your assigned article for a dashboard.wikiedu.org course. Visit your Dashboard course page and follow the links for your assigned article in the My Articles section. |
- ^ Ruby Best Practices by Gregory Brown, foreword by Yukihiro “Matz” Matsumoto