# = ERB -- Ruby Templating
# Author:: Masatoshi SEKI
# Documentation:: James Edward Gray II and Gavin Sinclair
# See ERB for primary documentation and ERB::Util for a couple of utility
# Copyright (c) 1999-2000,2002,2003 Masatoshi SEKI
# You can redistribute it and/or modify it under the same terms as Ruby.
# = ERB -- Ruby Templating
# 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.
# A very simple example is this:
# template = ERB.new <<-EOF
# The value of x is: <%= x %>
# puts template.result(binding)
# <em>Prints:</em> The value of x is: 42
# More complex examples are given below.
# ERB recognizes certain tags in the provided template and converts them based
# <% 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
# All other text is passed through ERB filtering unchanged.
# There are several settings you can change when you use ERB:
# * the nature of the tags that are recognized;
# * the value of <tt>$SAFE</tt> under which the template is run;
# * the binding used to resolve local variables in the template.
# See the ERB.new and ERB#result methods for more detail.
# 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
# <tt>%q{...}</tt> to avoid trouble with the backslash.
# From: James Edward Gray II <james@grayproductions.net>
# Subject: Addressing Needs
# Just wanted to send a quick note assuring that your needs are being
# I want you to know that my team will keep working on the issues,
# <%# ignore numerous minor requests -- focus on priorities %>
# % priorities.each do |priority|
# Thanks for your patience.
# message = ERB.new(template, 0, "%<>")
# # Set up template data.
# to = "Community Spokesman <spokesman@ruby_community.org>"
# priorities = [ "Run Ruby Quiz",
# "Answer Questions on Ruby Talk" ]
# From: James Edward Gray II <james@grayproductions.net>
# To: Community Spokesman <spokesman@ruby_community.org>
# Subject: Addressing Needs
# 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:
# * Answer Questions on Ruby Talk
# Thanks for your patience.
# ERB is often used in <tt>.rhtml</tt> 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.
# # Build template data class.
# def initialize( code, name, desc, cost )
# def add_feature( feature )
# # Support templating of member data.
# <head><title>Ruby Toys -- <%= @name %></title></head>
# <h1><%= @name %> (<%= @code %>)</h1>
# <% @features.each do |f| %>
# <li><b><%= f %></b></li>
# <b>Only <%= @cost %>!!!</b>
# Call for a price, today!
# rhtml = ERB.new(template)
# # Set up template data.
# toy = Product.new( "TZ-1002",
# "Geek's Best Friend! Responds to Ruby commands...",
# toy.add_feature("Listens for verbal commands in the Ruby language!")
# toy.add_feature("Ignores Perl, Java, and all C variants.")
# toy.add_feature("Karate-Chop Action!!!")
# toy.add_feature("Matz signature on left leg.")
# toy.add_feature("Gem studded eyes... Rubies, of course!")
# rhtml.run(toy.get_binding)
# <i>Generates (some blank lines removed):</i>
# <head><title>Ruby Toys -- Rubysapien</title></head>
# <h1>Rubysapien (TZ-1002)</h1>
# <p>Geek's Best Friend! Responds to Ruby commands...</p>
# <li><b>Listens for verbal commands in the Ruby language!</b></li>
# <li><b>Ignores Perl, Java, and all C variants.</b></li>
# <li><b>Karate-Chop Action!!!</b></li>
# <li><b>Matz signature on left leg.</b></li>
# <li><b>Gem studded eyes... Rubies, of course!</b></li>
# Call for a price, today!
# There are a variety of templating solutions available in various Ruby projects:
# * ERB's big brother, eRuby, works the same but is written in C for speed;
# * Amrita (smart at producing HTML/XML);
# * cs/Template (written in C for speed);
# * RDoc, distributed with Ruby, uses its own template engine, which can be reused elsewhere;
# * and others; search the RAA.
# Rails, the web application framework, uses ERB to create views.
Revision = '$Date: 2009-02-24 02:44:50 +0900 (Tue, 24 Feb 2009) $' #'
# Returns revision information for the erb.rb module.
"erb.rb [2.1.0 #{ERB::Revision.split[1]}]"
class PercentLine # :nodoc:
def self.regist_scanner(klass, trim_mode, percent)
@scanner_map[[trim_mode, percent]] = klass
def self.default_scanner=(klass)
def self.make_scanner(src, trim_mode, percent)
klass = @scanner_map.fetch([trim_mode, percent], @default_scanner)
klass.new(src, trim_mode, percent)
def initialize(src, trim_mode, percent)
class TrimScanner < Scanner # :nodoc:
def initialize(src, trim_mode, percent)
@scan_line = self.method(:trim_line1)
@scan_line = self.method(:trim_line2)
@scan_line = self.method(:explicit_trim_line)
@scan_line = self.method(:scan_line)
percent_line(line, &block)
@scan_line.call(@src, &block)
def percent_line(line, &block)
if @stag || line[0] != ?%
return @scan_line.call(line, &block)
@scan_line.call(line, &block)
yield(PercentLine.new(line.chomp))
line.scan(/(.*?)(<%%|%%>|<%=|<%#|<%|%>|\n|\z)/m) do |tokens|
line.scan(/(.*?)(<%%|%%>|<%=|<%#|<%|%>\n|%>|\n|\z)/m) do |tokens|
line.scan(/(.*?)(<%%|%%>|<%=|<%#|<%|%>\n|%>|\n|\z)/m) do |tokens|
head = nil if token == "\n"
def explicit_trim_line(line)
line.scan(/(.*?)(^[ \t]*<%\-|<%\-|<%%|%%>|<%=|<%#|<%|-%>\n|-%>|%>|\z)/m) do |tokens|
if @stag.nil? && /[ \t]*<%-/ =~ token
elsif @stag && token == "-%>\n"
elsif @stag && token == '-%>'
ERB_STAG = %w(<%= <%# <%)
Scanner.default_scanner = TrimScanner
class SimpleScanner < Scanner # :nodoc:
@src.scan(/(.*?)(<%%|%%>|<%=|<%#|<%|%>|\n|\z)/m) do |tokens|
Scanner.regist_scanner(SimpleScanner, nil, false)
class SimpleScanner2 < Scanner # :nodoc:
stag_reg = /(.*?)(<%%|<%=|<%#|<%|\z)/m
etag_reg = /(.*?)(%%>|%>|\z)/m
scanner = StringScanner.new(@src)
scanner.scan(@stag ? etag_reg : stag_reg)
Scanner.regist_scanner(SimpleScanner2, nil, false)
class ExplicitScanner < Scanner # :nodoc:
stag_reg = /(.*?)(^[ \t]*<%-|<%%|<%=|<%#|<%-|<%|\z)/m
etag_reg = /(.*?)(%%>|-%>|%>|\z)/m
scanner = StringScanner.new(@src)
scanner.scan(@stag ? etag_reg : stag_reg)
yield(:cr) if scanner.scan(/(\n|\z)/)
Scanner.regist_scanner(ExplicitScanner, '-', false)
@compiler.pre_cmd.each do |x|
@script << (@line.join('; '))
@compiler.post_cmd.each do |x|
@script << (@line.join('; '))
scanner = make_scanner(s)