instiki/vendor/plugins/erubis-2.6.5/doc/users-guide.html
Jacques Distler a5e08f7bcc Rails_xss Plugin
I installed the rails_xss plugin, for
the main purpose of seeing what will
break with Rails 3.0 (where the behaviour
of the plugin is the default). I think
I've fixed everything, but let me know if you
see stuff that is HTML-escaped, which
shouldn't be.

As a side benefit, we now use Erubis,
rather than ERB, to render templates.
They tell me it's faster ...
2010-05-26 00:27:49 -05:00

3286 lines
101 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html">
<title>Erubis Users' Guide</title>
<meta name="generator" content="kwaser">
<meta http-equiv="Content-Style-Type" content="text/css">
<link rel="stylesheet" href="docstyle.css" type="text/css">
</head>
<body>
<blockquote>
<div class="mainbody">
<div align="left"><h1>Erubis Users' Guide</h1></div>
<div align="left">
last update: $Date$<br>
</div>
<p>release: 2.6.5
</p>
<a name="preface"></a>
<h2 class="section1">Preface</h2>
<p>Erubis is an implementation of eRuby.
It has the following features.
</p>
<ul type="disc">
<li>Very fast, almost three times faster than ERB and about ten percent faster than eruby (implemented in C)
</li>
<li>File caching of converted Ruby script support
</li>
<li>Auto escaping support
</li>
<li>Auto trimming spaces around '&lt;% %&gt;'
</li>
<li>Embedded pattern changeable (default '&lt;% %&gt;')
</li>
<li>Enable to handle Processing Instructions (PI) as embedded pattern (ex. '&lt;?rb ... ?&gt;')
</li>
<li>Multi-language support (Ruby/PHP/C/Java/Scheme/Perl/Javascript)
</li>
<li>Context object available and easy to combine eRuby template with YAML datafile
</li>
<li>Print statement available
</li>
<li>Easy to expand and customize in subclass
</li>
<li><a href="#rails">Ruby on Rails support</a>
</li>
<li>mod_ruby support|#topcs-modruby
</li>
</ul>
<p>Erubis is implemented in pure Ruby. It requires Ruby 1.8 or higher.
Erubis now supports Ruby 1.9.
</p>
<a name="toc"></a>
<h3 class="section2">Table of Contents</h3>
<ul>
<li><a href="#preface">Preface</a>
<ul>
<li><a href="#toc">Table of Contents</a>
</li>
</ul>
</li>
<li><a href="#install">Installation</a>
</li>
<li><a href="#tutorial">Tutorial</a>
<ul>
<li><a href="#tut-basic">Basic Example</a>
</li>
<li><a href="#tut-trim">Trimming Spaces</a>
</li>
<li><a href="#tut-escape">Escape</a>
</li>
<li><a href="#tut-pattern">Embedded Pattern</a>
</li>
<li><a href="#tut-context">Context Object</a>
</li>
<li><a href="#tut-datafile">Context Data File</a>
</li>
<li><a href="#tut-datastr">Context Data String</a>
</li>
<li><a href="#tut-preamble">Preamble and Postamble</a>
</li>
<li><a href="#tut-pi">Processing Instruction (PI) Converter</a>
</li>
<li><a href="#tut-notext">Retrieve Ruby Code</a>
</li>
</ul>
</li>
<li><a href="#enhancer">Enhancer</a>
<ul>
<li><a href="#escape-enhancer">EscapeEnhancer</a>
</li>
<li><a href="#stdout-enhancer">StdoutEnhancer</a>
</li>
<li><a href="#printout-enhancer">PrintOutEnhancer</a>
</li>
<li><a href="#printenabled-enhancer">PrintEnabledEnhancer</a>
</li>
<li><a href="#array-enhancer">ArrayEnhancer</a>
</li>
<li><a href="#arraybuffer-enhancer">ArrayBufferEnhancer</a>
</li>
<li><a href="#stringbuffer-enhancer">StringBufferEnhancer</a>
</li>
<li><a href="#erbout-enhancer">ErboutEnhancer</a>
</li>
<li><a href="#notext-enhancer">NoTextEnhancer</a>
</li>
<li><a href="#nocode-enhancer">NoCodeEnhancer</a>
</li>
<li><a href="#simplify-enhancer">SimplifyEnhancer</a>
</li>
<li><a href="#bipattern-enhancer">BiPatternEnhancer</a>
</li>
<li><a href="#percentline-enhancer">PercentLineEnhancer</a>
</li>
<li><a href="#headerfooter-enhancer">HeaderFooterEnhancer</a>
</li>
<li><a href="#interpolation-enhancer">InterpolationEnhancer</a>
</li>
<li><a href="#deleteindent-enhancer">DeleteIndentEnhancer</a>
</li>
</ul>
</li>
<li><a href="#lang">Multi-Language Support</a>
<ul>
<li><a href="#lang-php">PHP</a>
</li>
<li><a href="#lang-c">C</a>
</li>
<li><a href="#lang-java">Java</a>
</li>
<li><a href="#lang-scheme">Scheme</a>
</li>
<li><a href="#lang-perl">Perl</a>
</li>
<li><a href="#lang-javascript">JavaScript</a>
</li>
</ul>
</li>
<li><a href="#rails">Ruby on Rails Support</a>
<ul>
<li><a href="#rails-settings">Settings</a>
</li>
<li><a href="#rails-preprocessing">Preprosessing</a>
</li>
<li><a href="#rails-formhelpers">Form Helpers for Preprocessing</a>
</li>
<li><a href="#rails-others">Others</a>
</li>
</ul>
</li>
<li><a href="#topics">Other Topics</a>
<ul>
<li><a href="#'&lt;%= =%&gt;' and '&lt;%= -%&gt;'">'&lt;%= =%&gt;' and '&lt;%= -%&gt;'</a>
</li>
<li><a href="#'&lt;%% %&gt;' and '&lt;%%= %&gt;'">'&lt;%% %&gt;' and '&lt;%%= %&gt;'</a>
</li>
<li><a href="#topics-context-vs-binding">evaluate(context) v.s. result(binding)</a>
</li>
<li><a href="#topics-fasteruby">Class Erubis::FastEruby</a>
</li>
<li><a href="#topics-syntax">Syntax Checking</a>
</li>
<li><a href="#topics-caching">File Caching</a>
</li>
<li><a href="#topics-tinyeruby">Erubis::TinyEruby class</a>
</li>
<li><a href="#topics-php">NoTextEnhancer and NoCodeEnhancer in PHP</a>
</li>
<li><a href="#topcs-modruby">Helper Class for mod_ruby</a>
</li>
<li><a href="#topics-defmethod">Define method</a>
</li>
<li><a href="#topics-benchmark">Benchmark</a>
</li>
</ul>
</li>
<li><a href="#command">Command Reference</a>
<ul>
<li><a href="#command-usage">Usage</a>
</li>
<li><a href="#command-options">Options</a>
</li>
<li><a href="#command-props">Properties</a>
</li>
</ul>
</li>
</ul>
<br>
<br>
<a name="install"></a>
<h2 class="section1">Installation</h2>
<ul type="disc">
<li>If you have installed RubyGems, just type <code>gem install --remote erubis</code>.
<pre class="terminal">$ sudo gem install --remote erubis
</pre>
</li>
</ul>
<ul type="disc">
<li>Else install <a href="http://rubyforge.org/projects/erubis/">abstract</a> at first,
and download erubis_X.X.X.tar.bz2 and install it by setup.rb.
<pre class="terminal">$ tar xjf abstract_X.X.X.tar.bz2
$ cd abstract_X.X.X/
$ sudo ruby setup.rb
$ cd ..
$ tar xjf erubis_X.X.X.tar.bz2
$ cd erubis_X.X.X/
$ sudo ruby setup.rb
</pre>
</li>
</ul>
<ul type="disc">
<li>(Optional) 'contrib/inline-require' enables you to merge 'lib/**/*.rb' into 'bin/erubis'.
<pre class="terminal">$ tar xjf erubis_X.X.X.tar.bz2
$ cd erubis_X.X.X/
$ unset RUBYLIB
$ contrib/inline-require -I lib bin/erubis &gt; contrib/erubis
</pre>
</li>
</ul>
<br>
<a name="tutorial"></a>
<h2 class="section1">Tutorial</h2>
<a name="tut-basic"></a>
<h3 class="section2">Basic Example</h3>
<p>Here is a basic example of Erubis.
</p>
<a name="example1.eruby"></a>
<div class="program_caption">
example1.eruby</div>
<pre class="program">&lt;ul&gt;
<strong>&lt;% for item in list %&gt;</strong>
&lt;li&gt;<strong>&lt;%= item %&gt;</strong>&lt;/li&gt;
<strong>&lt;% end %&gt;</strong>
<strong>&lt;%# here is ignored because starting with '#' %&gt;</strong>
&lt;/ul&gt;
</pre>
<a name="example1.rb"></a>
<div class="program_caption">
example1.rb</div>
<pre class="program">require 'erubis'
input = File.read('example1.eruby')
eruby = <strong>Erubis::Eruby.new(input)</strong> # create Eruby object
puts "---------- script source ---"
puts <strong>eruby.src</strong> # print script source
puts "---------- result ----------"
list = ['aaa', 'bbb', 'ccc']
puts <strong>eruby.result(binding())</strong> # get result
## or puts eruby.result(<strong>:list=&gt;list</strong>) # or pass Hash instead of Binding
## # or
## eruby = Erubis::Eruby.new
## input = File.read('example1.eruby')
## src = eruby.convert(input)
## eval src
</pre>
<a name="example1.result"></a>
<div class="terminal_caption">
output</div>
<pre class="terminal">$ ruby example1.rb
---------- script source ---
_buf = ''; _buf &lt;&lt; '&lt;ul&gt;
'; for item in list
_buf &lt;&lt; ' &lt;li&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/li&gt;
'; end
_buf &lt;&lt; '&lt;/ul&gt;
';
_buf.to_s
---------- result ----------
&lt;ul&gt;
&lt;li&gt;aaa&lt;/li&gt;
&lt;li&gt;bbb&lt;/li&gt;
&lt;li&gt;ccc&lt;/li&gt;
&lt;/ul&gt;
</pre>
<p>Erubis has command 'erubis'. Command-line option '-x' shows the compiled source code of eRuby script.
</p>
<a name="example1_x.result"></a>
<div class="terminal_caption">
example of command-line option '-x'</div>
<pre class="terminal">$ erubis <strong>-x</strong> example1.eruby
_buf = ''; _buf &lt;&lt; '&lt;ul&gt;
'; for item in list
_buf &lt;&lt; ' &lt;li&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/li&gt;
'; end
_buf &lt;&lt; '&lt;/ul&gt;
';
_buf.to_s
</pre>
<br>
<a name="tut-trim"></a>
<h3 class="section2">Trimming Spaces</h3>
<p>Erubis deletes spaces around '&lt;% %&gt;' automatically, while it leaves spaces around '&lt;%= %&gt;'.
</p>
<a name="example2.eruby.comment_filter"></a>
<div class="program_caption">
example2.eruby</div>
<pre class="program">&lt;ul&gt;
&lt;% for item in list %&gt; # trimmed
&lt;li&gt;
&lt;%= item %&gt; # not trimmed
&lt;/li&gt;
&lt;% end %&gt; # trimmed
&lt;/ul&gt;
</pre>
<a name="example2_x.result"></a>
<div class="terminal_caption">
compiled source code</div>
<pre class="terminal">$ erubis -x example2.eruby
_buf = ''; _buf &lt;&lt; '&lt;ul&gt;
'; for item in list
_buf &lt;&lt; ' &lt;li&gt;
'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '
'; _buf &lt;&lt; ' &lt;/li&gt;
'; end
_buf &lt;&lt; '&lt;/ul&gt;
';
_buf.to_s
</pre>
<p>If you want leave spaces around '&lt;% %&gt;', add command-line property '--trim=false'.
</p>
<a name="example2_trim.result"></a>
<div class="terminal_caption">
compiled source code with command-line property '--trim=false'</div>
<pre class="terminal">$ erubis -x <strong>--trim=false</strong> example2.eruby
_buf = ''; _buf &lt;&lt; '&lt;ul&gt;
'; _buf &lt;&lt; ' '; for item in list ; _buf &lt;&lt; '
'; _buf &lt;&lt; ' &lt;li&gt;
'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '
'; _buf &lt;&lt; ' &lt;/li&gt;
'; _buf &lt;&lt; ' '; end ; _buf &lt;&lt; '
'; _buf &lt;&lt; '&lt;/ul&gt;
';
_buf.to_s
</pre>
<p>Or add option <code>:trim=&gt;false</code> to Erubis::Eruby.new().
</p>
<a name="example2.rb"></a>
<div class="program_caption">
example2.rb</div>
<pre class="program">require 'erubis'
input = File.read('example2.eruby')
eruby = Erubis::Eruby.new(input<strong>, :trim=&gt;false</strong>)
puts "----- script source ---"
puts eruby.src # print script source
puts "----- result ----------"
list = ['aaa', 'bbb', 'ccc']
puts eruby.result(binding()) # get result
</pre>
<a name="example2.result"></a>
<div class="terminal_caption">
output</div>
<pre class="terminal">$ ruby example2.rb
----- script source ---
_buf = ''; _buf &lt;&lt; '&lt;ul&gt;
'; <strong>_buf &lt;&lt; ' ';</strong> for item in list ; _buf &lt;&lt; '
'; _buf &lt;&lt; ' &lt;li&gt;
'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '
'; _buf &lt;&lt; ' &lt;/li&gt;
'; <strong>_buf &lt;&lt; ' ';</strong> end ; _buf &lt;&lt; '
'; _buf &lt;&lt; '&lt;/ul&gt;
';
_buf.to_s
----- result ----------
&lt;ul&gt;
&lt;li&gt;
aaa
&lt;/li&gt;
&lt;li&gt;
bbb
&lt;/li&gt;
&lt;li&gt;
ccc
&lt;/li&gt;
&lt;/ul&gt;
</pre>
<br>
<a name="tut-escape"></a>
<h3 class="section2">Escape</h3>
<p>Erubis has ability to escape (sanitize) expression.
Erubis::Eruby class act as the following:
</p>
<ul type="disc">
<li><code>&lt;%= <em>expr</em> %&gt;</code> - not escaped.
</li>
<li><code>&lt;%== <em>expr</em> %&gt;</code> - escaped.
</li>
<li><code>&lt;%=== <em>expr</em> %&gt;</code> - out to $stderr.
</li>
<li><code>&lt;%==== <em>expr</em> %&gt;</code> - ignored.
</li>
</ul>
<p>Erubis::EscapedEruby<sup>(<a href="#fnref:1" name="fnlink:1">*1</a>)</sup> class handle '&lt;%= %&gt;' as escaped and '&lt;%== %&gt;' as not escaped.
It means that using Erubis::EscapedEruby you can escape expression by default.
Also Erubis::XmlEruby class (which is equivalent to Erubis::EscapedEruby) is provided for compatibility with Erubis 1.1.
</p>
<a name="example3.eruby"></a>
<div class="program_caption">
example3.eruby</div>
<pre class="program">&lt;% for item in list %&gt;
&lt;p&gt;<strong>&lt;%=</strong> item <strong>%&gt;</strong>&lt;/p&gt;
&lt;p&gt;<strong>&lt;%==</strong> item <strong>%&gt;</strong>&lt;/p&gt;
&lt;p&gt;<strong>&lt;%===</strong> item <strong>%&gt;</strong>&lt;/p&gt;
&lt;% end %&gt;
</pre>
<a name="example3.rb"></a>
<div class="program_caption">
example3.rb</div>
<pre class="program">require 'erubis'
input = File.read('example3.eruby')
eruby = Erubis::<strong>EscapedEruby</strong>.new(input) # or Erubis::XmlEruby
puts "----- script source ---"
puts eruby.src # print script source
puts "----- result ----------"
<strong>list = ['&lt;aaa&gt;', 'b&amp;b', '"ccc"']</strong>
puts eruby.result(binding()) # get result
</pre>
<a name="example3.result.split_filter"></a>
<div class="terminal_caption">
output</div>
<pre class="terminal">$ ruby example3.rb 2&gt; stderr.log
----- script source ---
_buf = ''; for item in list
_buf &lt;&lt; ' &lt;p&gt;'; <strong>_buf &lt;&lt; Erubis::XmlHelper.escape_xml( item )</strong>; _buf &lt;&lt; '&lt;/p&gt;
&lt;p&gt;'; <strong>_buf &lt;&lt; ( item ).to_s</strong>; _buf &lt;&lt; '&lt;/p&gt;
&lt;p&gt;'; <strong>$stderr.puts("*** debug: item=#{(item).inspect}")</strong>; _buf &lt;&lt; '&lt;/p&gt;
'; end
_buf.to_s
----- result ----------
&lt;p&gt;<strong>&amp;lt;aaa&amp;gt;</strong>&lt;/p&gt;
&lt;p&gt;&lt;aaa&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;<strong>b&amp;amp;b</strong>&lt;/p&gt;
&lt;p&gt;b&amp;b&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;<strong>&amp;quot;ccc&amp;quot;</strong>&lt;/p&gt;
&lt;p&gt;"ccc"&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
$ cat stderr.log
*** debug: item="&lt;aaa&gt;"
*** debug: item="b&amp;b"
*** debug: item="\"ccc\""
</pre>
<p>The command-line option '-e' will do the same action as Erubis::EscapedEruby.
This option is available for any language.
</p>
<a name="example3_e.result"></a>
<pre class="terminal">$ erubis -l ruby <strong>-e</strong> example3.eruby
_buf = ''; for item in list
_buf &lt;&lt; ' &lt;p&gt;'; _buf &lt;&lt; Erubis::XmlHelper.escape_xml( item ); _buf &lt;&lt; '&lt;/p&gt;
&lt;p&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/p&gt;
&lt;p&gt;'; $stderr.puts("*** debug: item=#{(item).inspect}"); _buf &lt;&lt; '&lt;/p&gt;
'; end
_buf.to_s
</pre>
<p>Escaping function (default 'Erubis::XmlHelper.escape_xml()') can be changed by command-line property '--escapefunc=xxx' or by overriding Erubis::Eruby#escaped_expr() in subclass.
</p>
<div class="program_caption">
example to override Erubis::Eruby#escaped_expr()</div>
<pre class="program">class CGIEruby &lt; Erubis::Eruby
def <strong>escaped_expr(code)</strong>
return "CGI.escapeHTML((#{code.strip}).to_s)"
#return "h(#{code.strip})"
end
end
class LatexEruby &lt; Erubi::Eruby
def <strong>escaped_expr(code)</strong>
return "(#{code}).gsub(/[%\\]/,'\\\\\&amp;')"
end
end
</pre>
<div class="footnote">
<dl compact>
<dt>(<a name="fnref:1" href="#fnlink:1">*1</a>)</dt>
<dd>Erubis::EscapedEruby class includes Erubis::EscapeEnhancer which swtches the action of '&lt;%= %&gt;' and '&lt;%== %&gt;'.</dd>
</dl>
</div>
<br>
<a name="tut-pattern"></a>
<h3 class="section2">Embedded Pattern</h3>
<p>You can change embedded pattern '<code>&lt;% %&gt;</code>' to another by command-line option '-p' or option '<code>:pattern=&gt;...</code>' of Erubis::Eruby.new().
</p>
<a name="example4.eruby"></a>
<div class="program_caption">
example4.eruby</div>
<pre class="program"><strong>&lt;!--%</strong> for item in list <strong>%--&gt;</strong>
&lt;p&gt;<strong>&lt;!--%=</strong> item <strong>%--&gt;</strong>&lt;/p&gt;
<strong>&lt;!--%</strong> end <strong>%--&gt;</strong>
</pre>
<a name="example4_x.result"></a>
<div class="terminal_caption">
compiled source code with command-line option '-p'</div>
<pre class="terminal">$ erubis -x <strong>-p '&lt;!--% %--&gt;'</strong> example4.eruby
_buf = ''; for item in list
_buf &lt;&lt; ' &lt;p&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/p&gt;
'; end
_buf.to_s
</pre>
<a name="example4.rb"></a>
<div class="program_caption">
example4.rb</div>
<pre class="program">require 'erubis'
input = File.read('example4.eruby')
eruby = Erubis::Eruby.new(input<strong>, :pattern=&gt;'&lt;!--% %--&gt;'</strong>)
# or '&lt;(?:!--)?% %(?:--)?&gt;'
puts "---------- script source ---"
puts eruby.src # print script source
puts "---------- result ----------"
list = ['aaa', 'bbb', 'ccc']
puts eruby.result(binding()) # get result
</pre>
<a name="example4.result"></a>
<div class="terminal_caption">
output</div>
<pre class="terminal">$ ruby example4.rb
---------- script source ---
_buf = ''; for item in list
_buf &lt;&lt; ' &lt;p&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/p&gt;
'; end
_buf.to_s
---------- result ----------
&lt;p&gt;aaa&lt;/p&gt;
&lt;p&gt;bbb&lt;/p&gt;
&lt;p&gt;ccc&lt;/p&gt;
</pre>
<p>It is able to specify regular expression with :pattern option.
Notice that you must use '<code>(?: )</code>' instead of '<code>( )</code>' for grouping.
For example, '<code>&lt;(!--)?% %(--)?&gt;</code>' will not work while '<code>&lt;(?:!--)?% %(?:--)?&gt;</code>' will work.
</p>
<br>
<a name="tut-context"></a>
<h3 class="section2">Context Object</h3>
<p>Context object is a set of data which are used in eRuby script.
Using context object makes clear which data to be used.
In Erubis, Hash object and Erubis::Context object are available as context object.
</p>
<p>Context data can be accessible via instance variables in eRuby script.
</p>
<a name="example5.eruby"></a>
<div class="program_caption">
example5.eruby</div>
<pre class="program">&lt;span&gt;&lt;%= <strong>@val</strong> %&gt;&lt;/span&gt;
&lt;ul&gt;
&lt;% for item in <strong>@list</strong> %&gt;
&lt;li&gt;&lt;%= item %&gt;&lt;/li&gt;
&lt;% end %&gt;
&lt;/ul&gt;
</pre>
<a name="example5.rb"></a>
<div class="program_caption">
example5.rb</div>
<pre class="program">require 'erubis'
input = File.read('example5.eruby')
eruby = Erubis::Eruby.new(input) # create Eruby object
## create context object
## (key means var name, which may be string or symbol.)
<strong>context = {
:val =&gt; 'Erubis Example',
'list' =&gt; ['aaa', 'bbb', 'ccc'],
}</strong>
## or
# context = Erubis::Context.new()
# context['val'] = 'Erubis Example'
# context[:list] = ['aaa', 'bbb', 'ccc'],
puts <strong>eruby.evaluate(context)</strong> # get result
</pre>
<a name="example5.result"></a>
<div class="terminal_caption">
output</div>
<pre class="terminal">$ ruby example5.rb
&lt;span&gt;Erubis Example&lt;/span&gt;
&lt;ul&gt;
&lt;li&gt;aaa&lt;/li&gt;
&lt;li&gt;bbb&lt;/li&gt;
&lt;li&gt;ccc&lt;/li&gt;
&lt;/ul&gt;
</pre>
<p>The difference between Erubis#result(binding) and Erubis#evaluate(context) is that the former invokes 'eval @src, binding' and the latter invokes 'context.instance_eval @src'.
This means that data is passed into eRuby script via local variables when Eruby::binding() is called, or passed via instance variables when Eruby::evaluate() is called.
</p>
<p>Here is the definition of Erubis#result() and Erubis#evaluate().
</p>
<div class="program_caption">
definition of result(binding) and evaluate(context)</div>
<pre class="program">def result(_binding=TOPLEVEL_BINDING)
if _binding.is_a?(Hash)
# load hash data as local variable
_h = _binding
_binding = binding()
eval _h.collect{|k,v| "#{k} = _h[#{k.inspect}];"}.join, _binding
end
return <strong>eval(@src, _binding)</strong>
end
def evaluate(_context=Erubis::Context.new)
if _context.is_a?(Hash)
# convert hash object to Context object
_hash = _context
_context = Erubis::Context.new
_hash.each {|k, v| _context[k] = v }
end
return <strong>_context.instance_eval(@src)</strong>
end
</pre>
<p>instance_eval() is defined at Object class so it is able to use any object as a context object as well as Hash or Erubis::Context.
</p>
<a name="example6.rb"></a>
<div class="program_caption">
example6.rb</div>
<pre class="program">class MyData
attr_accessor :val, :list
end
## any object can be a context object
<strong>mydata = MyData.new</strong>
<strong>mydata.val = 'Erubis Example'</strong>
<strong>mydata.list = ['aaa', 'bbb', 'ccc']</strong>
require 'erubis'
eruby = Erubis::Eruby.new(File.read('example5.eruby'))
puts eruby.evaluate(<strong>mydata</strong>)
</pre>
<a name="example6.result"></a>
<div class="terminal_caption">
output</div>
<pre class="terminal">$ ruby example6.rb
&lt;span&gt;Erubis Example&lt;/span&gt;
&lt;ul&gt;
&lt;li&gt;aaa&lt;/li&gt;
&lt;li&gt;bbb&lt;/li&gt;
&lt;li&gt;ccc&lt;/li&gt;
&lt;/ul&gt;
</pre>
<p>It is recommended to use 'Erubis::Eruby#evaluate(context)' rather than 'Erubis::Eruby#result(binding())' because the latter has some problems.
See <a href="#topics-context-vs-binding">evaluate(context) v.s. result(binding)</a> section for details.
</p>
<br>
<a name="tut-datafile"></a>
<h3 class="section2">Context Data File</h3>
<p>Command-line option '-f' specifies context data file.
Erubis load context data file and use it as context data.
Context data file can be YAML file ('*.yaml' or '*.yml') or Ruby script ('*.rb').
</p>
<a name="example7.eruby"></a>
<div class="program_caption">
example7.eruby</div>
<pre class="program">&lt;h1&gt;&lt;%= <strong>@title</strong> %&gt;&lt;/h1&gt;
&lt;ul&gt;
&lt;% for user in <strong>@users</strong> %&gt;
&lt;li&gt;
&lt;a href="mailto:&lt;%= user['mail']%&gt;"&gt;&lt;%= user['name'] %&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;% end %&gt;
&lt;/ul&gt;
</pre>
<a name="context.yaml"></a>
<div class="program_caption">
context.yaml</div>
<pre class="program"><strong>title:</strong> Users List
<strong>users:</strong>
- name: foo
mail: foo@mail.com
- name: bar
mail: bar@mail.net
- name: baz
mail: baz@mail.org
</pre>
<a name="context.rb"></a>
<div class="program_caption">
context.rb</div>
<pre class="program">@title = 'Users List'
@users = [
{ 'name'=&gt;'foo', 'mail'=&gt;'foo@mail.com' },
{ 'name'=&gt;'bar', 'mail'=&gt;'bar@mail.net' },
{ 'name'=&gt;'baz', 'mail'=&gt;'baz@mail.org' },
]
</pre>
<a name="example7.result.split_filter"></a>
<div class="terminal_caption">
example of command-line option '-f'</div>
<pre class="terminal">$ erubis <strong>-f context.yaml</strong> example7.eruby
&lt;h1&gt;Users List&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href="mailto:foo@mail.com"&gt;foo&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="mailto:bar@mail.net"&gt;bar&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="mailto:baz@mail.org"&gt;baz&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
$ erubis <strong>-f context.rb</strong> example7.eruby
&lt;h1&gt;Users List&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href="mailto:foo@mail.com"&gt;foo&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="mailto:bar@mail.net"&gt;bar&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="mailto:baz@mail.org"&gt;baz&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
</pre>
<p>Command-line option '-S' converts keys of mapping in YAML data file from string into symbol.
Command-line option '-B' invokes 'Erubis::Eruby#result(binding())' instead of 'Erubis::Eruby#evaluate(context)'.
</p>
<br>
<a name="tut-datastr"></a>
<h3 class="section2">Context Data String</h3>
<p>Command-line option '-c <em>str</em>' enables you to specify context data in command-line.
<em>str</em> can be YAML flow-style or Ruby code.
</p>
<a name="example8.eruby"></a>
<div class="program_caption">
example8.eruby</div>
<pre class="program">&lt;h1&gt;&lt;%= @title %&gt;&lt;/h1&gt;
&lt;ul&gt;
&lt;% for item in @list %&gt;
&lt;li&gt;&lt;%= item %&gt;&lt;/li&gt;
&lt;% end %&gt;
&lt;/ul&gt;
</pre>
<a name="example8_yaml.result"></a>
<div class="terminal_caption">
example of YAML flow style</div>
<pre class="terminal">$ erubis <strong>-c '{title: Example, list: [AAA, BBB, CCC]}'</strong> example8.eruby
&lt;h1&gt;Example&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;AAA&lt;/li&gt;
&lt;li&gt;BBB&lt;/li&gt;
&lt;li&gt;CCC&lt;/li&gt;
&lt;/ul&gt;
</pre>
<a name="example8_ruby.result"></a>
<div class="terminal_caption">
example of Ruby code</div>
<pre class="terminal">$ erubis <strong>-c '@title="Example"; @list=%w[AAA BBB CCC]'</strong> example8.eruby
&lt;h1&gt;Example&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;AAA&lt;/li&gt;
&lt;li&gt;BBB&lt;/li&gt;
&lt;li&gt;CCC&lt;/li&gt;
&lt;/ul&gt;
</pre>
<br>
<a name="tut-preamble"></a>
<h3 class="section2">Preamble and Postamble</h3>
<p>The first line ('_buf = '';') in the compiled source code is called preamble
and the last line ('_buf.to_s') is called postamble.
</p>
<p>Command-line option '-b' skips the output of preamble and postamble.
</p>
<a name="example9.eruby"></a>
<div class="program_caption">
example9.eruby</div>
<pre class="program">&lt;% for item in @list %&gt;
&lt;b&gt;&lt;%= item %&gt;&lt;/b&gt;
&lt;% end %&gt;
</pre>
<a name="example9.result.split_filter"></a>
<div class="terminal_caption">
compiled source code with and without command-line option '-b'</div>
<pre class="terminal">$ erubis -x example9.eruby
<strong>_buf = '';</strong> for item in @list
_buf &lt;&lt; ' &lt;b&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/b&gt;
'; end
<strong>_buf.to_s</strong>
$ erubis -x <strong>-b</strong> example9.eruby
for item in @list
_buf &lt;&lt; ' &lt;b&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/b&gt;
'; end
</pre>
<p>Erubis::Eruby.new option '<code>:preamble=&gt;false</code>' and '<code>:postamble=&gt;false</code>' also suppress output of preamble or postamle.
</p>
<a name="example9.rb"></a>
<div class="program_caption">
example9.rb</div>
<pre class="program">require 'erubis'
input = File.read('example9.eruby')
eruby1 = Erubis::Eruby.new(input)
eruby2 = Erubis::Eruby.new(input, <strong>:preamble=&gt;false, :postamble=&gt;false</strong>)
puts eruby1.src # print preamble and postamble
puts "--------------"
puts eruby2.src # don't print preamble and postamble
</pre>
<a name="example9.result"></a>
<div class="terminal_caption">
output</div>
<pre class="terminal">$ ruby example9.rb
_buf = ''; for item in @list
_buf &lt;&lt; ' &lt;b&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/b&gt;
'; end
_buf.to_s
--------------
for item in @list
_buf &lt;&lt; ' &lt;b&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/b&gt;
'; end
</pre>
<br>
<a name="tut-pi"></a>
<h3 class="section2">Processing Instruction (PI) Converter</h3>
<p>Erubis can parse Processing Instructions (PI) as embedded pattern.
</p>
<ul type="disc">
<li>'<code>&lt;?rb <em>...</em> ?&gt;</code>' represents Ruby statement.
</li>
<li>'<code>@{<em>...</em>}@</code>' represents escaped expression value.
</li>
<li>'<code>@!{<em>...</em>}@</code>' represents normal expression value.
</li>
<li>'<code>@!!{<em>...</em>}@</code>' prints expression value to standard output.
</li>
<li>(experimental) '<code>&lt;%= <em>...</em> %&gt;</code>' is also available to print expression value.
</li>
</ul>
<p>This is more useful than basic embedded pattern ('<code>&lt;% ... &gt;</code>') because PI doesn't break XML or HTML at all.
For example the following XHTML file is well-formed and HTML validator got no errors on this example.
</p>
<a name="example10.xhtml"></a>
<div class="program_caption">
example10.xhtml</div>
<pre class="program">&lt;?xml version="1.0" ?&gt;
<strong>&lt;?rb
lang = 'en'
list = ['&lt;aaa&gt;', 'b&amp;b', '"ccc"']
?&gt;</strong>
&lt;html lang="<strong>@!{lang}@</strong>"&gt;
&lt;body&gt;
&lt;ul&gt;
<strong>&lt;?rb for item in list ?&gt;</strong>
&lt;li&gt;<strong>@{item}@</strong>&lt;/li&gt;
<strong>&lt;?rb end ?&gt;</strong>
&lt;/ul&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>If the command-line property '--pi=<em>name</em>' is specified, erubis command parses input with PI converter.
If <em>name</em> is omitted then the following name is used according to '-l <em>lang</em>'.
</p>
<div align="center">
<table class="table1" border="1" cellspacing="0">
<tr class="tr1">
<th class="th1">'-l' option</th>
<th class="th1">PI name</th>
</tr>
<tr class="tr1">
<td class="td1">-l ruby</td>
<td class="td1">&lt;?rb ... ?&gt;</td>
</tr>
<tr class="tr1">
<td class="td1">-l php</td>
<td class="td1">&lt;?php ... ?&gt;</td>
</tr>
<tr class="tr1">
<td class="td1">-l perl</td>
<td class="td1">&lt;?perl ... ?&gt;</td>
</tr>
<tr class="tr1">
<td class="td1">-l java</td>
<td class="td1">&lt;?java ... ?&gt;</td>
</tr>
<tr class="tr1">
<td class="td1">-l javascript</td>
<td class="td1">&lt;?js ... ?&gt;</td>
</tr>
<tr class="tr1">
<td class="td1">-l scheme</td>
<td class="td1">&lt;?scheme ... ?&gt;</td>
</tr>
</table>
</div>
<a name="example10_x.result"></a>
<div class="terminal_caption">
output</div>
<pre class="terminal">$ erubis -x <strong>--pi</strong> example10.xhtml
_buf = ''; _buf &lt;&lt; '&lt;?xml version="1.0" ?&gt;
';
lang = 'en'
list = ['&lt;aaa&gt;', 'b&amp;b', '"ccc"']
_buf &lt;&lt; '&lt;html lang="'; _buf &lt;&lt; (lang).to_s; _buf &lt;&lt; '"&gt;
&lt;body&gt;
&lt;ul&gt;
'; for item in list
_buf &lt;&lt; ' &lt;li&gt;'; _buf &lt;&lt; Erubis::XmlHelper.escape_xml(item); _buf &lt;&lt; '&lt;/li&gt;
'; end
_buf &lt;&lt; ' &lt;/ul&gt;
&lt;/body&gt;
&lt;/html&gt;
';
_buf.to_s
</pre>
<p>Expression character can be changeable by command-line property '--embchar=<em>char</em>. Default is '<code>@</code>'.
</p>
<p>Use Erubis::PI::Eruby instead of Erubis::Eruby if you want to use PI as embedded pattern.
</p>
<a name="example10.rb"></a>
<div class="program_caption">
example10.rb</div>
<pre class="program">require 'erubis'
input = File.read('example10.xhtml')
eruby = Erubis::PI::Eruby.new(input)
print eruby.src
</pre>
<a name="example10.result"></a>
<div class="terminal_caption">
output</div>
<pre class="terminal">$ ruby example10.rb
_buf = ''; _buf &lt;&lt; '&lt;?xml version="1.0" ?&gt;
';
lang = 'en'
list = ['&lt;aaa&gt;', 'b&amp;b', '"ccc"']
_buf &lt;&lt; '&lt;html lang="'; _buf &lt;&lt; (lang).to_s; _buf &lt;&lt; '"&gt;
&lt;body&gt;
&lt;ul&gt;
'; for item in list
_buf &lt;&lt; ' &lt;li&gt;'; _buf &lt;&lt; Erubis::XmlHelper.escape_xml(item); _buf &lt;&lt; '&lt;/li&gt;
'; end
_buf &lt;&lt; ' &lt;/ul&gt;
&lt;/body&gt;
&lt;/html&gt;
';
_buf.to_s
</pre>
<p><strong>(experimental)</strong> Erubis supports '&lt;%= ... %&gt;' pattern with PI pattern.
</p>
<div class="program_caption">
example of Rails view template</div>
<pre class="program">&lt;table&gt;
&lt;tr&gt;
&lt;?rb for item in @list ?&gt;
&lt;td&gt;@{item.id}@&lt;/td&gt;
&lt;td&gt;@{item.name}@&lt;/td&gt;
&lt;td&gt;
<strong>&lt;%=</strong> link_to 'Destroy', {:action=&gt;'destroy', :id=&gt;item.id},
:confirm=&gt;'Are you OK?' <strong>%&gt;</strong>
&lt;/td&gt;
&lt;?rb end ?&gt;
&lt;/tr&gt;
&lt;/table&gt;
</pre>
<br>
<a name="tut-notext"></a>
<h3 class="section2">Retrieve Ruby Code</h3>
<p>Similar to '-x', ommand-line option '-X' shows converted Ruby source code.
The difference between '-x' and 'X' is that the former converts text part but the latter ignores it.
It means that you can retrieve Ruby code from eRuby script by '-X' option.
</p>
<p>For example, see the following eRuby script.
This is some complex, so it is difficult to grasp the program code.
</p>
<a name="example11.rhtml"></a>
<div class="program_caption">
example11.rhtml</div>
<pre class="program">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt;
&lt;body&gt;
&lt;h3&gt;List&lt;/h3&gt;
&lt;% if @list.nil? || @list.empty? %&gt;
&lt;p&gt;not found.&lt;/p&gt;
&lt;% else %&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;% @list.each_with_index do |item, i| %&gt;
&lt;tr bgcolor="&lt;%= i % 2 == 0 ? '#FCC' : '#CCF' %&gt;"&gt;
&lt;td&gt;&lt;%= item %&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;% end %&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;% end %&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>Command-line option '-X' extracts only the ruby code from eRuby script.
</p>
<a name="example11.result"></a>
<div class="terminal_caption">
result</div>
<pre class="terminal">$ erubis <strong>-X</strong> example11.rhtml
_buf = '';
if @list.nil? || @list.empty?
else
@list.each_with_index do |item, i|
_buf &lt;&lt; ( i % 2 == 0 ? '#FCC' : '#CCF' ).to_s;
_buf &lt;&lt; ( item ).to_s;
end
end
_buf.to_s
</pre>
<p>Command-line option '-C' (<strong>c</strong>mpact) deletes empty lines.
</p>
<a name="example11_C.result"></a>
<div class="terminal_caption">
result</div>
<pre class="terminal">$ erubis <strong>-XC</strong> example11.rhtml
_buf = '';
if @list.nil? || @list.empty?
else
@list.each_with_index do |item, i|
_buf &lt;&lt; ( i % 2 == 0 ? '#FCC' : '#CCF' ).to_s;
_buf &lt;&lt; ( item ).to_s;
end
end
_buf.to_s
</pre>
<p>Option '-U' (<strong>u</strong>nique) converts empty lines into a line.
</p>
<a name="example11_U.result"></a>
<div class="terminal_caption">
result</div>
<pre class="terminal">$ erubis <strong>-XU</strong> example11.rhtml
_buf = '';
if @list.nil? || @list.empty?
else
@list.each_with_index do |item, i|
_buf &lt;&lt; ( i % 2 == 0 ? '#FCC' : '#CCF' ).to_s;
_buf &lt;&lt; ( item ).to_s;
end
end
_buf.to_s
</pre>
<p>Option '-N' (<strong>n</strong>umber) adds line number.
It is available with '-C' or '-U'.
</p>
<a name="example11_N.result"></a>
<div class="terminal_caption">
result</div>
<pre class="terminal">$ erubis <strong>-XNU</strong> example11.rhtml
1: _buf = '';
7: if @list.nil? || @list.empty?
9: else
12: @list.each_with_index do |item, i|
13: _buf &lt;&lt; ( i % 2 == 0 ? '#FCC' : '#CCF' ).to_s;
14: _buf &lt;&lt; ( item ).to_s;
16: end
19: end
22: _buf.to_s
</pre>
<p>Command-line option '-X' is available with PHP script.
</p>
<a name="example11.php"></a>
<div class="program_caption">
example11.php</div>
<pre class="program">&lt;?xml version="1.0"?&gt;
&lt;html&gt;
&lt;body&gt;
&lt;h3&gt;List&lt;/h3&gt;
&lt;?php if (!$list) { ?&gt;
&lt;p&gt;not found.&lt;/p&gt;
&lt;?php } else { ?&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;?php $i = 0; ?&gt;
&lt;?php foreach ($list as $item) { ?&gt;
&lt;tr bgcolor="&lt;?php echo ++$i % 2 == 1 ? '#FCC' : '#CCF'; ?&gt;"&gt;
&lt;td&gt;&lt;?php echo $item; ?&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;?php } ?&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;?php } ?&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<a name="example11_php.result"></a>
<div class="terminal_caption">
result</div>
<pre class="terminal">$ erubis -XNU <strong>-l php</strong> <strong>--pi=php</strong> --trim=false example11.php
5: &lt;?php if (!$list) { ?&gt;
7: &lt;?php } else { ?&gt;
10: &lt;?php $i = 0; ?&gt;
11: &lt;?php foreach ($list as $item) { ?&gt;
12: &lt;?php echo ++$i % 2 == 1 ? '#FCC' : '#CCF'; ?&gt;
13: &lt;?php echo $item; ?&gt;
15: &lt;?php } ?&gt;
18: &lt;?php } ?&gt;
</pre>
<br>
<br>
<a name="enhancer"></a>
<h2 class="section1">Enhancer</h2>
<p>Enhancer is a module to add a certain feature into Erubis::Eruby class.
Enhancer may be language-independent or only for Erubis::Eruby class.
</p>
<p>To use enhancers, define subclass and include them.
The folloing is an example to use <a href="#escape-enhancer">EscapeEnhancer</a>, <a href="#percentline-enhancer">PercentLineEnhancer</a>, and <a href="#bipattern-enhancer">BiPatternEnhancer</a>.
</p>
<pre class="program">class MyEruby &lt; Erubis::Eruby
include EscapeEnhancer
include PercentLineEnhancer
include BiPatternEnhancer
end
</pre>
<p>You can specify enhancers in command-line with option '-E'.
The following is an example to use some enhancers in command-line.
</p>
<pre class="terminal">$ erubis -xE Escape,PercentLine,BiPattern example.eruby
</pre>
<p>The following is the list of enhancers.
</p>
<dl class="dl1">
<dt class="dt1">
<a href="#escape-enhancer">EscapeEnhander</a> (language-independent)</dt>
<dd class="dd1">
Switch '&lt;%= %&gt;' to escaped and '&lt;%== %&gt;' to unescaped.
</dd>
<dt class="dt1">
<a href="#stdout-enhancer">StdoutEnhancer</a> (only for Eruby)</dt>
<dd class="dd1">
Use $stdout instead of array buffer.
</dd>
<dt class="dt1">
<a href="#printout-enhancer">PrintOutEnhancer</a> (only for Eruby)</dt>
<dd class="dd1">
Use "print(...)" statement insead of "_buf &lt;&lt; ...".
</dd>
<dt class="dt1">
<a href="#printenabled-enhancer">PrintEnabledEnhancer</a> (only for Eruby)</dt>
<dd class="dd1">
Enable to use print() in '&lt;% ... %&gt;'.
</dd>
<dt class="dt1">
<a href="#array-enhancer">ArrayEnhancer</a> (only for Eruby)</dt>
<dd class="dd1">
Return array of string instead of returning string.
</dd>
<dt class="dt1">
<a href="#arraybuffer-enhancer">ArrayBufferEnhancer</a> (only for Eruby)</dt>
<dd class="dd1">
Use array buffer. It is a little slower than StringBufferEnhancer.
</dd>
<dt class="dt1">
<a href="#stringbuffer-enhancer">StringBufferEnhancer</a> (only for Eruby)</dt>
<dd class="dd1">
Use string buffer. This is included in Erubis::Eruby by default.
</dd>
<dt class="dt1">
<a href="#erbout-enhancer">ErboutEnhancer</a> (only for Eruby)</dt>
<dd class="dd1">
Set '_erbout = _buf = "";' to be compatible with ERB.
</dd>
<dt class="dt1">
<a href="#notext-enhancer">NoTextEnhancer</a> (language-independent)</dt>
<dd class="dd1">
Print embedded code only and ignore normal text.
</dd>
<dt class="dt1">
<a href="#nocode-enhancer">NoCodeEnhancer</a> (language-independent)</dt>
<dd class="dd1">
Print normal text only and ignore code.
</dd>
<dt class="dt1">
<a href="#simplify-enhancer">SimplifyEnhancer</a> (language-independent)</dt>
<dd class="dd1">
Make compile faster but don't trim spaces around '&lt;% %&gt;'.
</dd>
<dt class="dt1">
<a href="#bipattern-enhancer">BiPatternEnhancer</a> (language-independent)</dt>
<dd class="dd1">
[experimental] Enable to use another embedded pattern with '&lt;% %&gt;'.
</dd>
<dt class="dt1">
<a href="#percentline-enhancer">PercentLineEnhancer</a> (language-independent)</dt>
<dd class="dd1">
Regard lines starting with '%' as Ruby code. This is for compatibility with eruby and ERB.
</dd>
<dt class="dt1">
<a href="#headerfooter-enhancer">HeaderFooterEnhancer</a> (language-independent)</dt>
<dd class="dd1">
[experimental] Enable you to add header and footer in eRuby script.
</dd>
<dt class="dt1">
<a href="#interpolation-enhancer">InterpolationEnhancer</a> (only for Eruby)</dt>
<dd class="dd1">
[experimental] convert '&lt;p&gt;&lt;%= text %&gt;&lt;/p&gt;' into '_buf &lt;&lt; %Q`&lt;p&gt;#{text}&lt;/p&gt;`'.
</dd>
<dt class="dt1">
<a href="#deleteindent-enhancer">DeleteIndentEnhancer</a> (language-independent)</dt>
<dd class="dd1">
[experimental] delete indentation of HTML file and eliminate page size.
</dd>
</dl>
<p>If you required 'erubis/engine/enhanced', Eruby subclasses which include each enhancers are defined.
For example, class BiPatternEruby includes BiPatternEnhancer.
</p>
<a name="escape-enhancer"></a>
<h3 class="section2">EscapeEnhancer</h3>
<p>EscapeEnhancer switches '&lt;%= ... %&gt;' to escaped and '&lt;%== ... %&gt;' to unescaped.
</p>
<a name="example.eruby"></a>
<div class="program_caption">
example.eruby</div>
<pre class="program">&lt;div&gt;
&lt;% for item in list %&gt;
&lt;p&gt;&lt;%= item %&gt;&lt;/p&gt;
&lt;p&gt;&lt;%== item %&gt;&lt;/p&gt;
&lt;% end %&gt;
&lt;/div&gt;
</pre>
<a name="escape_example.result"></a>
<div class="terminal_caption">
compiled source code</div>
<pre class="terminal">$ erubis -xE Escape example.eruby
_buf = ''; _buf &lt;&lt; '&lt;div&gt;
'; for item in list
_buf &lt;&lt; ' &lt;p&gt;'; <strong>_buf &lt;&lt; Erubis::XmlHelper.escape_xml( item );</strong> _buf &lt;&lt; '&lt;/p&gt;
&lt;p&gt;'; <strong>_buf &lt;&lt; ( item ).to_s;</strong> _buf &lt;&lt; '&lt;/p&gt;
'; end
_buf &lt;&lt; '&lt;/div&gt;
';
_buf.to_s
</pre>
<p>EscapeEnhancer is language-independent.
</p>
<br>
<a name="stdout-enhancer"></a>
<h3 class="section2">StdoutEnhancer</h3>
<p>StdoutEnhancer use $sdtdout instead of array buffer.
Therefore, you can use 'print' statement in embedded ruby code.
</p>
<a name="stdout_exmple.result"></a>
<div class="terminal_caption">
compiled source code</div>
<pre class="terminal">$ erubis -xE Stdout example.eruby
<strong>_buf = $stdout;</strong> _buf &lt;&lt; '&lt;div&gt;
'; for item in list
_buf &lt;&lt; ' &lt;p&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/p&gt;
&lt;p&gt;'; _buf &lt;&lt; Erubis::XmlHelper.escape_xml( item ); _buf &lt;&lt; '&lt;/p&gt;
'; end
_buf &lt;&lt; '&lt;/div&gt;
';
<strong>''</strong>
</pre>
<p>StdoutEnhancer is only for Eruby.
</p>
<br>
<a name="printout-enhancer"></a>
<h3 class="section2">PrintOutEnhancer</h3>
<p>PrintOutEnhancer makes compiled source code to use 'print(...)' instead of '_buf &lt;&lt; ...'.
</p>
<a name="printstatement_example.result"></a>
<div class="terminal_caption">
compiled source code</div>
<pre class="terminal">$ erubis -xE PrintOut example.eruby
<strong>print</strong> '&lt;div&gt;
'; for item in list
<strong>print</strong> ' &lt;p&gt;'; <strong>print</strong>(( item ).to_s); <strong>print</strong> '&lt;/p&gt;
&lt;p&gt;'; <strong>print</strong> Erubis::XmlHelper.escape_xml( item ); <strong>print</strong> '&lt;/p&gt;
'; end
<strong>print</strong> '&lt;/div&gt;
';
</pre>
<p>PrintOutEnhancer is only for Eruby.
</p>
<br>
<a name="printenabled-enhancer"></a>
<h3 class="section2">PrintEnabledEnhancer</h3>
<p>PrintEnabledEnhancer enables you to use print() method in '&lt;% ... %&gt;'.
</p>
<a name="printenabled-example.eruby"></a>
<div class="program_caption">
printenabled-example.eruby</div>
<pre class="program">&lt;% for item in @list %&gt;
&lt;b&gt;<strong>&lt;% print item %&gt;</strong>&lt;/b&gt;
&lt;% end %&gt;
</pre>
<a name="printenabled-example.rb"></a>
<div class="program_caption">
printenabled-example.rb</div>
<pre class="program">require 'erubis'
class PrintEnabledEruby &lt; Erubis::Eruby
include Erubis::PrintEnabledEnhancer
end
input = File.read('printenabled-example.eruby')
eruby = PrintEnabledEruby.new(input)
list = ['aaa', 'bbb', 'ccc']
print eruby.evaluate(:list=&gt;list)
</pre>
<a name="printenable_example.result"></a>
<div class="terminal_caption">
output result</div>
<pre class="terminal">$ ruby printenabled-example.rb
&lt;b&gt;aaa&lt;/b&gt;
&lt;b&gt;bbb&lt;/b&gt;
&lt;b&gt;ccc&lt;/b&gt;
</pre>
<p>Notice to use Eruby#evaluate() and not to use Eruby#result(),
because print() method in '&lt;% ... %&gt;' invokes not Kernel#print() but PrintEnabledEnhancer#print().
</p>
<p>PrintEnabledEnhancer is only for Eruby.
</p>
<br>
<a name="array-enhancer"></a>
<h3 class="section2">ArrayEnhancer</h3>
<p>ArrayEnhancer makes Eruby to return an array of strings.
</p>
<a name="array_example.result"></a>
<div class="terminal_caption">
compiled source code</div>
<pre class="terminal">$ erubis -xE Array example.eruby
<strong>_buf = [];</strong> _buf &lt;&lt; '&lt;div&gt;
'; for item in list
_buf &lt;&lt; ' &lt;p&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/p&gt;
&lt;p&gt;'; _buf &lt;&lt; Erubis::XmlHelper.escape_xml( item ); _buf &lt;&lt; '&lt;/p&gt;
'; end
_buf &lt;&lt; '&lt;/div&gt;
';
<strong>_buf</strong>
</pre>
<p>ArrayEnhancer is only for Eruby.
</p>
<br>
<a name="arraybuffer-enhancer"></a>
<h3 class="section2">ArrayBufferEnhancer</h3>
<p>ArrayBufferEnhancer makes Eruby to use array buffer.
Array buffer is a litte slower than String buffer.
</p>
<p>ArrayBufferEnhancer is only for Eruby.
</p>
<a name="arraybuffer_example.result"></a>
<div class="terminal_caption">
compiled source code</div>
<pre class="terminal">$ erubis -xE ArrayBuffer example.eruby
<strong>_buf = [];</strong> _buf &lt;&lt; '&lt;div&gt;
'; for item in list
_buf &lt;&lt; ' &lt;p&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/p&gt;
&lt;p&gt;'; _buf &lt;&lt; Erubis::XmlHelper.escape_xml( item ); _buf &lt;&lt; '&lt;/p&gt;
'; end
_buf &lt;&lt; '&lt;/div&gt;
';
<strong>_buf.join</strong>
</pre>
<br>
<a name="stringbuffer-enhancer"></a>
<h3 class="section2">StringBufferEnhancer</h3>
<p>StringBufferEnhancer makes Eruby to use string buffer.
String buffer is a little faster than array buffer.
Erubis::Eruby includes this enhancer by default.
</p>
<a name="stringbuffer_example.result"></a>
<div class="terminal_caption">
compiled source code</div>
<pre class="terminal">$ erubis -xE StringBuffer example.eruby
<strong>_buf = '';</strong> _buf &lt;&lt; '&lt;div&gt;
'; for item in list
_buf &lt;&lt; ' &lt;p&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/p&gt;
&lt;p&gt;'; _buf &lt;&lt; Erubis::XmlHelper.escape_xml( item ); _buf &lt;&lt; '&lt;/p&gt;
'; end
_buf &lt;&lt; '&lt;/div&gt;
';
<strong>_buf.to_s</strong>
</pre>
<p>StringBufferEnhancer is only for Eruby.
</p>
<br>
<a name="erbout-enhancer"></a>
<h3 class="section2">ErboutEnhancer</h3>
<p>ErboutEnhancer makes Eruby to be compatible with ERB.
This is useful especially for Ruby on Rails.
</p>
<div class="terminal_caption">
compiled source code</div>
<pre class="terminal">$ erubis -xE Erbout example.eruby
<strong>_erbout = _buf = '';</strong> _buf &lt;&lt; '&lt;div&gt;
'; for item in list
_buf &lt;&lt; ' &lt;p&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/p&gt;
&lt;p&gt;'; _buf &lt;&lt; Erubis::XmlHelper.escape_xml( item ); _buf &lt;&lt; '&lt;/p&gt;
'; end
_buf &lt;&lt; '&lt;/div&gt;
';
_buf.to_s
</pre>
<p>ErboutEnhancer is only for Eruby.
</p>
<br>
<a name="notext-enhancer"></a>
<h3 class="section2">NoTextEnhancer</h3>
<p>NoTextEnhancer suppress output of text and prints only embedded code.
This is useful especially when debugging a complex eRuby script.
</p>
<a name="notext-example.eruby"></a>
<div class="program_caption">
notext-example.eruby</div>
<pre class="program">&lt;h3&gt;List&lt;/h3&gt;
&lt;% if !@list || @list.empty? %&gt;
&lt;p&gt;not found.&lt;/p&gt;
&lt;% else %&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;% @list.each_with_index do |item, i| %&gt;
&lt;tr bgcolor="&lt;%= i%2 == 0 ? '#FFCCCC' : '#CCCCFF' %&gt;"&gt;
&lt;td&gt;&lt;%= item %&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;% end %&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;% end %&gt;
</pre>
<a name="notext_example.result"></a>
<div class="terminal_caption">
output example of NoTextEnhancer</div>
<pre class="terminal">$ erubis -xE NoText notext-example.eruby
_buf = '';
if !@list || @list.empty?
else
@list.each_with_index do |item, i|
_buf &lt;&lt; ( i%2 == 0 ? '#FFCCCC' : '#CCCCFF' ).to_s;
_buf &lt;&lt; ( item ).to_s;
end
end
_buf.to_s
</pre>
<p>NoTextEnhancer is language-independent. It is useful even if you are PHP user, see <a href="#topics-php">this section</a>.
</p>
<br>
<a name="nocode-enhancer"></a>
<h3 class="section2">NoCodeEnhancer</h3>
<p>NoCodeEnhancer suppress output of embedded code and prints only normal text.
This is useful especially when validating HTML tags.
</p>
<a name="nocode-example.eruby"></a>
<div class="program_caption">
nocode-example.eruby</div>
<pre class="program">&lt;h3&gt;List&lt;/h3&gt;
&lt;% if !@list || @list.empty? %&gt;
&lt;p&gt;not found.&lt;/p&gt;
&lt;% else %&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;% @list.each_with_index do |item, i| %&gt;
&lt;tr bgcolor="&lt;%= i%2 == 0 ? '#FFCCCC' : '#CCCCFF' %&gt;"&gt;
&lt;td&gt;&lt;%= item %&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;% end %&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;% end %&gt;
</pre>
<a name="nocode_example.result"></a>
<div class="terminal_caption">
output example of NoCodeEnhancer</div>
<pre class="terminal">$ erubis -xE NoCode notext-example.eruby
&lt;h3&gt;List&lt;/h3&gt;
&lt;p&gt;not found.&lt;/p&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr bgcolor=""&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
</pre>
<p>NoCodeEnhancer is language-independent. It is useful even if you are PHP user, see <a href="#topics-php">this section</a>.
</p>
<br>
<a name="simplify-enhancer"></a>
<h3 class="section2">SimplifyEnhancer</h3>
<p>SimplifyEnhancer makes compiling a little faster but don't trim spaces around '&lt;% %&gt;'.
</p>
<a name="simplify_example.result"></a>
<div class="terminal_caption">
compiled source code</div>
<pre class="terminal">$ erubis -xE Simplify example.eruby
_buf = ''; _buf &lt;&lt; '&lt;div&gt;
'; for item in list ; _buf &lt;&lt; '
&lt;p&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/p&gt;
&lt;p&gt;'; _buf &lt;&lt; Erubis::XmlHelper.escape_xml( item ); _buf &lt;&lt; '&lt;/p&gt;
'; end ; _buf &lt;&lt; '
&lt;/div&gt;
';
_buf.to_s
</pre>
<p>SimplifyEnhancer is language-independent.
</p>
<br>
<a name="bipattern-enhancer"></a>
<h3 class="section2">BiPatternEnhancer</h3>
<p>BiPatternEnhancer enables to use another embedded pattern with '&lt;% %&gt;'.
By Default, '[= ... =]' is available for expression.
You can specify pattern by :bipattern property.
</p>
<a name="bipattern-example.rhtml"></a>
<div class="program_caption">
bipattern-example.rhtml</div>
<pre class="program">&lt;% for item in list %&gt;
&lt;b&gt;<strong>[= item =]</strong>&lt;/b&gt;
&lt;b&gt;<strong>[== item =]</strong>&lt;/b&gt;
&lt;% end %&gt;
</pre>
<a name="bipattern_example.result"></a>
<div class="terminal_caption">
compiled source code</div>
<pre class="terminal">$ erubis -xE BiPattern bipattern-example.rhtml
_buf = ''; for item in list
_buf &lt;&lt; ' &lt;b&gt;'; <strong>_buf &lt;&lt; ( item ).to_s;</strong> _buf &lt;&lt; '&lt;/b&gt;
&lt;b&gt;'; <strong>_buf &lt;&lt; Erubis::XmlHelper.escape_xml( item );</strong> _buf &lt;&lt; '&lt;/b&gt;
'; end
_buf.to_s
</pre>
<p>BiPatternEnhancer is language-independent.
</p>
<br>
<a name="percentline-enhancer"></a>
<h3 class="section2">PercentLineEnhancer</h3>
<p>PercentLineEnhancer regards lines starting with '%' as Ruby code.
This is for compatibility with eruby and ERB.
</p>
<a name="percentline-example.rhtml"></a>
<div class="program_caption">
percentline-example.rhtml</div>
<pre class="program"><strong>% for item in list</strong>
&lt;b&gt;&lt;%= item %&gt;&lt;/b&gt;
<strong>% end</strong>
%% lines with '%%'
</pre>
<a name="percentline_example.result"></a>
<div class="terminal_caption">
compiled source code</div>
<pre class="terminal">$ erubis -xE PercentLine percentline-example.rhtml
_buf = ''; <strong>for item in list</strong>
_buf &lt;&lt; ' &lt;b&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/b&gt;
'; <strong>end</strong>
_buf &lt;&lt; '% lines with \'%%\'
';
_buf.to_s
</pre>
<p>PercentLineEnhancer is language-independent.
</p>
<br>
<a name="headerfooter-enhancer"></a>
<h3 class="section2">HeaderFooterEnhancer</h3>
<p>[experimental]
</p>
<p>HeaderFooterEnhancer enables you to add header and footer in eRuby script.
</p>
<a name="headerfooter-example.eruby"></a>
<div class="program_caption">
headerfooter-example.eruby</div>
<pre class="program"><strong>&lt;!--#header:</strong>
<strong>def list_items(items)</strong>
<strong>#--&gt;</strong>
&lt;% for item in items %&gt;
&lt;b&gt;&lt;%= item %&gt;&lt;/b&gt;
&lt;% end %&gt;
<strong>&lt;!--#footer:</strong>
<strong>end</strong>
<strong>#--&gt;</strong>
</pre>
<a name="headerfooter_example.result"></a>
<div class="terminal_caption">
compiled source code</div>
<pre class="terminal">$ erubis -xE HeaderFooter headerfooter-example.eruby
<strong>def list_items(items)</strong>
_buf = ''; for item in items
_buf &lt;&lt; ' &lt;b&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/b&gt;
'; end
_buf.to_s
<strong>end</strong>
</pre>
<p>Compare to the following:
</p>
<a name="normal-eruby-test.eruby"></a>
<div class="program_caption">
normal-eruby-test.eruby</div>
<pre class="program"><strong>&lt;%</strong>
<strong>def list_items(items)</strong>
<strong>%&gt;</strong>
&lt;% for item in items %&gt;
&lt;li&gt;&lt;%= item %&gt;&lt;/li&gt;
&lt;% end %&gt;
<strong>&lt;%</strong>
<strong>end</strong>
<strong>%&gt;</strong>
</pre>
<a name="normal_eruby_test.result"></a>
<div class="terminal_caption">
compiled source code</div>
<pre class="terminal">$ erubis -x normal-eruby-test.eruby
_buf = '';
<strong>def list_items(items)</strong>
for item in items
_buf &lt;&lt; '&lt;li&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/li&gt;
'; end
<strong>end</strong>
_buf.to_s
</pre>
<p>Header and footer can be in any position in eRuby script,
that is, header is no need to be in the head of eRuby script.
</p>
<a name="headerfooter-example2.rhtml"></a>
<div class="program_caption">
headerfooter-example2.rhtml</div>
<pre class="program">&lt;?xml version="1.0"?&gt;
&lt;html&gt;
<strong>&lt;!--#header:</strong>
<strong>def page(list)</strong>
<strong>#--&gt;</strong>
:
<strong>&lt;!--#footer:</strong>
<strong>end</strong>
<strong>#--&gt;</strong>
&lt;/html&gt;
</pre>
<a name="headerfooter_example2.result"></a>
<div class="terminal_caption">
compiled source code</div>
<pre class="terminal">$ erubis -xE HeaderFooter headerfooter-example2.rhtml
<strong>def page(list)</strong>
_buf = ''; _buf &lt;&lt; '&lt;?xml version="1.0"?&gt;
&lt;html&gt;
'; _buf &lt;&lt; ' :
'; _buf &lt;&lt; '&lt;/html&gt;
';
_buf.to_s
<strong>end</strong>
</pre>
<p>HeaderFooterEnhancer is experimental and is language-independent.
</p>
<br>
<a name="interpolation-enhancer"></a>
<h3 class="section2">InterpolationEnhancer</h3>
<p>[experimental]
</p>
<p>InterpolationEnhancer converts "&lt;h1&gt;&lt;%= title %&gt;&lt;/h1&gt;" into
"_buf &lt;&lt; %Q`&lt;h1&gt;#{ title }&lt;/h1&gt;`".
This makes Eruby a litter faster because method call of String#&lt;&lt; are eliminated
by expression interpolations.
</p>
<div class="program_caption">
InterpolationEnhancer elmininates method call of String#&lt;&lt;.</div>
<pre class="program">## Assume that input is '&lt;a href="&lt;%=url%&gt;"&gt;&lt;%=name%&gt;&lt;/a&gt;'.
## Eruby convert input into the following code. String#&lt;&lt; is called 5 times.
_buf &lt;&lt; '&lt;a href="'; _buf &lt;&lt; (url).to_s; _buf &lt;&lt; '"&gt;'; _buf &lt;&lt; (name).to_s; _buf &lt;&lt; '&lt;/a&gt;';
## If InterpolationEnhancer is used, String#&lt;&lt; is called only once.
_buf &lt;&lt; %Q`&lt;a href="#{url}"&gt;#{name}&lt;/a&gt;`;
</pre>
<a name="interpolation_example.result"></a>
<div class="terminal_caption">
compiled source code</div>
<pre class="terminal">$ erubis -xE Interpolation example.eruby
_buf = ''; _buf &lt;&lt; <strong>%Q`</strong>&lt;div&gt;\n<strong>`</strong>
for item in list
_buf &lt;&lt; <strong>%Q`</strong> &lt;p&gt;<strong>#{ item }</strong>&lt;/p&gt;
&lt;p&gt;<strong>#{Erubis::XmlHelper.escape_xml( item )}</strong>&lt;/p&gt;\n<strong>`</strong>
end
_buf &lt;&lt; <strong>%Q`</strong>&lt;/div&gt;\n<strong>`</strong>
_buf.to_s
</pre>
<p>Erubis provides Erubis::FastEruby class which includes InterpolationEnhancer.
You can use Erubis::FastEruby class instead of Erubis::Eruby class.
</p>
<p>InterpolationEnhancer is only for Eruby.
</p>
<br>
<a name="deleteindent-enhancer"></a>
<h3 class="section2">DeleteIndentEnhancer</h3>
<p>[experimental]
DeleteIndentEnhancer deletes indentation of HTML file.
</p>
<a name="interpolation_example.result"></a>
<div class="terminal_caption">
compiled source code</div>
<pre class="terminal">$ erubis -xE DeleteIndent example.eruby
_buf = ''; _buf &lt;&lt; '&lt;div&gt;
'; for item in list
_buf &lt;&lt; '&lt;p&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/p&gt;
&lt;p&gt;'; _buf &lt;&lt; Erubis::XmlHelper.escape_xml( item ); _buf &lt;&lt; '&lt;/p&gt;
'; end
_buf &lt;&lt; '&lt;/div&gt;
';
_buf.to_s
</pre>
<p>Notice that DeleteIndentEnhancer isn't intelligent.
It deletes indentations even if they are in &lt;PRE&gt;&lt;/PRE&gt;.
</p>
<p>DeleteIndentEnhancer is language-independent.
</p>
<br>
<br>
<a name="lang"></a>
<h2 class="section1">Multi-Language Support</h2>
<p>Erubis supports the following languages<sup>(<a href="#fnref:2" name="fnlink:2">*2</a>)</sup>:
</p>
<ul type="disc">
<li>Ruby
</li>
<li><a href="#lang-php">PHP</a>
</li>
<li><a href="#lang-c">C</a>
</li>
<li><a href="#lang-java">Java</a>
</li>
<li><a href="#lang-scheme">Scheme</a>
</li>
<li><a href="#lang-perl">Perl</a>
</li>
<li><a href="#lang-javascript">JavaScript</a>
</li>
</ul>
<div class="footnote">
<dl compact>
<dt>(<a name="fnref:2" href="#fnlink:2">*2</a>)</dt>
<dd>If you need template engine in pure PHP/Perl/JavaScript, try <a href="http://www.kuwata-lab.com/tenjin/">Tenjin</a> (<a href="http://www.kuwata-lab.com/tenjin/">http://www.kuwata-lab.com/tenjin/</a>). Tenjin is a very fast and full-featured template engine implemented in pure PHP/Perl/JavaScript.</dd>
</dl>
</div>
<a name="lang-php"></a>
<h3 class="section2">PHP</h3>
<a name="example.ephp"></a>
<div class="program_caption">
example.ephp</div>
<pre class="program">&lt;?xml version="1.0"?&gt;
&lt;html&gt;
&lt;body&gt;
&lt;p&gt;Hello <strong>&lt;%= $user %&gt;</strong>!&lt;/p&gt;
&lt;table&gt;
&lt;tbody&gt;
<strong>&lt;% $i = 0; %&gt;</strong>
<strong>&lt;% foreach ($list as $item) { %&gt;</strong>
<strong>&lt;% $i++; %&gt;</strong>
&lt;tr bgcolor=<strong>"&lt;%= $i % 2 == 0 ? '#FFCCCC' : '#CCCCFF' %&gt;"</strong>&gt;
&lt;td&gt;<strong>&lt;%= $i %&gt;</strong>&lt;/td&gt;
&lt;td&gt;<strong>&lt;%== $item %&gt;</strong>&lt;/td&gt;
&lt;/tr&gt;
<strong>&lt;% } %&gt;</strong>
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<a name="example_php.result"></a>
<div class="terminal_caption">
compiled source code</div>
<pre class="terminal">$ erubis -l php example.ephp
&lt;&lt;?php ?&gt;?xml version="1.0"?&gt;
&lt;html&gt;
&lt;body&gt;
&lt;p&gt;Hello &lt;?php echo $user; ?&gt;!&lt;/p&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;?php $i = 0; ?&gt;
&lt;?php foreach ($list as $item) { ?&gt;
&lt;?php $i++; ?&gt;
&lt;tr bgcolor="&lt;?php echo $i % 2 == 0 ? '#FFCCCC' : '#CCCCFF'; ?&gt;"&gt;
&lt;td&gt;&lt;?php echo $i; ?&gt;&lt;/td&gt;
&lt;td&gt;&lt;?php echo htmlspecialchars($item); ?&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;?php } ?&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<br>
<a name="lang-c"></a>
<h3 class="section2">C</h3>
<a name="example.ec"></a>
<div class="program_caption">
example.ec</div>
<pre class="program"><strong>&lt;%
#include &lt;stdio.h&gt;
int main(int argc, char *argv[])
{
int i;
%&gt;</strong>
&lt;html&gt;
&lt;body&gt;
&lt;p&gt;Hello <strong>&lt;%= "%s", argv[0] %&gt;</strong>!&lt;/p&gt;
&lt;table&gt;
&lt;tbody&gt;
<strong>&lt;% for (i = 1; i &lt; argc; i++) { %&gt;</strong>
&lt;tr bgcolor="<strong>&lt;%= i % 2 == 0 ? "#FFCCCC" : "#CCCCFF" %&gt;</strong>"&gt;
&lt;td&gt;<strong>&lt;%= "%d", i %&gt;</strong>&lt;/td&gt;
&lt;td&gt;<strong>&lt;%= "%s", argv[i] %&gt;</strong>&lt;/td&gt;
&lt;/tr&gt;
<strong>&lt;% } %&gt;</strong>
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
<strong>&lt;%
return 0;
}
%&gt;</strong>
</pre>
<a name="example_c.result"></a>
<div class="terminal_caption">
compiled source code</div>
<pre class="terminal">$ erubis -l c example.ec
#line 1 "example.ec"
#include &lt;stdio.h&gt;
int main(int argc, char *argv[])
{
int i;
fputs("&lt;html&gt;\n"
" &lt;body&gt;\n"
" &lt;p&gt;Hello ", stdout); fprintf(stdout, "%s", argv[0]); fputs("!&lt;/p&gt;\n"
" &lt;table&gt;\n"
" &lt;tbody&gt;\n", stdout);
for (i = 1; i &lt; argc; i++) {
fputs(" &lt;tr bgcolor=\"", stdout); fprintf(stdout, i % 2 == 0 ? "#FFCCCC" : "#CCCCFF"); fputs("\"&gt;\n"
" &lt;td&gt;", stdout); fprintf(stdout, "%d", i); fputs("&lt;/td&gt;\n"
" &lt;td&gt;", stdout); fprintf(stdout, "%s", argv[i]); fputs("&lt;/td&gt;\n"
" &lt;/tr&gt;\n", stdout);
}
fputs(" &lt;/tbody&gt;\n"
" &lt;/table&gt;\n"
" &lt;/body&gt;\n"
"&lt;/html&gt;\n", stdout);
return 0;
}
</pre>
<br>
<a name="lang-java"></a>
<h3 class="section2">Java</h3>
<a name="Example.ejava"></a>
<div class="program_caption">
Example.ejava</div>
<pre class="program"><strong>&lt;%
import java.util.*;
public class Example {
private String user;
private String[] list;
public example(String user, String[] list) {
this.user = user;
this.list = list;
}
public String view() {
StringBuffer _buf = new StringBuffer();
%&gt;</strong>
&lt;html&gt;
&lt;body&gt;
&lt;p&gt;Hello <strong>&lt;%= user %&gt;</strong>!&lt;/p&gt;
&lt;table&gt;
&lt;tbody&gt;
<strong>&lt;% for (int i = 0; i &lt; list.length; i++) { %&gt;</strong>
&lt;tr bgcolor=<strong>"&lt;%= i % 2 == 0 ? "#FFCCCC" : "#CCCCFF" %&gt;"</strong>&gt;
&lt;td&gt;<strong>&lt;%= i + 1 %&gt;</strong>&lt;/td&gt;
&lt;td&gt;<strong>&lt;%== list[i] %&gt;</strong>&lt;/td&gt;
&lt;/tr&gt;
<strong>&lt;% } %&gt;</strong>
&lt;/tbody&gt;
&lt;/table&gt;
&lt;body&gt;
&lt;/html&gt;
<strong>&lt;%
return _buf.toString();
}
public static void main(String[] args) {
String[] list = { "&lt;aaa&gt;", "b&amp;b", "\"ccc\"" };
Example ex = Example.new("Erubis", list);
System.out.print(ex.view());
}
public static String escape(String s) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i &lt; s.length(); i++) {
char ch = s.charAt(i);
switch (ch) {
case '&lt;': sb.append("&amp;lt;"); break;
case '&gt;': sb.append("&amp;gt;"); break;
case '&amp;': sb.append("&amp;amp;"); break;
case '"': sb.append("&amp;quot;"); break;
default: sb.append(ch);
}
}
return sb.toString();
}
}
%&gt;</strong>
</pre>
<a name="example_java.result"></a>
<div class="terminal_caption">
compiled source code</div>
<pre class="terminal">$ erubis -b -l java example.ejava
import java.util.*;
public class Example {
private String user;
private String[] list;
public example(String user, String[] list) {
this.user = user;
this.list = list;
}
public String view() {
StringBuffer _buf = new StringBuffer();
_buf.append("&lt;html&gt;\n"
+ " &lt;body&gt;\n"
+ " &lt;p&gt;Hello "); _buf.append(user); _buf.append("!&lt;/p&gt;\n"
+ " &lt;table&gt;\n"
+ " &lt;tbody&gt;\n");
for (int i = 0; i &lt; list.length; i++) {
_buf.append(" &lt;tr bgcolor=\""); _buf.append(i % 2 == 0 ? "#FFCCCC" : "#CCCCFF"); _buf.append("\"&gt;\n"
+ " &lt;td&gt;"); _buf.append(i + 1); _buf.append("&lt;/td&gt;\n"
+ " &lt;td&gt;"); _buf.append(escape(list[i])); _buf.append("&lt;/td&gt;\n"
+ " &lt;/tr&gt;\n");
}
_buf.append(" &lt;/tbody&gt;\n"
+ " &lt;/table&gt;\n"
+ " &lt;body&gt;\n"
+ "&lt;/html&gt;\n");
return _buf.toString();
}
public static void main(String[] args) {
String[] list = { "&lt;aaa&gt;", "b&amp;b", "\"ccc\"" };
Example ex = Example.new("Erubis", list);
System.out.print(ex.view());
}
public static String escape(String s) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i &lt; s.length(); i++) {
char ch = s.charAt(i);
switch (ch) {
case '&lt;': sb.append("&amp;lt;"); break;
case '&gt;': sb.append("&amp;gt;"); break;
case '&amp;': sb.append("&amp;amp;"); break;
case '"': sb.append("&amp;quot;"); break;
default: sb.append(ch);
}
}
return sb.toString();
}
}
</pre>
<br>
<a name="lang-scheme"></a>
<h3 class="section2">Scheme</h3>
<a name="example.escheme"></a>
<div class="program_caption">
example.escheme</div>
<pre class="program">&lt;html&gt;
&lt;body&gt;
<strong>&lt;%
(let ((user "Erubis")
(items '("&lt;aaa&gt;" "b&amp;b" "\"ccc\""))
(i 0))
%&gt;</strong>
&lt;p&gt;Hello <strong>&lt;%= user %&gt;</strong>!&lt;/p&gt;
&lt;table&gt;
<strong>&lt;%
(for-each
(lambda (item)
(set! i (+ i 1))
%&gt;</strong>
&lt;tr bgcolor="<strong>&lt;%= (if (= (modulo i 2) 0) "#FFCCCC" "#CCCCFF") %&gt;</strong>"&gt;
&lt;td&gt;<strong>&lt;%= i %&gt;</strong>&lt;/td&gt;
&lt;td&gt;<strong>&lt;%= item %&gt;</strong>&lt;/td&gt;
&lt;/tr&gt;
<strong>&lt;%
) ; lambda end
items) ; for-each end
%&gt;</strong>
&lt;/table&gt;
<strong>&lt;%
) ; let end
%&gt;</strong>
&lt;/body&gt;
&lt;/html&gt;
</pre>
<a name="example_scheme.result"></a>
<div class="terminal_caption">
compiled source code</div>
<pre class="terminal">$ erubis -l scheme example.escheme
(let ((_buf '())) (define (_add x) (set! _buf (cons x _buf))) (_add "&lt;html&gt;
&lt;body&gt;\n")
(let ((user "Erubis")
(items '("&lt;aaa&gt;" "b&amp;b" "\"ccc\""))
(i 0))
(_add " &lt;p&gt;Hello ")(_add user)(_add "!&lt;/p&gt;
&lt;table&gt;\n")
(for-each
(lambda (item)
(set! i (+ i 1))
(_add " &lt;tr bgcolor=\"")(_add (if (= (modulo i 2) 0) "#FFCCCC" "#CCCCFF"))(_add "\"&gt;
&lt;td&gt;")(_add i)(_add "&lt;/td&gt;
&lt;td&gt;")(_add item)(_add "&lt;/td&gt;
&lt;/tr&gt;\n")
) ; lambda end
items) ; for-each end
(_add " &lt;/table&gt;\n")
) ; let end
(_add " &lt;/body&gt;
&lt;/html&gt;\n")
(reverse _buf))
</pre>
<a name="example_scheme_display.result"></a>
<div class="terminal_caption">
compiled source code (with <code>--func=display</code> property)</div>
<pre class="terminal">$ erubis -l scheme --func=display example.escheme
(display "&lt;html&gt;
&lt;body&gt;\n")
(let ((user "Erubis")
(items '("&lt;aaa&gt;" "b&amp;b" "\"ccc\""))
(i 0))
(display " &lt;p&gt;Hello ")(display user)(display "!&lt;/p&gt;
&lt;table&gt;\n")
(for-each
(lambda (item)
(set! i (+ i 1))
(display " &lt;tr bgcolor=\"")(display (if (= (modulo i 2) 0) "#FFCCCC" "#CCCCFF"))(display "\"&gt;
&lt;td&gt;")(display i)(display "&lt;/td&gt;
&lt;td&gt;")(display item)(display "&lt;/td&gt;
&lt;/tr&gt;\n")
) ; lambda end
items) ; for-each end
(display " &lt;/table&gt;\n")
) ; let end
(display " &lt;/body&gt;
&lt;/html&gt;\n")
</pre>
<br>
<a name="lang-perl"></a>
<h3 class="section2">Perl</h3>
<a name="example.eperl"></a>
<div class="program_caption">
example.eprl</div>
<pre class="program"><strong>&lt;%
my $user = 'Erubis';
my @list = ('&lt;aaa&gt;', 'b&amp;b', '"ccc"');
%&gt;</strong>
&lt;html&gt;
&lt;body&gt;
&lt;p&gt;Hello <strong>&lt;%= $user %&gt;</strong>!&lt;/p&gt;
&lt;table&gt;
<strong>&lt;% $i = 0; %&gt;</strong>
<strong>&lt;% for $item (@list) { %&gt;</strong>
&lt;tr bgcolor=<strong>&lt;%= ++$i % 2 == 0 ? '#FFCCCC' : '#CCCCFF' %&gt;</strong>"&gt;
&lt;td&gt;<strong>&lt;%= $i %&gt;</strong>&lt;/td&gt;
&lt;td&gt;<strong>&lt;%= $item %&gt;</strong>&lt;/td&gt;
&lt;/tr&gt;
<strong>&lt;% } %&gt;</strong>
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<a name="example_perl.result"></a>
<div class="terminal_caption">
compiled source code</div>
<pre class="terminal">$ erubis -l perl example.eperl
use HTML::Entities;
my $user = 'Erubis';
my @list = ('&lt;aaa&gt;', 'b&amp;b', '"ccc"');
print('&lt;html&gt;
&lt;body&gt;
&lt;p&gt;Hello '); print($user); print('!&lt;/p&gt;
&lt;table&gt;
'); $i = 0;
for $item (@list) {
print(' &lt;tr bgcolor='); print(++$i % 2 == 0 ? '#FFCCCC' : '#CCCCFF'); print('"&gt;
&lt;td&gt;'); print($i); print('&lt;/td&gt;
&lt;td&gt;'); print($item); print('&lt;/td&gt;
&lt;/tr&gt;
'); }
print(' &lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
');
</pre>
<br>
<a name="lang-javascript"></a>
<h3 class="section2">JavaScript</h3>
<a name="example.ejs"></a>
<div class="program_caption">
example.ejs</div>
<pre class="program"><strong>&lt;%
var user = 'Erubis';
var list = ['&lt;aaa&gt;', 'b&amp;b', '"ccc"'];
%&gt;</strong>
&lt;html&gt;
&lt;body&gt;
&lt;p&gt;Hello <strong>&lt;%= user %&gt;</strong>!&lt;/p&gt;
&lt;table&gt;
&lt;tbody&gt;
<strong>&lt;% var i; %&gt;</strong>
<strong>&lt;% for (i = 0; i &lt; list.length; i++) { %&gt;</strong>
&lt;tr bgcolor="<strong>&lt;%= i % 2 == 0 ? '#FFCCCC' : '#CCCCFF' %&gt;</strong>"&gt;
&lt;td&gt;<strong>&lt;%= i + 1 %&gt;</strong>&lt;/td&gt;
&lt;td&gt;<strong>&lt;%= list[i] %&gt;</strong>&lt;/td&gt;
&lt;/tr&gt;
<strong>&lt;% } %&gt;</strong>
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<a name="example_js.result"></a>
<div class="terminal_caption">
compiled source code</div>
<pre class="terminal">$ erubis -l js example.ejs
var _buf = [];
var user = 'Erubis';
var list = ['&lt;aaa&gt;', 'b&amp;b', '"ccc"'];
_buf.push("&lt;html&gt;\n\
&lt;body&gt;\n\
&lt;p&gt;Hello "); _buf.push(user); _buf.push("!&lt;/p&gt;\n\
&lt;table&gt;\n\
&lt;tbody&gt;\n");
var i;
for (i = 0; i &lt; list.length; i++) {
_buf.push(" &lt;tr bgcolor=\""); _buf.push(i % 2 == 0 ? '#FFCCCC' : '#CCCCFF'); _buf.push("\"&gt;\n\
&lt;td&gt;"); _buf.push(i + 1); _buf.push("&lt;/td&gt;\n\
&lt;td&gt;"); _buf.push(list[i]); _buf.push("&lt;/td&gt;\n\
&lt;/tr&gt;\n");
}
_buf.push(" &lt;/tbody&gt;\n\
&lt;/table&gt;\n\
&lt;/body&gt;\n\
&lt;/html&gt;\n");
document.write(_buf.join(""));
</pre>
<p>If command-line option '<code>--docwrite=false</code>' is specified,
'<code>_buf.join("");</code>' is used instead of '<code>document.write(_buf.join(""));</code>'.
This is useful when passing converted source code to eval() function in JavaScript.
</p>
<p>You can pass <code>:docwrite=&gt;false</code> to Erubis::Ejavascript.new() in your Ruby script.
</p>
<pre class="program">s = File.read('example.jshtml')
engine = Erubis::Ejavascript.new(s, <code>:docwrite=&gt;false</code>)
</pre>
<p>If you want to specify any JavaScript code, use '--postamble=...'.
</p>
<p>Notice that default value of 'docwrite' property will be false in the future release.
</p>
<br>
<br>
<a name="rails"></a>
<h2 class="section1">Ruby on Rails Support</h2>
<p>Erubis supports Ruby on Rails.
This section describes how to use Erubis with Ruby on Rails.
</p>
<a name="rails-settings"></a>
<h3 class="section2">Settings</h3>
<p>Add the following code to your 'config/environment.rb' and restart web server.
This replaces ERB in Rails by Erubis entirely.
</p>
<div class="program_caption">
config/environment.rb</div>
<pre class="program">require 'erubis/helpers/rails_helper'
#Erubis::Helpers::RailsHelper.engine_class = Erubis::Eruby # or Erubis::FastEruby
#Erubis::Helpers::RailsHelper.init_properties = {}
#Erubis::Helpers::RailsHelper.show_src = nil
#Erubis::Helpers::RailsHelper.preprocessing = false
</pre>
<p>Options:
</p>
<dl class="dl2">
<dt class="dt2">
Erubis::Helpers::RailsHelper.engine_class (=Erubis::Eruby)</dt>
<dd class="dd2">
<p> Erubis engine class (default Erubis::Eruby).
</p>
</dd>
<dt class="dt2">
Erubis::Helpers::RailsHelper.init_properties (={})</dt>
<dd class="dd2">
<p> Optional arguments for Erubis::Eruby#initialize() method (default {}).
</p>
</dd>
<dt class="dt2">
Erubis::Helpers::RailsHelper.show_src (=nil)</dt>
<dd class="dd2">
<p> Whether to print converted Ruby code into log file.
If true, Erubis prints coverted code into log file.
If false, Erubis doesn't.
If nil, Erubis prints when ENV['RAILS_ENV'] == 'development'.
Default is nil.
</p>
</dd>
<dt class="dt2">
Erubis::Helpers::RailsHelper.preprocessing (=false)</dt>
<dd class="dd2">
<p> Enable preprocessing if true (default false).
</p>
</dd>
</dl>
<br>
<a name="rails-preprocessing"></a>
<h3 class="section2">Preprosessing</h3>
<p>Erubis supports preprocessing of template files.
Preprocessing make your Ruby on Rails application about 20-40 percent faster.
To enable preprocessing, set Erubis::Helpers::RailsHelper.preprocessing to true in your 'environment.rb' file.
</p>
<p>For example, assume the following template.
This is slow because link_to() method is called every time when template is rendered.
</p>
<pre class="program">&lt;%= link_to 'Create', :action=&gt;'create' %&gt;
</pre>
<p>The following is faster than the above, but not flexible because url is fixed.
</p>
<pre class="program">&lt;a href="/users/create"&gt;Create&lt;/a&gt;
</pre>
<p>Preprocessing solves this problem.
If you use '[%= %]' instead of '&lt;%= %&gt;', preprocessor evaluate it only once when template is loaded.
</p>
<pre class="program"><strong>[%= link_to 'Create', :action=&gt;'create'%]</strong>
</pre>
<p>The above is evaluated by preprocessor and replaced to the following code automatically.
</p>
<pre class="program">&lt;a href="/users/create"&gt;Create&lt;/a&gt;
</pre>
<p>Notice that this is done only once when template file is loaded.
It means that link_to() method is not called when template is rendered.
</p>
<p>If link_to() method have variable arguments, use <code>_?()</code> helper method.
</p>
<pre class="program">&lt;% for user in @users %&gt;
[%= link_to <strong>_?('user.name')</strong>, :action=&gt;'show', :id=&gt;<strong>_?('user.id')</strong> %]
&lt;% end %&gt;
</pre>
<p>The above is evaluated by preprocessor when template is loaded and expanded into the following code.
This will be much faster because link_to() method is not called when rendering.
</p>
<pre class="program">&lt;% for user in @users %&gt;
&lt;a href="/users/show/<strong>&lt;%=user.id%&gt;</strong>"&gt;<strong>&lt;%=user.name%&gt;</strong>&lt;/a&gt;
&lt;% end %&gt;
</pre>
<p>Preprocessing statement (<code>[% %]</code>) is also available as well as preprocessing expression (<code>[%= %]</code>).
</p>
<pre class="program">&lt;select name="state"&gt;
&lt;option value=""&gt;-&lt;/option&gt;
<strong>[% for code in states.keys.sort %]</strong>
&lt;option value="<strong>[%= code %]</strong>"&gt;<strong>[%= states[code] %]</strong>&lt;/option&gt;
<strong>[% end %]</strong>
&lt;/select&gt;
</pre>
<p>The above will be evaluated by preprocessor and expanded into the following when template is loaded.
In the result, rendering speed will be much faster because for-loop is not executed when rendering.
</p>
<pre class="program">&lt;select name="state"&gt;
&lt;option value=""&gt;-&lt;/option&gt;
&lt;option value="AK"&gt;Alaska&lt;/option&gt;
&lt;option value="AL"&gt;Alabama&lt;/option&gt;
&lt;option value="AR"&gt;Arkansas&lt;/option&gt;
&lt;option value="AS"&gt;American Samoa&lt;/option&gt;
&lt;option value="AZ"&gt;Arizona&lt;/option&gt;
&lt;option value="CA"&gt;California&lt;/option&gt;
&lt;option value="CO"&gt;Colorado&lt;/option&gt;
....
&lt;/select&gt;
</pre>
<p>Notice that it is not recommended to use preprocessing with tag helpers,
because tag helpers generate different html code when form parameter has errors or not.
</p>
<p>Helper methods of Ruby on Rails are divided into two groups.
</p>
<ul type="disc">
<li>link_to() or _() (method of gettext package) are not need to call for every time
as template is rendered because it returns same value when same arguments are passed.
These methods can be got faster by preprocessing.
</li>
<li>Tag helper methods should be called for every time as template is rendered
because it may return differrent value even if the same arguments are passed.
Preprocessing is not available with these methods.
</li>
</ul>
<p>In Ruby on Rails 2.0, <code>_?('user_id')</code> is OK but <code>_?('user.id')</code> is NG
because the latter contains period ('.') character.
</p>
<pre class="program">&lt;!-- NG in Rails 2.0, because _?('') contains period --&gt;
[%= link_to 'Edit', edit_user_path(<strong>_?('@user.id')</strong>) %]
[%= link_to 'Show', <strong>@user</strong> %]
[%= link_to 'Delete', <strong>@user</strong>, :confirm=&gt;'OK?', :method=&gt;:delete %]
&lt;!-- OK in Rails 2.0 --&gt;
<strong>&lt;%= user_id = @user.id %&gt;</strong>
[%= link_to 'Edit', edit_user_path(<strong>_?('user_id')</strong>) %]
[%= link_to 'Show', <strong>:action=&gt;'show', :id=&gt;_?('user_id')</strong> %]
[%= link_to 'Delete', <strong>{:action=&gt;'destroy', :id=&gt;_?('user_id')}</strong>,
{:confirm=&gt;'OK?', :method=&gt;:delete} %]
</pre>
<br>
<a name="rails-formhelpers"></a>
<h3 class="section2">Form Helpers for Preprocessing</h3>
<p><strong>(Experimental)</strong>
</p>
<p>Erubis provides form helper methods for preprocessing.
These are defined in 'erubis/helpers/rails_form_helper.rb'.
If you want to use it, require it and include Erubis::Helpers::RailsFormHelper in 'app/helpers/applition_helper.rb'
</p>
<div class="program_caption">
app/helpers/xxx_helper.rb</div>
<pre class="program">require 'erubis/helpers/rails_form_helper'
module ApplicationHelper
include Erubis::Helpers::RailsFormHelper
end
</pre>
<p>Form helper methods defined in Erubis::Helpers::RailsFormHelper are named as 'pp_xxxx'
('pp' represents preprocessing).
</p>
<p>Assume the following view template:
</p>
<div class="program_caption">
_form.rhtml</div>
<pre class="program"> &lt;p&gt;
Name: &lt;%= text_field :user, :name %&gt;
&lt;/p&gt;
&lt;p&gt;
Name: <strong>[%= pp_text_field :user, :name %]</strong>
&lt;/p&gt;
</pre>
<p>Erubis preprocessor converts it to the following eRuby string:
</p>
<div class="program_caption">
preprocessed</div>
<pre class="program"> &lt;p&gt;
Name: &lt;%= text_field :user, :name %&gt;
&lt;/p&gt;
&lt;p&gt;
Name: <strong>&lt;input id="stock_name" name="stock[name]" size="30" type="text" value="&lt;%=h @stock.name%&gt;" /&gt;</strong>
&lt;/p&gt;
</pre>
<p>Erubis converts it to the following Ruby code:
</p>
<div class="program_caption">
Ruby code</div>
<pre class="program"> _buf &lt;&lt; ' &lt;p&gt;
Name: '; _buf &lt;&lt; ( text_field :stock, :name ).to_s; _buf &lt;&lt; '
'; _buf &lt;&lt; ' &lt;/p&gt;
&lt;p&gt;
Name: &lt;input id="stock_name" name="stock[name]" size="30" type="text" value="'; _buf &lt;&lt; (h @stock.name).to_s; _buf &lt;&lt; '" /&gt;
&lt;/p&gt;
';
</pre>
<p>The above Ruby code shows that text_field() is called everytime when rendering,
but pp_text_field() is called only once when template is loaded.
This means that pp_text_field() with preprocessing makes view layer very fast.
</p>
<p>Module Erubis::Helpers::RailsFormHelper defines the following form helper methods.
</p>
<ul type="disc">
<li>pp_render_partial(basename)
</li>
<li>pp_form_tag(url_for_options={}, options={}, *parameters_for_url, &amp;block)
</li>
<li>pp_text_field(object_name, method, options={})
</li>
<li>pp_password_field(object_name, method, options={})
</li>
<li>pp_hidden_field(object_name, method, options={})
</li>
<li>pp_file_field(object_name, method, options={})
</li>
<li>pp_text_area(object_name, method, options={})
</li>
<li>pp_check_box(object_name, method, options={}, checked_value="1", unchecked_value="0")
</li>
<li>pp_radio_button(object_name, method, tag_value, options={})
</li>
<li>pp_select(object, method, collection, options={}, html_options={})
</li>
<li>pp_collection_select(object, method, collection, value_method, text_method, options={}, html_options={})
</li>
<li>pp_country_select(object, method, priority_countries=nil, options={}, html_options={})
</li>
<li>pp_time_zone_select(object, method, priority_zones=nil, options={}, html_options={})
</li>
<li>pp_submit_tag(value="Save changes", options={})
</li>
<li>pp_image_submit_tag(source, options={})
</li>
</ul>
<p>Notice that pp_form_for() is not provided.
</p>
<p><span style="color:#FF0000">CAUTION:</span> These are experimental and may not work in Ruby on Rails 2.0.
</p>
<br>
<a name="rails-others"></a>
<h3 class="section2">Others</h3>
<ul type="disc">
<li>ActionView::Helpers::CaptureHelper#capture() and ActionView::Helpers::Texthelper#concat() are available.
</li>
</ul>
<ul type="disc">
<li>Form helper methods are not tested in Ruby on Rails 2.0.
</li>
</ul>
<ul type="disc">
<li>ERB::Util.h() is redefined if you require 'erubis/helpers/rails_helper.rb'.
Original definition of ERB::Util.h() is the following and it is slow
because it scans string four times.
<pre class="program"> def html_escape(s)
s.to_s.gsub(/&amp;/, "&amp;amp;").gsub(/\"/, "&amp;quot;").gsub(/&gt;/, "&amp;gt;").gsub(/&lt;/, "&amp;lt;")
end
alias h html_escape
</pre>
<p> New definition in 'erubis/helpers/rails_helper.rb' is faster than the above
because it scans string only once.
</p>
<pre class="program"> ESCAPE_TABLE = { '&amp;'=&gt;'&amp;amp;', '&lt;'=&gt;'&amp;lt;', '&gt;'=&gt;'&amp;gt;', '"'=&gt;'&amp;quot;', "'"=&gt;'&amp;#039;', }
def h(value)
value.to_s.gsub(/[&amp;&lt;&gt;"]/) { |s| ESCAPE_TABLE[s] }
end
</pre>
<p> Notice that the new definition may be slow if string contains
many '&lt; &gt; &amp; "' characters because block is call many time.
You should use ERB::Util.html_hscape() if string contains a lot of '&lt; &gt; &amp; "'
characters.
</p>
</li>
</ul>
<br>
<br>
<a name="topics"></a>
<h2 class="section1">Other Topics</h2>
<a name="'&lt;%= =%&gt;' and '&lt;%= -%&gt;'"></a>
<h3 class="section2">'&lt;%= =%&gt;' and '&lt;%= -%&gt;'</h3>
<p>Since 2.6.0, '&lt;%= -%&gt;' remove tail spaces and newline.
This is for compatibiliy with ERB when trim mode is '-'.
'&lt;%= =%&gt;' also removes tail spaces and newlines, and this is
Erubis-original enhancement (cooler than '&lt;%= -%&gt;', isn't it?).
</p>
<a name="tailnewline.rhtml.comment_filter"></a>
<div class="program_caption">
tailnewline.rhtml</div>
<pre class="program">&lt;div&gt;
&lt;%= @var -%&gt; # or &lt;%= @var =%&gt;
&lt;/div&gt;
</pre>
<div class="terminal_caption">
result (version 2.5.0):</div>
<pre class="terminal">$ erubis -c '{var: "AAA\n"}' tailnewline.rhtml
&lt;div&gt;
AAA
&lt;/div&gt;
</pre>
<a name="tail_260.result"></a>
<div class="terminal_caption">
result (version 2.6.0):</div>
<pre class="terminal">$ erubis -c '{var: "AAA\n"}' tailnewline.rhtml
&lt;div&gt;
AAA
&lt;/div&gt;
</pre>
<br>
<a name="'&lt;%% %&gt;' and '&lt;%%= %&gt;'"></a>
<h3 class="section2">'&lt;%% %&gt;' and '&lt;%%= %&gt;'</h3>
<p>Since 2.6.0, '&lt;%% %&gt;' and '&lt;%%= %&gt;' are converted into '&lt;% %&gt;' and '&lt;%= %&gt;' respectively.
This is for compatibility with ERB.
</p>
<div class="program_caption">
doublepercent.rhtml:</div>
<pre class="program">&lt;ul&gt;
&lt;%% for item in @list %&gt;
&lt;li&gt;&lt;%%= item %&gt;&lt;/li&gt;
&lt;%% end %&gt;
&lt;/ul&gt;
</pre>
<div class="terminal_caption">
result:</div>
<pre class="terminal">$ erubis doublepercent.rhtml
&lt;ul&gt;
&lt;% for item in @list %&gt;
&lt;li&gt;&lt;%= item %&gt;&lt;/li&gt;
&lt;% end %&gt;
&lt;/ul&gt;
</pre>
<br>
<a name="topics-context-vs-binding"></a>
<h3 class="section2">evaluate(context) v.s. result(binding)</h3>
<p>It is recommended to use 'Erubis::Eruby#evaluate(context)' instead of 'Erubis::Eruby#result(binding)' because Ruby's Binding object has some problems.
</p>
<ul type="disc">
<li>It is not able to specify variables to use.
Using binding() method, all of local variables are passed to templates.
</li>
<li>Changing local variables in templates may affect to varialbes in main program.
If you assign '10' to local variable 'x' in templates, it may change variable 'x' in main program unintendedly.
</li>
</ul>
<p>The following example shows that assignment of some values into variable 'x' in templates affect to local variable 'x' in main program unintendedly.
</p>
<a name="template1.rhtml"></a>
<div class="program_caption">
template1.rhtml (intended to be passed 'items' from main program)</div>
<pre class="program">&lt;% for <strong>x</strong> in <strong>items</strong> %&gt;
item = &lt;%= x %&gt;
&lt;% end %&gt;
** debug: local variables=&lt;%= local_variables().inspect() %&gt;
</pre>
<a name="main_program1.rb"></a>
<div class="program_caption">
main_program1.rb (intended to pass 'items' to template)</div>
<pre class="program">require 'erubis'
eruby = Erubis::Eruby.new(File.read('template1.rhtml'))
items = ['foo', 'bar', 'baz']
x = 1
## local variable 'x' and 'eruby' are passed to template as well as 'items'!
print <strong>eruby.result(binding())</strong>
## local variable 'x' is changed unintendedly because it is changed in template!
puts "** debug: x=#{x.inspect}" #=&gt; "baz"
</pre>
<a name="main_program1.result"></a>
<div class="terminal_caption">
Result:</div>
<pre class="terminal">$ ruby main_program1.rb
item = foo
item = bar
item = baz
** debug: local variables=["eruby", "items", "x", "_buf"]
** debug: x="baz"
</pre>
<p>This problem is caused because Ruby's Binding class is poor to use in template engine.
Binding class should support the following features.
</p>
<pre class="program">b = Binding.new # create empty Binding object
b['x'] = 1 # set local variables using binding object
</pre>
<p>But the above features are not implemented in Ruby.
</p>
<p>A pragmatic solution is to use 'Erubis::Eruby#evaluate(context)' instead of 'Erubis::Eruby#result(binding)'.
'evaluate(context)' uses Erubis::Context object and instance variables instead of Binding object and local variables.
</p>
<a name="template2.rhtml"></a>
<div class="program_caption">
template2.rhtml (intended to be passed '@items' from main program)</div>
<pre class="program">&lt;% for <strong>x</strong> in <strong>@items</strong> %&gt;
item = &lt;%= x %&gt;
&lt;% end %&gt;
** debug: local variables=&lt;%= local_variables().inspect() %&gt;
</pre>
<a name="main_program2.rb"></a>
<div class="program_caption">
main_program2.rb (intended to pass '@items' to template)</div>
<pre class="program">require 'erubis'
eruby = Erubis::Eruby.new(File.read('template2.rhtml'))
items = ['foo', 'bar', 'baz']
x = 1
## only 'items' are passed to template
print <strong>eruby.evaluate(:items=&gt;items)</strong>
## local variable 'x' is not changed!
puts "** debug: x=#{x.inspect}" #=&gt; 1
</pre>
<a name="main_program2.result"></a>
<div class="terminal_caption">
Result:</div>
<pre class="terminal">$ ruby main_program2.rb
item = foo
item = bar
item = baz
** debug: local variables=["_context", "x", "_buf"]
** debug: x=1
</pre>
<br>
<a name="topics-fasteruby"></a>
<h3 class="section2">Class Erubis::FastEruby</h3>
<p>[experimental]
</p>
<p>Erubis provides Erubis::FastEruby class which includes <a href="#interpolation-enhancer">InterpolationEnhancer</a> and <a href="#topics-benchmark">works faster than Erubis::Eruby class</a>.
If you desire more speed, try Erubis::FastEruby class.
</p>
<a name="fasteruby.rhtml"></a>
<div class="program_caption">
File 'fasteruby.rhtml':</div>
<pre class="program">&lt;html&gt;
&lt;body&gt;
&lt;h1&gt;&lt;%== @title %&gt;&lt;/h1&gt;
&lt;table&gt;
&lt;% i = 0 %&gt;
&lt;% for item in @list %&gt;
&lt;% i += 1 %&gt;
&lt;tr&gt;
&lt;td&gt;&lt;%= i %&gt;&lt;/td&gt;
&lt;td&gt;&lt;%== item %&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;% end %&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<a name="fasteruby.rb"></a>
<div class="program_caption">
File 'fasteruby.rb':</div>
<pre class="program">require 'erubis'
input = File.read('fasteruby.rhtml')
eruby = <strong>Erubis::FastEruby.new(input)</strong> # create Eruby object
puts "---------- script source ---"
puts eruby.src
puts "---------- result ----------"
context = { :title=&gt;'Example', :list=&gt;['aaa', 'bbb', 'ccc'] }
output = eruby.evaluate(context)
print output
</pre>
<a name="fasteruby.result"></a>
<div class="terminal_caption">
output</div>
<pre class="terminal">$ ruby fasteruby.rb
---------- script source ---
_buf = ''; _buf &lt;&lt; <strong>%Q`</strong>&lt;html&gt;
&lt;body&gt;
&lt;h1&gt;<strong>#{Erubis::XmlHelper.escape_xml( @title )}</strong>&lt;/h1&gt;
&lt;table&gt;\n<strong>`</strong>
i = 0
for item in @list
i += 1
_buf &lt;&lt; <strong>%Q`</strong> &lt;tr&gt;
&lt;td&gt;<strong>#{ i }</strong>&lt;/td&gt;
&lt;td&gt;<strong>#{Erubis::XmlHelper.escape_xml( item )}</strong>&lt;/td&gt;
&lt;/tr&gt;\n<strong>`</strong>
end
_buf &lt;&lt; <strong>%Q`</strong> &lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;\n<strong>`</strong>
_buf.to_s
---------- result ----------
&lt;html&gt;
&lt;body&gt;
&lt;h1&gt;Example&lt;/h1&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;aaa&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;bbb&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;ccc&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<br>
<a name="topics-syntax"></a>
<h3 class="section2">Syntax Checking</h3>
<p>Command-line option '-z' checks syntax. It is similar to 'erubis -x file.rhtml | ruby -wc', but it can take several file names.
</p>
<div class="terminal_caption">
example of command-line option '-z'</div>
<pre class="terminal">$ erubis <strong>-z</strong> app/views/*/*.rhtml
Syntax OK
</pre>
<br>
<a name="topics-caching"></a>
<h3 class="section2">File Caching</h3>
<p>Erubis::Eruby.load_file(filename) convert file into Ruby script and return Eruby object.
In addition, it caches converted Ruby script into cache file (filename + '.cache') if cache file is old or not exist.
If cache file exists and is newer than eruby file, Erubis::Eruby.load_file() loads cache file.
</p>
<div class="program_caption">
example of Erubis::Eruby.load_file()</div>
<pre class="program">require 'erubis'
filename = 'example.rhtml'
eruby = <strong>Erubis::Eruby.load_file(filename)</strong>
cachename = filename + '.cache'
if test(?f, cachename)
puts "*** cache file '#{cachename}' created."
end
</pre>
<p>Since 2.6.0, it is able to specify cache filename.
</p>
<div class="program_caption">
specify cache filename.</div>
<pre class="program">filename = 'example.rhtml'
eruby = Erubis::Eruby.load_file(filename, :cachename=&gt;filename+'.cache')
</pre>
<p>Caching makes Erubis about 40-50 percent faster than no-caching.
See <a href="#topics-benchmark">benchmark</a> for details.
</p>
<br>
<a name="topics-tinyeruby"></a>
<h3 class="section2">Erubis::TinyEruby class</h3>
<p>Erubis::TinyEruby class in 'erubis/tiny.rb' is the smallest implementation of eRuby.
If you don't need any enhancements of Erubis and only require simple eRuby implementation,
try Erubis::TinyEruby class.
</p>
<br>
<a name="topics-php"></a>
<h3 class="section2">NoTextEnhancer and NoCodeEnhancer in PHP</h3>
<p>NoTextEnhancer and NoCodEnahncer are quite useful not only for eRuby but also for PHP.
The former "drops" HTML text and show up embedded Ruby/PHP code
and the latter drops embedded Ruby/PHP code and leave HTML text.
</p>
<p>For example, see the following PHP script.
</p>
<a name="notext-example.php"></a>
<div class="program_caption">
notext-example.php</div>
<pre class="program">&lt;html&gt;
&lt;body&gt;
&lt;h3&gt;List&lt;/h3&gt;
&lt;?php if (!$list || count($list) == 0) { ?&gt;
&lt;p&gt;not found.&lt;/p&gt;
&lt;?php } else { ?&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;?php $i = 0; ?&gt;
&lt;?php foreach ($list as $item) { ?&gt;
&lt;tr bgcolor="&lt;?php echo ++$i % 2 == 1 ? '#FFCCCC' : '#CCCCFF'; ?&gt;"&gt;
&lt;td&gt;&lt;?php echo $item; ?&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;?php } ?&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;?php } ?&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>This is complex because PHP code and HTML document are mixed.
NoTextEnhancer can separate PHP code from HTML document.
</p>
<a name="notext-php.result"></a>
<div class="terminal_caption">
example of using NoTextEnhancer with PHP file</div>
<pre class="terminal">$ erubis -l php --pi=php -N -E NoText --trim=false notext-example.php
1:
2:
3:
4: &lt;?php if (!$list || count($list) == 0) { ?&gt;
5:
6: &lt;?php } else { ?&gt;
7:
8:
9: &lt;?php $i = 0; ?&gt;
10: &lt;?php foreach ($list as $item) { ?&gt;
11: &lt;?php echo ++$i % 2 == 1 ? '#FFCCCC' : '#CCCCFF'; ?&gt;
12: &lt;?php echo $item; ?&gt;
13:
14: &lt;?php } ?&gt;
15:
16:
17: &lt;?php } ?&gt;
18:
19:
</pre>
<p>In the same way, NoCodeEnhancer can extract HTML tags.
</p>
<a name="nocode-php.result"></a>
<div class="terminal_caption">
example of using NoCodeEnhancer with PHP file</div>
<pre class="terminal">$ erubis -l php --pi=php -N -E NoCode --trim=false notext-example.php
1: &lt;html&gt;
2: &lt;body&gt;
3: &lt;h3&gt;List&lt;/h3&gt;
4:
5: &lt;p&gt;not found.&lt;/p&gt;
6:
7: &lt;table&gt;
8: &lt;tbody&gt;
9:
10:
11: &lt;tr bgcolor=""&gt;
12: &lt;td&gt;&lt;/td&gt;
13: &lt;/tr&gt;
14:
15: &lt;/tbody&gt;
16: &lt;/table&gt;
17:
18: &lt;/body&gt;
19: &lt;/html&gt;
</pre>
<br>
<a name="topcs-modruby"></a>
<h3 class="section2">Helper Class for mod_ruby</h3>
<p>Thanks Andrew R Jackson, he developed 'erubis-run.rb' which enables you to use Erubis with mod_ruby.
</p>
<ol type="1">
<li>Copy 'erubis-2.6.5/contrib/erubis-run.rb' to the 'RUBYLIBDIR/apache' directory (for example '/usr/local/lib/ruby/1.8/apache') which contains 'ruby-run.rb', 'eruby-run.rb', and so on.
<pre class="terminal">$ cd erubis-2.6.5/
$ sudo copy contrib/erubis-run.rb /usr/local/lib/ruby/1.8/apache/
</pre>
</li>
<li>Add the following example to your 'httpd.conf' (for example '/usr/local/apache2/conf/httpd.conf')
<pre class="program">LoadModule ruby_module modules/mod_ruby.so
&lt;IfModule mod_ruby.c&gt;
RubyRequire apache/ruby-run
RubyRequire apache/eruby-run
RubyRequire apache/erubis-run
&lt;Location /erubis&gt;
SetHandler ruby-object
RubyHandler Apache::ErubisRun.instance
&lt;/Location&gt;
&lt;Files *.rhtml&gt;
SetHandler ruby-object
RubyHandler Apache::ErubisRun.instance
&lt;/Files&gt;
&lt;/IfModule&gt;
</pre>
</li>
<li>Restart Apache web server.
<pre class="terminal">$ sudo /usr/local/apache2/bin/apachectl stop
$ sudo /usr/local/apache2/bin/apachectl start
</pre>
</li>
<li>Create *.rhtml file, for example:
<pre class="program">&lt;html&gt;
&lt;body&gt;
Now is &lt;%= Time.now %&gt;
Erubis version is &lt;%= Erubis::VERSION %&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
</li>
<li>Change mode of your directory to be writable by web server process.
<pre class="terminal">$ cd /usr/local/apache2/htdocs/erubis
$ sudo chgrp daemon .
$ sudo chmod 775 .
</pre>
</li>
<li>Access the *.rhtml file and you'll get the web page.
</li>
</ol>
<p>You must set your directories to be writable by web server process, because
Apache::ErubisRun calls Erubis::Eruby.load_file() internally which creates cache files
in the same directory in which '*.rhtml' file exists.
</p>
<br>
<a name="topics-defmethod"></a>
<h3 class="section2">Define method</h3>
<p>Erubis::Eruby#def_method() defines instance method or singleton method.
</p>
<a name="def_method.rb"></a>
<pre class="program">require 'erubis'
s = "hello &lt;%= name %&gt;"
eruby = Erubis::Eruby.new(s)
filename = 'hello.rhtml'
## define instance method to Dummy class (or module)
class Dummy; end
<strong>eruby.def_method(Dummy, 'render(name)', filename)</strong> # filename is optional
p Dummy.new.render('world') #=&gt; "hello world"
## define singleton method to dummy object
obj = Object.new
<strong>eruby.def_method(obj, 'render(name)', filename)</strong> # filename is optional
p obj.render('world') #=&gt; "hello world"
</pre>
<br>
<a name="topics-benchmark"></a>
<h3 class="section2">Benchmark</h3>
<p>A benchmark script is included in Erubis package at 'erubis-2.6.5/benchark/' directory.
Here is an example result of benchmark.
</p>
<div class="terminal_caption">
MacOS X 10.4 Tiger, Intel CoreDuo 1.83GHz, Ruby1.8.6, eruby1.0.5, gcc4.0.1</div>
<pre class="terminal">$ cd erubis-2.6.5/benchmark/
$ ruby bench.rb -n 10000 -m execute
*** ntimes=10000, testmode=execute
user system total real
eruby 12.720000 0.240000 <strong>12.960000</strong> ( 12.971888)
ERB 36.760000 0.350000 <strong>37.110000</strong> ( 37.112019)
ERB(cached) 11.990000 0.440000 <strong>12.430000</strong> ( 12.430375)
Erubis::Eruby 10.840000 0.300000 <strong>11.140000</strong> ( 11.144426)
Erubis::Eruby(cached) 7.540000 0.410000 <strong>7.950000</strong> ( 7.969305)
Erubis::FastEruby 10.440000 0.300000 <strong>10.740000</strong> ( 10.737808)
Erubis::FastEruby(cached) 6.940000 0.410000 <strong>7.350000</strong> ( 7.353666)
Erubis::TinyEruby 9.550000 0.290000 9.840000 ( 9.851729)
Erubis::ArrayBufferEruby 11.010000 0.300000 11.310000 ( 11.314339)
Erubis::PrintOutEruby 11.640000 0.290000 11.930000 ( 11.942141)
Erubis::StdoutEruby 11.590000 0.300000 11.890000 ( 11.886512)
</pre>
<p>This shows that...
</p>
<ul type="disc">
<li>Erubis::Eruby runs more than 10 percent faster than eruby.
</li>
<li>Erubis::Eruby runs about 3 times faster than ERB.
</li>
<li>Caching (by Erubis::Eruby.load_file()) makes Erubis about 40-50 percent faster.
</li>
<li>Erubis::FastEruby is a litte faster than Erubis::Eruby.
</li>
<li>Array buffer (ArrayBufferEnhancer) is a little slower than string buffer (StringBufferEnhancer which Erubis::Eruby includes)
</li>
<li>$stdout and print() make Erubis a little slower.
</li>
<li>Erubis::TinyEruby (at 'erubis/tiny.rb') is the fastest in all eRuby implementations when no caching.
</li>
</ul>
<p>Escaping HTML characters (such as '&lt; &gt; &amp; "') makes Erubis more faster than eruby and ERB,
because Erubis::XmlHelper#escape_xml() works faster than CGI.escapeHTML() and ERB::Util#h().
The following shows that Erubis runs more than 40 percent (when no-cached) or 90 percent (when cached) faster than eruby if HTML characters are escaped.
</p>
<div class="terminal_caption">
When escaping HTML characters with option '-e'</div>
<pre class="terminal">$ ruby bench.rb -n 10000 -m execute -ep
*** ntimes=10000, testmode=execute
user system total real
eruby 21.700000 0.290000 <strong>21.990000</strong> ( 22.050687)
ERB 45.140000 0.390000 <strong>45.530000</strong> ( 45.536976)
ERB(cached) 20.340000 0.470000 <strong>20.810000</strong> ( 20.822653)
Erubis::Eruby 14.830000 0.310000 <strong>15.140000</strong> ( 15.147930)
Erubis::Eruby(cached) 11.090000 0.420000 <strong>11.510000</strong> ( 11.514954)
Erubis::FastEruby 14.850000 0.310000 <strong>15.160000</strong> ( 15.172499)
Erubis::FastEruby(cached) 10.970000 0.430000 <strong>11.400000</strong> ( 11.399605)
Erubis::ArrayBufferEruby 14.970000 0.300000 15.270000 ( 15.281061)
Erubis::PrintOutEruby 15.780000 0.300000 16.080000 ( 16.088289)
Erubis::StdoutEruby 15.840000 0.310000 16.150000 ( 16.235338)
</pre>
<br>
<br>
<a name="command"></a>
<h2 class="section1">Command Reference</h2>
<a name="command-usage"></a>
<h3 class="section2">Usage</h3>
<p>erubis [..options..] [<em>file</em> ...]
</p>
<br>
<a name="command-options"></a>
<h3 class="section2">Options</h3>
<dl class="dl3" compact>
<dt class="dt3"><b>
-h, --help </b></dt>
<dd class="dd3">
Help.
</dd>
<dt class="dt3"><b>
-v </b></dt>
<dd class="dd3">
Release version.
</dd>
<dt class="dt3"><b>
-x </b></dt>
<dd class="dd3">
Show compiled source.
</dd>
<dt class="dt3"><b>
-X </b></dt>
<dd class="dd3">
Show compiled source but only Ruby code.
This is equivarent to '-E NoText'.
</dd>
<dt class="dt3"><b>
-N </b></dt>
<dd class="dd3">
Numbering: add line numbers. (for '-x/-X')
</dd>
<dt class="dt3"><b>
-U </b></dt>
<dd class="dd3">
Unique mode: zip empty lines into a line. (for '-x/-X')
</dd>
<dt class="dt3"><b>
-C </b></dt>
<dd class="dd3">
Compact: remove empty lines. (for '-x/-X')
</dd>
<dt class="dt3"><b>
-b </b></dt>
<dd class="dd3">
Body only: no preamble nor postamble. (for '-x/-X')
This is equivarent to '--preamble=false --postamble=false'.
</dd>
<dt class="dt3"><b>
-z </b></dt>
<dd class="dd3">
Syntax checking.
</dd>
<dt class="dt3"><b>
-e </b></dt>
<dd class="dd3">
Escape. This is equivarent to '-E Escape'.
</dd>
<dt class="dt3"><b>
-p pattern </b></dt>
<dd class="dd3">
Embedded pattern (default '&lt;% %&gt;').
This is equivarent to '--pattern=<em>pattern</em>'.
</dd>
<dt class="dt3"><b>
-l lang </b></dt>
<dd class="dd3">
Language name.
This option makes erubis command to compile script but no execute.
</dd>
<dt class="dt3"><b>
-E enhacers </b></dt>
<dd class="dd3">
Enhancer name (Escape, PercentLine, ...).
It is able to specify several enhancer name separating with ','
(ex. -f Escape,PercentLine,HeaderFooter).
</dd>
<dt class="dt3"><b>
-I path </b></dt>
<dd class="dd3">
Require library path ($:).
It is able to specify several paths separating with ','
(ex. -f path1,path2,path3).
</dd>
<dt class="dt3"><b>
-K kanji </b></dt>
<dd class="dd3">
Kanji code (euc, sjis, utf8, or none) (default none).
</dd>
<dt class="dt3"><b>
-f datafile </b></dt>
<dd class="dd3">
Context data file in YAML format ('*.yaml', '*.yml') or
Ruby script ('*.rb').
It is able to specify several filenames separating with ','
(ex. -f file1,file2,file3).
</dd>
<dt class="dt3"><b>
-c context </b></dt>
<dd class="dd3">
Context data string in YAML inline style or Ruby code.
</dd>
<dt class="dt3"><b>
-T </b></dt>
<dd class="dd3">
Don't expand tab characters in YAML file.
</dd>
<dt class="dt3"><b>
-S </b></dt>
<dd class="dd3">
Convert mapping key from string to symbol in YAML file.
</dd>
<dt class="dt3"><b>
-B </b></dt>
<dd class="dd3">
invoke Eruby#result() instead of Eruby#evaluate()
</dd>
<dt class="dt3"><b>
--pi[=name] </b></dt>
<dd class="dd3">
parse '&lt;?name ... ?&gt;' instead of '&lt;% ... %&gt;'
</dd>
<dt class="dt3"><b>
--trim=false </b></dt>
<dd class="dd3">
No trimming spaces around '&lt;% %&gt;'.
</dd>
</dl>
<br>
<a name="command-props"></a>
<h3 class="section2">Properties</h3>
<p>Some Eruby classes can take optional properties to change it's compile option.
For example, property '--indent=" "' may change indentation of compiled source code.
Try 'erubis -h' for details.
</p>
<br>
<br>
</div>
</blockquote>
</body>
</html>