eRuby

From Wikipedia, the free encyclopedia
  (Redirected from Eruby)
Jump to: navigation, search
eRuby (Embedded Ruby)
Stable release 1.0.5 / December 14, 2004 (2004-12-14)
Development status Active
Written in Ruby
Operating system Cross-platform
Type Template engine
License GPL and LGPL

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.

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).

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 code

Multiple lines

These 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:

<ul>
<% 3.times do %>
 
  <li>list item</li>
 
<% end %>
</ul>

Outputting:

  • list item
  • list item
  • list item

The same code could also be written as:

<ul>
% 3.times do
  <li>list item</li>
% end
</ul>

Expression result substitution

<%= ruby expression %>

- Value evaluated from expression like 11 from 7 + 4 would replace the ruby expression between the delimiters. Often these are only one line.

Comments

<%# ruby code %>

- this is the same as a comment in Ruby. All Ruby code after the # is ignored and generates nothing. Other things common in eRuby are simply common in Ruby, such as string substitution with

#{string_name}

, which is similar in languages such as Perl or PHP.

Implementations[edit]

There are several implementations of eRuby:

eruby[edit]

eruby is an implementation of eRuby written in the C programming language.

erb[edit]

erb is an implementation of eRuby written purely in the Ruby programming language and included in the Ruby standard library.

erubis[edit]

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.[1]

ember[edit]

ember is a pure Ruby implementation of eRuby. It allows debugging of eRuby templates, improves their composability, and provides powerful shorthand eRuby directives.

See also[edit]

References[edit]

External links[edit]