- 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 andPHP .Usage
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).Here are a few examples of eRuby usage:
One Line of Ruby
<% ruby code %>Output like hello from
<% print "hello" %>
would replace the ruby code between the delimiters.Alternatively, lines starting with a
%
sign are interpreted as Ruby as well:% ruby codeMultiple LinesThese can appear less graceful because the beginning and ending are not quite the same. They function like blocks in Ruby and are terminated by
<% end %>
. They are commonly used as looping constructs, which appear like this:- <% 3.times do %>
- list item
<% end %>
Outputting:
- list item
- list item
- list item
If one comes from other languages such as PHP or Perl, the structure may not seem intuitive, but is often very concise and readable (if one knows Ruby).
The same code could also be written as:
- % 3.times do
- list item % end
Expression Result Substitution
- Value evaluated from expression like 11 from<%= ruby expression %>7 + 4
would replace the ruby expression between the delimiters. Often these are only one line.Comments
- this is the same as a comment in Ruby. All Ruby code after the # is ignored and generates nothing.<%# ruby code %>Other things common in eRuby are simply common in Ruby, such as string substitution with
which is similar templating languages in Perl or PHP. The main difference could be described as "string_name" in Ruby is a string object, and not a string variable. In practice it works almost the same.#{string_name}Implementations
There are several implementations of eRuby:
eruby
eruby is an implementation of eRuby written in the
C programming language .erb
erb is an implementation of eRuby, written purely in the Ruby programming language.
erubis
erubis is an implementation of eRuby, implemented in ruby and also in java. It runs faster than eruby and erb, and has several useful options, including alternate tags allowing for valid xml.
ee also
*
mod_ruby
*Haml
*RDoc
*Markaby External links
* [http://www.modruby.net/en/index.rbx/eruby/download.html eruby source Download] (from [http://www.modruby.net www.modruby.net] )
* [http://ruby-doc.org/docs/ProgrammingRuby/html/web.html "Ruby and the web"] , a chapter from [http://ruby-doc.org/docs/ProgrammingRuby/ "The Pragmatic Programmer's Guide"]
* [http://www.hiveminds.co.uk/node/3094 eRuby: How to Get Started with Ruby Web Programming]
* [http://www.hiveminds.co.uk/node/3098 eRuby: Using Ruby DBI for Database Connectivity]
* [http://www.hiveminds.co.uk/node/3100 eRuby: Getting Started with Ruby on Windows IIS]
* [http://www.hiveminds.co.uk/node/3096 eRuby: Using Ruby and MySQL for dynamic web pages]
* [http://www.hiveminds.co.uk/node/3093 eRuby: Using Embedded Ruby to Create Web Pages]
Wikimedia Foundation. 2010.