Class/Module Index

ERB

ERB provides an easy to use but powerful templating
system for Ruby. Using ERB, actual Ruby code can be
added to any plain text document for the purposes of generating document
information details and/or flow control.

ERB recognizes certain tags in the provided template
and converts them based on the rules below:

<% Ruby code -- inline with output %>
<%= Ruby expression -- replace with result %>
<%# comment -- ignored -- useful in testing %>
% a line of Ruby code -- treated as <% line %> (optional -- see ERB.new)
%% replaced with % if first thing on a line and % processing is used
<%% or %%> -- replace with <% or %> respectively

ERB (or Ruby code generated by ERB) returns a string in the same character encoding as
the input string. When the input string has a magic comment, however, it
returns a string in the encoding specified by the magic comment.

ERB is useful for any generic templating situation.
Note that in this example, we use the convenient “% at start of line” tag,
and we quote the template literally with %q{...} to avoid
trouble with the backslash.

From: James Edward Gray II <james@grayproductions.net>
To: Community Spokesman <spokesman@ruby_community.org>
Subject: Addressing Needs
Community:
Just wanted to send a quick note assuring that your needs are being addressed.
I want you to know that my team will keep working on the issues, especially:
* Run Ruby Quiz
* Document Modules
* Answer Questions on Ruby Talk
Thanks for your patience.
James Edward Gray II

ERB is often used in .rhtml files (HTML
with embedded Ruby). Notice the need in this example to provide a special
binding when the template is run, so that the instance variables in the
Product object can be resolved.

Public Class Methods

An ERB object works by building a chunk of Ruby code
that will output the completed template when run. If safe_level is
set to a non-nil value, ERB code will be run in a
separate thread with $SAFE set to the provided level.

If trim_mode is passed a String containing one or more of the
following modifiers, ERB will adjust its code
generation as listed:

eoutvar can be used to set the name of the variable ERB will build up its output in. This is useful when
you need to run multiple ERB templates through the
same binding and/or when you want to control where output ends up. Pass
the name of the variable to be used inside a String.