eRuby
From Wikipedia, the free encyclopedia
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.
Contents |
[edit] Usage
eRuby allows Ruby code to be embedded within (delimited by) 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
If you come from other languages such as PHP or Perl, the structure may not seem intuitive, but is often very concise and readable (if you know Ruby, of course).
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 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.
[edit] Implementations
There are several implementations of eRuby:
[edit] eruby
eruby is an implementation of eRuby written in the C programming language.
[edit] erb
erb is an implementation of eRuby, written purely in the Ruby programming language.
[edit] 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.
[edit] See also
[edit] External links
- eruby source Download (from www.modruby.net)
- "Ruby and the web", a chapter from "The Pragmatic Programmer's Guide"
- eRuby: How to Get Started with Ruby Web Programming
- eRuby: Using Ruby DBI for Database Connectivity
- eRuby: Getting Started with Ruby on Windows IIS
- eRuby: Using Ruby and MySQL for dynamic web pages
- eRuby: Using Embedded Ruby to Create Web Pages
|