/External.LCA_RESTRICTED/Languages/Ruby/ruby19/lib/ruby/gems/1.9.1/gems/erubis-2.6.6/doc/users-guide.html
HTML | 3286 lines | 3057 code | 229 blank | 0 comment | 0 complexity | 82a50bbe1552bf9bf0ed09a0ea043ad5 MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
Large files files are truncated, but you can click here to view the full file
- <!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">
- release: 2.6.6<br>
- last update: $Date$<br>
- </div>
- <p>release: 2.6.6
- </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 '<% %>'
- </li>
- <li>Embedded pattern changeable (default '<% %>')
- </li>
- <li>Enable to handle Processing Instructions (PI) as embedded pattern (ex. '<?rb ... ?>')
- </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="#'<%= =%>' and '<%= -%>'">'<%= =%>' and '<%= -%>'</a>
- </li>
- <li><a href="#'<%% %>' and '<%%= %>'">'<%% %>' and '<%%= %>'</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 > 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"><ul>
- <strong><% for item in list %></strong>
- <li><strong><%= item %></strong></li>
- <strong><% end %></strong>
- <strong><%# here is ignored because starting with '#' %></strong>
- </ul>
- </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=>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 << '<ul>
- '; for item in list
- _buf << ' <li>'; _buf << ( item ).to_s; _buf << '</li>
- '; end
- _buf << '</ul>
- ';
- _buf.to_s
- ---------- result ----------
- <ul>
- <li>aaa</li>
- <li>bbb</li>
- <li>ccc</li>
- </ul>
- </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 << '<ul>
- '; for item in list
- _buf << ' <li>'; _buf << ( item ).to_s; _buf << '</li>
- '; end
- _buf << '</ul>
- ';
- _buf.to_s
- </pre>
- <br>
- <a name="tut-trim"></a>
- <h3 class="section2">Trimming Spaces</h3>
- <p>Erubis deletes spaces around '<% %>' automatically, while it leaves spaces around '<%= %>'.
- </p>
- <a name="example2.eruby.comment_filter"></a>
- <div class="program_caption">
- example2.eruby</div>
- <pre class="program"><ul>
- <% for item in list %> # trimmed
- <li>
- <%= item %> # not trimmed
- </li>
- <% end %> # trimmed
- </ul>
- </pre>
- <a name="example2_x.result"></a>
- <div class="terminal_caption">
- compiled source code</div>
- <pre class="terminal">$ erubis -x example2.eruby
- _buf = ''; _buf << '<ul>
- '; for item in list
- _buf << ' <li>
- '; _buf << ( item ).to_s; _buf << '
- '; _buf << ' </li>
- '; end
- _buf << '</ul>
- ';
- _buf.to_s
- </pre>
- <p>If you want leave spaces around '<% %>', 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 << '<ul>
- '; _buf << ' '; for item in list ; _buf << '
- '; _buf << ' <li>
- '; _buf << ( item ).to_s; _buf << '
- '; _buf << ' </li>
- '; _buf << ' '; end ; _buf << '
- '; _buf << '</ul>
- ';
- _buf.to_s
- </pre>
- <p>Or add option <code>:trim=>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=>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 << '<ul>
- '; <strong>_buf << ' ';</strong> for item in list ; _buf << '
- '; _buf << ' <li>
- '; _buf << ( item ).to_s; _buf << '
- '; _buf << ' </li>
- '; <strong>_buf << ' ';</strong> end ; _buf << '
- '; _buf << '</ul>
- ';
- _buf.to_s
- ----- result ----------
- <ul>
-
- <li>
- aaa
- </li>
-
- <li>
- bbb
- </li>
-
- <li>
- ccc
- </li>
-
- </ul>
- </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><%= <em>expr</em> %></code> - not escaped.
- </li>
- <li><code><%== <em>expr</em> %></code> - escaped.
- </li>
- <li><code><%=== <em>expr</em> %></code> - out to $stderr.
- </li>
- <li><code><%==== <em>expr</em> %></code> - ignored.
- </li>
- </ul>
- <p>Erubis::EscapedEruby<sup>(<a href="#fnref:1" name="fnlink:1">*1</a>)</sup> class handle '<%= %>' as escaped and '<%== %>' 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"><% for item in list %>
- <p><strong><%=</strong> item <strong>%></strong></p>
- <p><strong><%==</strong> item <strong>%></strong></p>
- <p><strong><%===</strong> item <strong>%></strong></p>
- <% end %>
- </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 = ['<aaa>', 'b&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> stderr.log
- ----- script source ---
- _buf = ''; for item in list
- _buf << ' <p>'; <strong>_buf << Erubis::XmlHelper.escape_xml( item )</strong>; _buf << '</p>
- <p>'; <strong>_buf << ( item ).to_s</strong>; _buf << '</p>
- <p>'; <strong>$stderr.puts("*** debug: item=#{(item).inspect}")</strong>; _buf << '</p>
- '; end
- _buf.to_s
- ----- result ----------
- <p><strong>&lt;aaa&gt;</strong></p>
- <p><aaa></p>
- <p></p>
- <p><strong>b&amp;b</strong></p>
- <p>b&b</p>
- <p></p>
- <p><strong>&quot;ccc&quot;</strong></p>
- <p>"ccc"</p>
- <p></p>
- $ cat stderr.log
- *** debug: item="<aaa>"
- *** debug: item="b&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 << ' <p>'; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << '</p>
- <p>'; _buf << ( item ).to_s; _buf << '</p>
- <p>'; $stderr.puts("*** debug: item=#{(item).inspect}"); _buf << '</p>
- '; 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 < Erubis::Eruby
- def <strong>escaped_expr(code)</strong>
- return "CGI.escapeHTML((#{code.strip}).to_s)"
- #return "h(#{code.strip})"
- end
- end
- class LatexEruby < Erubi::Eruby
- def <strong>escaped_expr(code)</strong>
- return "(#{code}).gsub(/[%\\]/,'\\\\\&')"
- 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 '<%= %>' and '<%== %>'.</dd>
- </dl>
- </div>
- <br>
- <a name="tut-pattern"></a>
- <h3 class="section2">Embedded Pattern</h3>
- <p>You can change embedded pattern '<code><% %></code>' to another by command-line option '-p' or option '<code>:pattern=>...</code>' of Erubis::Eruby.new().
- </p>
- <a name="example4.eruby"></a>
- <div class="program_caption">
- example4.eruby</div>
- <pre class="program"><strong><!--%</strong> for item in list <strong>%--></strong>
- <p><strong><!--%=</strong> item <strong>%--></strong></p>
- <strong><!--%</strong> end <strong>%--></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 '<!--% %-->'</strong> example4.eruby
- _buf = ''; for item in list
- _buf << ' <p>'; _buf << ( item ).to_s; _buf << '</p>
- '; 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=>'<!--% %-->'</strong>)
- # or '<(?:!--)?% %(?:--)?>'
- 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 << ' <p>'; _buf << ( item ).to_s; _buf << '</p>
- '; end
- _buf.to_s
- ---------- result ----------
- <p>aaa</p>
- <p>bbb</p>
- <p>ccc</p>
- </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><(!--)?% %(--)?></code>' will not work while '<code><(?:!--)?% %(?:--)?></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"><span><%= <strong>@val</strong> %></span>
- <ul>
- <% for item in <strong>@list</strong> %>
- <li><%= item %></li>
- <% end %>
- </ul>
- </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 => 'Erubis Example',
- 'list' => ['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
- <span>Erubis Example</span>
- <ul>
- <li>aaa</li>
- <li>bbb</li>
- <li>ccc</li>
- </ul>
- </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
- <span>Erubis Example</span>
- <ul>
- <li>aaa</li>
- <li>bbb</li>
- <li>ccc</li>
- </ul>
- </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"><h1><%= <strong>@title</strong> %></h1>
- <ul>
- <% for user in <strong>@users</strong> %>
- <li>
- <a href="mailto:<%= user['mail']%>"><%= user['name'] %></a>
- </li>
- <% end %>
- </ul>
- </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'=>'foo', 'mail'=>'foo@mail.com' },
- { 'name'=>'bar', 'mail'=>'bar@mail.net' },
- { 'name'=>'baz', 'mail'=>'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
- <h1>Users List</h1>
- <ul>
- <li>
- <a href="mailto:foo@mail.com">foo</a>
- </li>
- <li>
- <a href="mailto:bar@mail.net">bar</a>
- </li>
- <li>
- <a href="mailto:baz@mail.org">baz</a>
- </li>
- </ul>
- $ erubis <strong>-f context.rb</strong> example7.eruby
- <h1>Users List</h1>
- <ul>
- <li>
- <a href="mailto:foo@mail.com">foo</a>
- </li>
- <li>
- <a href="mailto:bar@mail.net">bar</a>
- </li>
- <li>
- <a href="mailto:baz@mail.org">baz</a>
- </li>
- </ul>
- </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"><h1><%= @title %></h1>
- <ul>
- <% for item in @list %>
- <li><%= item %></li>
- <% end %>
- </ul>
- </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
- <h1>Example</h1>
- <ul>
- <li>AAA</li>
- <li>BBB</li>
- <li>CCC</li>
- </ul>
- </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
- <h1>Example</h1>
- <ul>
- <li>AAA</li>
- <li>BBB</li>
- <li>CCC</li>
- </ul>
- </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"><% for item in @list %>
- <b><%= item %></b>
- <% end %>
- </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 << ' <b>'; _buf << ( item ).to_s; _buf << '</b>
- '; end
- <strong>_buf.to_s</strong>
- $ erubis -x <strong>-b</strong> example9.eruby
- for item in @list
- _buf << ' <b>'; _buf << ( item ).to_s; _buf << '</b>
- '; end
- </pre>
- <p>Erubis::Eruby.new option '<code>:preamble=>false</code>' and '<code>:postamble=>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=>false, :postamble=>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 << ' <b>'; _buf << ( item ).to_s; _buf << '</b>
- '; end
- _buf.to_s
- --------------
- for item in @list
- _buf << ' <b>'; _buf << ( item ).to_s; _buf << '</b>
- '; 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><?rb <em>...</em> ?></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><%= <em>...</em> %></code>' is also available to print expression value.
- </li>
- </ul>
- <p>This is more useful than basic embedded pattern ('<code><% ... ></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"><?xml version="1.0" ?>
- <strong><?rb
- lang = 'en'
- list = ['<aaa>', 'b&b', '"ccc"']
- ?></strong>
- <html lang="<strong>@!{lang}@</strong>">
- <body>
- <ul>
- <strong><?rb for item in list ?></strong>
- <li><strong>@{item}@</strong></li>
- <strong><?rb end ?></strong>
- </ul>
- </body>
- </html>
- </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"><?rb ... ?></td>
- </tr>
- <tr class="tr1">
- <td class="td1">-l php</td>
- <td class="td1"><?php ... ?></td>
- </tr>
- <tr class="tr1">
- <td class="td1">-l perl</td>
- <td class="td1"><?perl ... ?></td>
- </tr>
- <tr class="tr1">
- <td class="td1">-l java</td>
- <td class="td1"><?java ... ?></td>
- </tr>
- <tr class="tr1">
- <td class="td1">-l javascript</td>
- <td class="td1"><?js ... ?></td>
- </tr>
- <tr class="tr1">
- <td class="td1">-l scheme</td>
- <td class="td1"><?scheme ... ?></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 << '<?xml version="1.0" ?>
- ';
- lang = 'en'
- list = ['<aaa>', 'b&b', '"ccc"']
- _buf << '<html lang="'; _buf << (lang).to_s; _buf << '">
- <body>
- <ul>
- '; for item in list
- _buf << ' <li>'; _buf << Erubis::XmlHelper.escape_xml(item); _buf << '</li>
- '; end
- _buf << ' </ul>
- </body>
- </html>
- ';
- _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 << '<?xml version="1.0" ?>
- ';
- lang = 'en'
- list = ['<aaa>', 'b&b', '"ccc"']
- _buf << '<html lang="'; _buf << (lang).to_s; _buf << '">
- <body>
- <ul>
- '; for item in list
- _buf << ' <li>'; _buf << Erubis::XmlHelper.escape_xml(item); _buf << '</li>
- '; end
- _buf << ' </ul>
- </body>
- </html>
- ';
- _buf.to_s
- </pre>
- <p><strong>(experimental)</strong> Erubis supports '<%= ... %>' pattern with PI pattern.
- </p>
- <div class="program_caption">
- example of Rails view template</div>
- <pre class="program"><table>
- <tr>
- <?rb for item in @list ?>
- <td>@{item.id}@</td>
- <td>@{item.name}@</td>
- <td>
- <strong><%=</strong> link_to 'Destroy', {:action=>'destroy', :id=>item.id},
- :confirm=>'Are you OK?' <strong>%></strong>
- </td>
- <?rb end ?>
- </tr>
- </table>
- </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"><?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
- <body>
- <h3>List</h3>
- <% if @list.nil? || @list.empty? %>
- <p>not found.</p>
- <% else %>
- <table>
- <tbody>
- <% @list.each_with_index do |item, i| %>
- <tr bgcolor="<%= i % 2 == 0 ? '#FCC' : '#CCF' %>">
- <td><%= item %></td>
- </tr>
- <% end %>
- </tbody>
- </table>
- <% end %>
- </body>
- </html>
- </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 << ( i % 2 == 0 ? '#FCC' : '#CCF' ).to_s;
- _buf << ( 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 << ( i % 2 == 0 ? '#FCC' : '#CCF' ).to_s;
- _buf << ( 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 << ( i % 2 == 0 ? '#FCC' : '#CCF' ).to_s;
- _buf << ( 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 << ( i % 2 == 0 ? '#FCC' : '#CCF' ).to_s;
- 14: _buf << ( 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"><?xml version="1.0"?>
- <html>
- <body>
- <h3>List</h3>
- <?php if (!$list) { ?>
- <p>not found.</p>
- <?php } else { ?>
- <table>
- <tbody>
- <?php $i = 0; ?>
- <?php foreach ($list as $item) { ?>
- <tr bgcolor="<?php echo ++$i % 2 == 1 ? '#FCC' : '#CCF'; ?>">
- <td><?php echo $item; ?></td>
- </tr>
- <?php } ?>
- </tbody>
- </table>
- <?php } ?>
- </body>
- </html>
- </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: <?php if (!$list) { ?>
- 7: <?php } else { ?>
- 10: <?php $i = 0; ?>
- 11: <?php foreach ($list as $item) { ?>
- 12: <?php echo ++$i % 2 == 1 ? '#FCC' : '#CCF'; ?>
- 13: <?php echo $item; ?>
- 15: <?php } ?>
- 18: <?php } ?>
- </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 < 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 '<%= %>' to escaped and '<%== %>' 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 << ...".
- </dd>
- <dt class="dt1">
- <a href="#printenabled-enhancer">PrintEnabledEnhancer</a> (only for Eruby)</dt>
- <dd class="dd1">
- Enable to use print() in '<% ... %>'.
- </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 '<% %>'.
- </dd>
- <dt class="dt1">
- <a href="#bipattern-enhancer">BiPatternEnhancer</a> (language-independent)</dt>
- <dd class="dd1">
- [experimental] Enable to use another embedded pattern with '<% %>'.
- </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 '<p><%= text %></p>' into '_buf << %Q`<p>#{text}</p>`'.
- </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 '<%= ... %>' to escaped and '<%== ... %>' to unescaped.
- </p>
- <a name="example.eruby"></a>
- <div class="program_caption">
- example.eruby</div>
- <pre class="program"><div>
- <% for item in list %>
- <p><%= item %></p>
- <p><%== item %></p>
- <% end %>
- </div>
- </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 << '<div>
- '; for item in list
- _buf << ' <p>'; <strong>_buf << Erubis::XmlHelper.escape_xml( item );</strong> _buf << '</p>
- <p>'; <strong>_buf << ( item ).to_s;</strong> _buf << '</p>
- '; end
- _buf << '</div>
- ';
- _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 << '<div>
- '; for item in list
- _buf << ' <p>'; _buf << ( item ).to_s; _buf << '</p>
- <p>'; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << '</p>
- '; end
- _buf << '</div>
- ';
- <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 << ...'.
- </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> '<div>
- '; for item in list
- <strong>print</strong> ' <p>'; <strong>print</strong>(( item ).to_s); <strong>print</strong> '</p>
- <p>'; <strong>print</strong> Erubis::XmlHelper.escape_xml( item ); <strong>print</strong> '</p>
- '; end
- <strong>print</strong> '</div>
- ';
- </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 '<% ... %>'.
- </p>
- <a name="printenabled-example.eruby"></a>
- <div class="program_caption">
- printenabled-example.eruby</div>
- <pre class="program"><% for item in @list %>
- <b><strong><% print item %></strong></b>
- <% end %>
- </pre>
- <a name="printenabled-example.rb"></a>
- <div class="program_caption">
- printenabled-example.rb</div>
- <pre class="program">require 'erubis'
- class PrintEnabledEruby < Erubis::Eruby
- include Erubis::PrintEnabledEnhancer
- end
- input = File.read('printenabled-example.eruby')
- eruby = PrintEnabledEruby.new(input)
- list = ['aaa', 'bbb', 'ccc']
- print eruby.evaluate(:list=>list)
- </pre>
- <a name="printenable_example.result"></a>
- <div class="terminal_caption">
- output result</div>
- <pre class="terminal">$ ruby printenabled-example.rb
- <b>aaa</b>
- <b>bbb</b>
- <b>ccc</b>
- </pre>
- <p>Notice to use Eruby#evaluate() and not to use Eruby#result(),
- because print() method in '<% ... %>' 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 << '<div>
- '; for item in list
- _buf << ' <p>'; _buf << ( item ).to_s; _buf << '</p>
- <p>'; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << '</p>
- '; end
- _buf << '</div>
- ';
- <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 << '<div>
- '; for item in list
- _buf << ' <p>'; _buf << ( item ).to_s; _buf << '</p>
- <p>'; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << '</p>
- '; end
- _buf << '</div>
- ';
- <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 << '<div>
- '; for item in list
- _buf << ' <p>'; _buf << ( item ).to_s; _buf << '</p>
- <p>'; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << '</p>
- '; end
- _buf << '</div>
- ';
- <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 << '<div>
- '; for item in list
- _buf << ' <p>'; _buf << ( item ).to_s; _buf << '</p>
- <p>'; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << '</p>
- '; end
- _buf << '</div>
- ';
- _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"><h3>List</h3>
- <% if !@list || @list.empty? %>
- <p>not found.</p>
- <% else %>
- <table>
- <tbody>
- <% @list.each_with_index do |item, i| %>
- <tr bgcolor="<%= i%2 == 0 ? '#FFCCCC' : '#CCCCFF' %>">
- <td><%= item %></td>
- </tr>
- <% end %>
- </tbody>
- </table>
- <% end %>
- </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 << ( i%2 == 0 ? '#FFCCCC' : '#CCCCFF' ).to_s;
- _buf << ( 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"><h3>List</h3>
- <% if !@list || @list.empty? %>
- <p>not found.</p>
- <% else %>
- <table>
- <tbody>
- <% @list.each_with_index do |item, i| %>
- <tr bgcolor="<%= i%2 == 0 ? '#FFCCCC' : '#CCCCFF' %>">
- <td><%= item %></td>
- </tr>
- <% end %>
- </tbody>
- </table>
- <% end %>
- </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
- <h3>List</h3>
- <p>not found.</p>
- <table>
- <tbody>
- <tr bgcolor="">
- <td></td>
- </tr>
- </tbody>
- </table>
- </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 '<% %>'.
- </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 << '<div>
- '; for item in list ; _buf << '
- <p>'; _buf << ( item ).to_s; _buf << '</p>
- <p>'; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << '</p>
- '; end ; _buf << '
- </div>
- ';
- _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 '<% %>'.
- 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"><% for item in list %>
- <b><strong>[= item =]</strong></b>
- <b><strong>[== item…
Large files files are truncated, but you can click here to view the full file