diff options
author | makoto kuwata <kwa@kuwata-lab.com> | 2006-05-07 09:33:34 +0000 |
---|---|---|
committer | makoto kuwata <kwa@kuwata-lab.com> | 2006-05-07 09:33:34 +0000 |
commit | 38a6e0cdd97f780764c6bff548c185dae2d18dcf (patch) | |
tree | db1b8c478384615a541cf62825bf3d08360fa748 /doc | |
parent | 0f4453c60d6f1a1992ac5f7a085c03fc5b77269e (diff) | |
download | erubis-38a6e0cdd97f780764c6bff548c185dae2d18dcf.tar.gz |
- [enhance] new module NoTextEnhancer added
- [change] Eruby#add_stmt() desn't add ';' when the last character is ?\n
- [bugfix] PrintEnableEnhancer#evaluate() added to evaluate with it's context
Diffstat (limited to 'doc')
-rw-r--r-- | doc/users-guide.html | 511 | ||||
-rw-r--r-- | doc/users-guide.txt | 497 |
2 files changed, 615 insertions, 393 deletions
diff --git a/doc/users-guide.html b/doc/users-guide.html index b623db5..239bf73 100644 --- a/doc/users-guide.html +++ b/doc/users-guide.html @@ -73,7 +73,7 @@ It has the following features. </li> <li><a href="#enhancer">Enhancer</a> <ul> - <li><a href="#escape-enhander">EscapeEnhancer</a> + <li><a href="#escape-enhancer">EscapeEnhancer</a> </li> <li><a href="#stdout-enhancer">StdoutEnhancer</a> </li> @@ -87,6 +87,8 @@ It has the following features. </li> <li><a href="#stringbuffer-enhancer">StringBufferEnhancer</a> </li> + <li><a href="#notext-enhancer">NoTextEnhancer</a> + </li> <li><a href="#simplify-enhancer">SimplifyEnhancer</a> </li> <li><a href="#bipattern-enhancer">BiPatternEnhancer</a> @@ -103,6 +105,8 @@ It has the following features. </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> @@ -111,7 +115,15 @@ It has the following features. </li> </ul> </li> - <li><a href="#benchmark">Benchmark</a> + <li><a href="#topics">Other Topics</a> + <ul> + <li><a href="#topics-tinyeruby">TinyEruby class</a> + </li> + <li><a href="#topics-php">NoTextEnhancer in PHP</a> + </li> + <li><a href="#topics-benchmark">Benchmark</a> + </li> + </ul> </li> <li><a href="#command">Command Reference</a> <ul> @@ -178,9 +190,7 @@ $ contrib/inline-require -I lib bin/erubis > contrib/erubis example1.eruby</div> <pre class="program"><ul> <b><% for item in list %></b> - <li> - <b><%= item %></b> - </li> + <li><b><%= item %></b></li> <b><% end %></b> <b><%# here is ignored because starting with '#' %></b> </ul> @@ -205,27 +215,33 @@ output</div> ---------- script source --- _out = []; _out << '<ul> '; for item in list -; _out << ' <li> -'; _out << ' '; _out << ( item ).to_s; _out << ' -'; _out << ' </li> + _out << ' <li>'; _out << ( item ).to_s; _out << '</li> '; end -; -; _out << '</ul> + + _out << '</ul> '; _out.join ---------- result ---------- <ul> - <li> - aaa - </li> - <li> - bbb - </li> - <li> - ccc - </li> + <li>aaa</li> + <li>bbb</li> + <li>ccc</li> </ul> </pre> +<p>Erubis has command 'erubis'. Command-line option '-s' shows the compiled source code of eRuby script. +</p> +<div class="terminal_caption"> +example of command-line option '-s'</div> +<pre class="terminal">$ erubis <b>-s</b> example1.eruby +_out = []; _out << '<ul> +'; for item in list + _out << ' <li>'; _out << ( item ).to_s; _out << '</li> +'; end + + _out << '</ul> +'; +_out.join +</pre> <br> @@ -340,12 +356,11 @@ output</div> <pre class="terminal">$ ruby example3.rb 2> stderr.log ---------- script source --- _out = []; for item in list -; _out << ' <p>'; _out << Erubis::XmlHelper.escape_xml( item ); _out << '</p> - <p>'; _out << ( item ).to_s; _out << '</p> - <p>'; $stderr.puts("*** debug: item=#{(item).inspect}"); _out << '</p> + _out << ' <p>'; <b>_out << Erubis::XmlHelper.escape_xml( item );</b> _out << '</p> + <p>'; <b>_out << ( item ).to_s;</b> _out << '</p> + <p>'; <b>$stderr.puts("*** debug: item=#{(item).inspect}");</b> _out << '</p> '; end -; _out.join ---------- result ---------- <p>&lt;aaa&gt;</p> @@ -370,12 +385,11 @@ This is available in any language. </p> <pre class="terminal">$ erubis -l ruby <b>-e Escape</b> example3.eruby _out = []; for item in list -; _out << ' <p>'; _out << Erubis::XmlHelper.escape_xml( item ); _out << '</p> + _out << ' <p>'; _out << Erubis::XmlHelper.escape_xml( item ); _out << '</p> <p>'; _out << ( item ).to_s; _out << '</p> <p>'; $stderr.puts("*** debug: item=#{(item).inspect}"); _out << '</p> '; end -; _out.join </pre> <p>Escaping function (default 'Erubis::XmlHelper.escape_xml()') can be changed by command-line property '--escape=xxx' or by overriding Erubis::Eruby#escaped_expr() in subclass. @@ -383,14 +397,14 @@ _out.join <div class="program_caption"> example to override Erubis::Eruby#escaped_expr()</div> <pre class="program">class CGIEruby < Erubis::Eruby - def escaped_expr(code) + def <b>escaped_expr(code)</b> return "CGI.escapeHTML((#{code.strip}).to_s)" #return "h(#{code.strip})" end end class LatexEruby < Erubi::Eruby - def escaped_expr(code) + def <b>escaped_expr(code)</b> return "(#{code}).gsub(/[%\\]/,'\\\\\&')" end end @@ -435,9 +449,8 @@ output</div> <pre class="terminal">$ ruby example4.rb ---------- script source --- _out = []; for item in list -; _out << ' <p>'; _out << ( item ).to_s; _out << '</p> + _out << ' <p>'; _out << ( item ).to_s; _out << '</p> '; end -; _out.join ---------- result ---------- <p>aaa</p> @@ -535,6 +548,7 @@ example6.rb</div> attr_accessor :val, :list end +## any object can be a context object <b>mydata = MyData.new</b> <b>mydata.val = 'Erubis Example'</b> <b>mydata.list = ['aaa', 'bbb', 'ccc']</b> @@ -543,9 +557,9 @@ require 'erubis' eruby = Erubis::Eruby.new(File.read('example5.eruby')) puts eruby.evaluate(<b>mydata</b>) </pre> -<div class="program_caption"> +<div class="terminal_caption"> output</div> -<pre class="program">$ ruby example6.rb +<pre class="terminal">$ ruby example6.rb <span>Erubis Example</span> <ul> <li>aaa</li> @@ -668,15 +682,13 @@ puts eruby2.src # don't print preamble and postamble output</div> <pre class="terminal">$ ruby example8.rb _out = []; for item in @list -; _out << ' <b>'; _out << ( item ).to_s; _out << '</b> + _out << ' <b>'; _out << ( item ).to_s; _out << '</b> '; end -; _out.join -------------- for item in @list -; _out << ' <b>'; _out << ( item ).to_s; _out << '</b> + _out << ' <b>'; _out << ( item ).to_s; _out << '</b> '; end -; </pre> <p>The command-line option '-b' specify both :preamble and :postamble to false. </p> @@ -703,63 +715,68 @@ end <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 -l ruby -e Escape,PercentLine,BiPattern example.eruby +<pre class="terminal">$ erubis -se Escape,PercentLine,BiPattern example.eruby </pre> <p>The following is the list of enhancers. </p> <dl class="dl1"> <dt class="dt1"> -<b>EscapeEnhander</b> (language-independent)</dt> +<a href="#escape-enhancer">EscapeEnhander</a> (language-independent)</dt> <dd class="dd1"> Switch '<%= %>' to escaped and '<%== %>' to unescaped. </dd> <dt class="dt1"> -<b>StdoutEnhancer</b> (only for Eruby)</dt> +<a href="#stdout-enhancer">StdoutEnhancer</a> (only for Eruby)</dt> <dd class="dd1"> Use $stdout instead of array buffer. </dd> <dt class="dt1"> -<b>PrintOutEnhancer</b> (only for Eruby)</dt> +<a href="#printout-enhancer">PrintOutEnhancer</a> (only for Eruby)</dt> <dd class="dd1"> Use "print(...)" statement insead of "_out << ...". </dd> <dt class="dt1"> -<b>PrintEnabledEnhancer</b> (only for Eruby)</dt> +<a href="#printenabled-enhancer">PrintEnabledEnhancer</a> (only for Eruby)</dt> <dd class="dd1"> Enable to use print() in '<% ... %>'. </dd> <dt class="dt1"> -<b>ArrayEnhancer</b> (only for Eruby)</dt> +<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"> -<b>ArrayBufferEnhancer</b> (only for Eruby)</dt> +<a href="#arraybuffer-enhancer">ArrayBufferEnhancer</a> (only for Eruby)</dt> <dd class="dd1"> Use array buffer. This is included in Erubis::Eruby by default. </dd> <dt class="dt1"> -<b>StringBufferEnhancer</b> (only for Eruby)</dt> +<a href="#stringbuffer-enhancer">StringBufferEnhancer</a> (only for Eruby)</dt> <dd class="dd1"> Use string buffer. It is a little slower than ArrayBufferEnhancer. </dd> <dt class="dt1"> -<b>SimplifiedEnhancer</b> (language-independent)</dt> +<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="#simplify-enhancer">SimplifyEnhancer</a> (language-independent)</dt> <dd class="dd1"> Make compile faster but don't trim spaces around '<% %>'. </dd> <dt class="dt1"> -<b>BiPatternEnhancer</b> (language-independent)</dt> +<a href="#bipattern-enhancer">BiPatternEnhancer</a> (language-independent)</dt> <dd class="dd1"> [experimental] Enable to use another embedded pattern with '<% %>'. </dd> <dt class="dt1"> -<b>PercentLineEnhancer</b> (language-independent)</dt> +<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"> -<b>HeaderFooterEnhancer</b> (language-independent)</dt> +<a href="#headerfooter-enhancer">HeaderFooterEnhancer</a> (language-independent)</dt> <dd class="dd1"> [experimental] Enable you to add header and footer in eRuby script. </dd> @@ -767,36 +784,30 @@ The following is an example to use some enhancers in command-line. <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-enhander"></a> +<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"><% for item in list %> - <b><%= item %></b> - <b><%== item %></b> +<pre class="program"><div> +<% for item in list %> + <p><%= item %></p> + <p><%== item %></p> <% end %> -</pre> -<a name="escape-enhancer-test.rb"></a> -<div class="program_caption"> -escape-enhancer-test.rb</div> -<pre class="program">require 'erubis' -class EscapedEruby < Erubis::Eruby - include Erubis::EscapeEnhancer -end -eruby = EscapedEruby.new(File.read('example.eruby')) -print eruby.src +</div> </pre> <div class="terminal_caption"> compiled source code</div> -<pre class="terminal">$ ruby escape-enhancer-test.rb -_out = []; for item in list -; _out << ' <b>'; _out << Erubis::XmlHelper.escape_xml( item ); _out << '</b> - <b>'; _out << ( item ).to_s; _out << '</b> +<pre class="terminal">$ erubis -se Escape example.eruby +_out = []; _out << '<div> +'; for item in list + _out << ' <p>'; <b>_out << Erubis::XmlHelper.escape_xml( item );</b> _out << '</p> + <p>'; <b>_out << ( item ).to_s;</b> _out << '</p> '; end -; + _out << '</div> +'; _out.join </pre> <p>EscapeEnhancer is language-independent. @@ -809,24 +820,16 @@ _out.join <p>StdoutEnhancer use $sdtdout instead of array buffer. Therefore, you can use 'print' statement in embedded ruby code. </p> -<a name="stdout-enhancer-test.rb"></a> -<div class="program_caption"> -stdout-enhancer-test.rb</div> -<pre class="program">require 'erubis' -class StdoutEruby < Erubis::Eruby - include Erubis::StdoutEnhancer -end -eruby = StdoutEruby.new(File.read('example.eruby')) -print eruby.src -</pre> <div class="terminal_caption"> compiled source code</div> -<pre class="terminal">$ ruby stdout-enhancer-test.rb -_out = $stdout; for item in list -; _out << ' <b>'; _out << ( item ).to_s; _out << '</b> - <b>'; _out << Erubis::XmlHelper.escape_xml( item ); _out << '</b> +<pre class="terminal">$ erubis -se Stdout example.eruby +<b>_out = $stdout;</b> _out << '<div> +'; for item in list + _out << ' <p>'; _out << ( item ).to_s; _out << '</p> + <p>'; _out << Erubis::XmlHelper.escape_xml( item ); _out << '</p> '; end -; + _out << '</div> +'; '' </pre> <p>StdoutEnhancer is only for Eruby. @@ -838,24 +841,16 @@ _out = $stdout; for item in list <h3 class="section2">PrintOutEnhancer</h3> <p>PrintOutEnhancer makes compiled source code to use 'print(...)' instead of '_out << ...'. </p> -<a name="printstatement-enhancer-test.rb"></a> -<div class="program_caption"> -printstatement-enhancer-test.rb</div> -<pre class="program">require 'erubis' -class PrintOutEruby < Erubis::Eruby - include Erubis::PrintOutEnhancer -end -eruby = PrintOutEruby.new(File.read('example.eruby')) -print eruby.src -</pre> <div class="terminal_caption"> compiled source code</div> -<pre class="terminal">$ ruby printstatement-enhancer-test.rb - for item in list -; print ' <b>'; print(( item ).to_s); print '</b> - <b>'; print Erubis::XmlHelper.escape_xml( item ); print '</b> +<pre class="terminal">$ erubis -se PrintOut example.eruby + <b>print</b> '<div> +'; for item in list + <b>print</b> ' <p>'; <b>print</b>(( item ).to_s); <b>print</b> '</p> + <p>'; <b>print</b> Erubis::XmlHelper.escape_xml( item ); <b>print</b> '</p> '; end -; + <b>print</b> '</div> +'; </pre> <p>PrintOutEnhancer is only for Eruby. </p> @@ -866,31 +861,31 @@ compiled source code</div> <h3 class="section2">PrintEnabledEnhancer</h3> <p>PrintEnabledEnhancer enables you to use print() method in '<% ... %>'. </p> -<a name="printenabled-enhancer-test.eruby"></a> +<a name="printenabled-example.eruby"></a> <div class="program_caption"> -printenabled-enhancer-test.eruby</div> +printenabled-example.eruby</div> <pre class="program"><% for item in @list %> <b><b><% print item %></b></b> <% end %> </pre> -<a name="printenabled-enhancer-test.rb"></a> +<a name="printenabled-example.rb"></a> <div class="program_caption"> -printenabled-enhancer-test.rb</div> +printenabled-example.rb</div> <pre class="program">require 'erubis' class PrintEnabledEruby < Erubis::Eruby include Erubis::PrintEnabledEnhancer end -input = File.read('printenabled-enhancer-test.eruby') +input = File.read('printenabled-example.eruby') eruby = PrintEnabledEruby.new(input) list = ['aaa', 'bbb', 'ccc'] print eruby.evaluate(:list=>list) </pre> <div class="terminal_caption"> output result</div> -<pre class="terminal">$ ruby printenabled-enhancer-test.rb -aaabbbccc <b></b> - <b></b> - <b></b> +<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(). @@ -904,25 +899,16 @@ because print() method in '<% ... %>' invokes not Kernel#print() but Print <h3 class="section2">ArrayEnhancer</h3> <p>ArrayEnhancer makes Eruby to return an array of strings. </p> -<a name="array-enhancer-test.rb"></a> -<div class="program_caption"> -array-enhancer-test.rb</div> -<pre class="program">require 'erubis' -class ArrayEruby < Erubis::Eruby - include Erubis::ArrayEnhancer -end -eruby = ArrayEruby.new(File.read('example.eruby')) -print eruby.src -</pre> <div class="terminal_caption"> compiled source code</div> -<pre class="terminal">$ ruby array-enhancer-test.rb -_out = []; for item in list -; _out << ' <b>'; _out << ( item ).to_s; _out << '</b> - <b>'; _out << Erubis::XmlHelper.escape_xml( item ); _out << '</b> +<pre class="terminal"><b>_out = [];</b> _out << '<div> +'; for item in list + _out << ' <p>'; _out << ( item ).to_s; _out << '</p> + <p>'; _out << Erubis::XmlHelper.escape_xml( item ); _out << '</p> '; end -; -_out + _out << '</div> +'; +<b>_out</b> </pre> <p>ArrayEnhancer is only for Eruby. </p> @@ -944,26 +930,65 @@ Erubis::Eruby includes this enhancer by default. <h3 class="section2">StringBufferEnhancer</h3> <p>StringBufferEnhancer makes Eruby to use string buffer. </p> -<a name="stringbuffer-enhancer-test.rb"></a> +<pre class="terminal">$ erubis -se StringBuffer example.eruby +<b>_out = '';</b> _out << '<div> +'; for item in list + _out << ' <p>'; _out << ( item ).to_s; _out << '</p> + <p>'; _out << Erubis::XmlHelper.escape_xml( item ); _out << '</p> +'; end + _out << '</div> +'; +<b>_out</b> +</pre> +<p>StringBufferEnhancer 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"> -stringbuffer-enhancer-test.rb</div> -<pre class="program">require 'erubis' -class StringBufferEruby < Erubis::Eruby - include Erubis::StringBufferEnhancer -end -eruby = StringBufferEruby.new(File.read('example.eruby')) -print eruby.src +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> <div class="terminal_caption"> -compiled source code</div> -<pre class="terminal">$ ruby stringbuffer-enhancer-test.rb -_out = ''; for item in list -; _out << ' <b>'; _out << ( item ).to_s; _out << '</b> - <b>'; _out << Erubis::XmlHelper.escape_xml( item ); _out << '</b> -'; end -; -_out +output example of NoTextEnhancer</div> +<pre class="terminal">$ erubis <b>-se NoText</b> notext-example.eruby +_out = []; + if !@list || @list.empty? + + else + + + @list.each_with_index do |item, i| + _out << ( i%2 == 0 ? '#FFCCCC' : '#CCCCFF' ).to_s; + _out << ( item ).to_s; + + end + + + end +_out.join </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> @@ -971,26 +996,20 @@ _out <h3 class="section2">SimplifyEnhancer</h3> <p>SimplifyEnhancer makes compiling a little faster but don't trim spaces around '<% %>'. </p> -<a name="simplify-enhancer-test.rb"></a> -<div class="program_caption"> -simplify-enhancer-test.rb</div> -<pre class="program">require 'erubis' -class SimplifiedEruby < Erubis::Eruby - include Erubis::SimplifyEnhancer -end -eruby = SimplifiedEruby.new(File.read('example.eruby')) -print eruby.src -</pre> <div class="terminal_caption"> compiled source code</div> -<pre class="terminal">$ ruby simplify-enhancer-test.rb -_out = []; for item in list ; _out << ' - <b>'; _out << ( item ).to_s; _out << '</b> - <b>'; _out << Erubis::XmlHelper.escape_xml( item ); _out << '</b> +<pre class="terminal">$ erubis -se Simplify example.euby +_out = []; _out << '<div> +'; for item in list ; _out << ' + <p>'; _out << ( item ).to_s; _out << '</p> + <p>'; _out << Erubis::XmlHelper.escape_xml( item ); _out << '</p> '; end ; _out << ' +</div> '; _out.join </pre> +<p>SimplifyEnhancer is language-independent. +</p> <br> @@ -1000,21 +1019,9 @@ _out.join By Default, '[= ... =]' is available for expression. You can specify pattern by :bipattern property. </p> -<a name="bipattern-enhancer-test.rb"></a> +<a name="bipattern-example.rhtml"></a> <div class="program_caption"> -bipattern-enhancer-test.rb</div> -<pre class="program">require 'erubis' -class BiPatternEruby < Erubis::Eruby - include Erubis::BiPatternEnhancer -end - -input = File.read('bipattern-enhancer-test.rhtml') -eruby = BiPatternEruby.new(input) -print eruby.src -</pre> -<a name="bipattern-enhancer-test.rhtml"></a> -<div class="program_caption"> -bipattern-enhancer-test.rhtml</div> +bipattern-example.rhtml</div> <pre class="program"><% for item in list %> <b><b>[= item =]</b></b> <b><b>[== item =]</b></b> @@ -1022,12 +1029,11 @@ bipattern-enhancer-test.rhtml</div> </pre> <div class="terminal_caption"> compiled source code</div> -<pre class="terminal">$ ruby bipattern-enhancer-test.rb +<pre class="terminal">$ erubis -se BiPattern bipattern-example.rhtml _out = []; for item in list -; _out << ' <b>'; _out << ( item ).to_s; _out << '</b> - <b>'; _out << Erubis::XmlHelper.escape_xml( item ); _out << '</b> + _out << ' <b>'; <b>_out << ( item ).to_s;</b> _out << '</b> + <b>'; <b>_out << Erubis::XmlHelper.escape_xml( item );</b> _out << '</b> '; end -; _out.join </pre> <p>BiPatternEnhancer is language-independent. @@ -1040,21 +1046,9 @@ _out.join <p>PercentLineEnhancer regards lines starting with '%' as Ruby code. This is for compatibility with eruby and ERB. </p> -<a name="percentline-enhancer-test.rb"></a> -<div class="program_caption"> -percentline-enhancer-test.rb</div> -<pre class="program">require 'erubis' -class PercentLineEruby < Erubis::Eruby - include Erubis::PercentLineEnhancer -end - -input = File.read('percentline-enhancer-test.rhtml') -eruby = PercentLineEruby.new(input) -print eruby.src -</pre> -<a name="percentline-enhancer-test.rhtml"></a> +<a name="percentline-example.rhtml"></a> <div class="program_caption"> -percentline-enhancer-test.rhtml</div> +percentline-example.rhtml</div> <pre class="program"><b>% for item in list</b> <b><%= item %></b> <b>% end</b> @@ -1062,11 +1056,11 @@ percentline-enhancer-test.rhtml</div> </pre> <div class="terminal_caption"> compiled source code</div> -<pre class="terminal">$ ruby percentline-enhancer-test.rb -_out = []; for item in list -; _out << ' <b>'; _out << ( item ).to_s; _out << '</b> -'; end -; _out << '% lines with \'%%\' +<pre class="terminal">$ erubis -se PercentLine percentline-example.rhtml +_out = []; <b>for item in list</b> + _out << ' <b>'; _out << ( item ).to_s; _out << '</b> +'; <b>end</b> + _out << '% lines with \'%%\' '; _out.join </pre> @@ -1081,21 +1075,9 @@ _out.join </p> <p>HeaderFooterEnhancer enables you to add header and footer in eRuby script. </p> -<a name="headerfooter-enhancer-test.rb"></a> +<a name="headerfooter-example.eruby"></a> <div class="program_caption"> -headerfooter-enhancer-test.rb</div> -<pre class="program">require 'erubis' -class HeaderFooterEruby < Erubis::Eruby - include Erubis::HeaderFooterEnhancer -end - -input = File.read('headerfooter-enhancer-test.eruby') -eruby = HeaderFooterEruby.new(input) -print eruby.src -</pre> -<a name="headerfooter-enhancer-test.eruby"></a> -<div class="program_caption"> -headerfooter-enhancer-test.eruby</div> +headerfooter-example.eruby</div> <pre class="program"><b><!--#header:</b> <b>def list_items(items)</b> <b>#--></b> @@ -1108,17 +1090,16 @@ headerfooter-enhancer-test.eruby</div> </pre> <div class="terminal_caption"> compiled source code</div> -<pre class="terminal">$ ruby headerfooter-enhancer-test.rb +<pre class="terminal">$ erubis -se HeaderFooter headerfooter-example.eruby -def list_items(items) +<b>def list_items(items)</b> _out = []; for item in items -; _out << ' <b>'; _out << ( item ).to_s; _out << '</b> + _out << ' <b>'; _out << ( item ).to_s; _out << '</b> '; end -; _out.join -end +<b>end</b> </pre> <p>Compare to the following: @@ -1140,23 +1121,22 @@ normal-eruby-test.eruby</div> compiled source code</div> <pre class="terminal">$ erubis -s normal-eruby-test.eruby _out = []; -def list_items(items) +<b>def list_items(items)</b> -; for item in items -; _out << '<li>'; _out << ( item ).to_s; _out << '</li> + for item in items + _out << '<li>'; _out << ( item ).to_s; _out << '</li> '; end -; -end -; +<b>end</b> + _out.join </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-enhancer-test2.rhtml"></a> +<a name="headerfooter-example2.rhtml"></a> <div class="program_caption"> -headerfooter-enhancer-test2.rhtml</div> +headerfooter-example2.rhtml</div> <pre class="program"><?xml version="1.0"?> <html> <b><!--#header:</b> @@ -1170,9 +1150,9 @@ headerfooter-enhancer-test2.rhtml</div> </pre> <div class="terminal_caption"> compiled source code</div> -<pre class="terminal">$ ruby headerfooter-enhancer-test2.rb +<pre class="terminal">$ erubis -se HeaderFooter headerfooter-example2.rhtml -def page(list) +<b>def page(list)</b> _out = []; _out << '<?xml version="1.0"?> <html> @@ -1181,7 +1161,7 @@ _out = []; _out << '<?xml version="1.0"?> '; _out.join -end +<b>end</b> </pre> <p>HeaderFooterEnhancer is experimental and is language-independent. @@ -1223,7 +1203,7 @@ example.ephp</div> <p>Hello <b><%= $user %></b>!</p> <table> <tbody> - <b><% $i = 0 %></b> + <b><% $i = 0; %></b> <b><% foreach ($list as $item) { %></b> <b><% $i++; %></b> <tr bgcolor=<b>"<%= $i % 2 == 0 ? '#FFCCCC' : '#CCCCFF' %>"</b>> @@ -1245,7 +1225,7 @@ compiled source code</div> <p>Hello <?php echo $user; ?>!</p> <table> <tbody> -<?php $i = 0 ?> +<?php $i = 0; ?> <?php foreach ($list as $item) { ?> <?php $i++; ?> <tr bgcolor="<?php echo $i % 2 == 0 ? '#FFCCCC' : '#CCCCFF'; ?>"> @@ -1326,6 +1306,11 @@ fputs(" </tbody>\n" } </pre> +<br> + + +<a name="lang-java"></a> +<h3 class="section2">Java</h3> <a name="Example.ejava"></a> <div class="program_caption"> Example.ejava</div> @@ -1341,6 +1326,7 @@ public class Example { } public String view() { + StringBuffer _out = new StringBuffer(); %></b> <html> <body> @@ -1358,6 +1344,7 @@ public class Example { <body> </html> <b><% + return _out.toString(); } public static void main(String[] args) { @@ -1385,7 +1372,7 @@ public class Example { </pre> <div class="terminal_caption"> compiled source code</div> -<pre class="terminal">$ erubis -l java example.ejava +<pre class="terminal">$ erubis -b -l java example.ejava StringBuffer _out = new StringBuffer(); import java.util.*; @@ -1398,6 +1385,7 @@ public class Example { } public String view() { + StringBuffer _out = new StringBuffer(); _out.append("<html>\n" + " <body>\n" @@ -1415,6 +1403,7 @@ _out.append(" </tbody>\n" + " <body>\n" + "</html>\n"); + return _out.toString(); } public static void main(String[] args) { @@ -1625,7 +1614,7 @@ print(' </table> <div class="terminal_caption"> compiled source code</div> <pre class="terminal">$ erubis -l js example.ejs -_out = []; +var _out = []; var user = 'Erubis'; var list = ['<aaa>', 'b&b', '"ccc"']; @@ -1653,8 +1642,73 @@ document.write(_out.join("")); <br> -<a name="benchmark"></a> -<h2 class="section1">Benchmark</h2> +<a name="topics"></a> +<h2 class="section1">Other Topics</h2> +<a name="topics-tinyeruby"></a> +<h3 class="section2">TinyEruby class</h3> +<p>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 TinyEruby class. +</p> +<br> + + +<a name="topics-php"></a> +<h3 class="section2">NoTextEnhancer in PHP</h3> +<p>NoTextEnhancer is quite useful not only for eRuby but also for PHP. +It can "drop" HTML text and show up embedded Ruby/PHP code. +</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"><html> + <body> + <h3>List</h3> + <?php if (!$list || count($list) == 0) { ?> + <p>not found.</p> + <?php } else { ?> + <table> + <tbody> + <?php $i = 0; ?> + <?php foreach ($list as $item) { ?> + <tr bgcolor="<?php echo ++$i % 2 == 1 ? '#FFCCCC' : '#CCCCFF'; ?>"> + <td><?php echo $item; ?></td> + </tr> + <?php } ?> + </tbody> + </table> + <?php } ?> + </body> +</html> +</pre> +<p>This is complex because PHP code and HTML document are mixed. +NoTextEnhancer can separate PHP code from HTML document. +</p> +<div class="terminal_caption"> +example of using NoTextEnhancer with PHP file</div> +<pre class="terminal">$ erubis -l php -e NoText -p '<\?php \?>' notext-example.php | uniq + +<?php if (!$list || count($list) == 0) { ?> + +<?php } else { ?> + +<?php $i = 0; ?> +<?php foreach ($list as $item) { ?> +<?php echo ++$i % 2 == 1 ? '#FFCCCC' : '#CCCCFF'; ?> +<?php echo $item; ?> + +<?php } ?> + +<?php } ?> + +</pre> +<br> + + +<a name="topics-benchmark"></a> +<h3 class="section2">Benchmark</h3> <p>A benchmark script is included in Erubis package at erubis-X.X.X/benchark directory. Here is an example result of benchmark. </p> @@ -1668,11 +1722,8 @@ Erubis::StringBufferEruby 186.130000 2.600000 188.730000 (190.374098) Erubis::SimplifiedEruby 130.100000 2.210000 132.310000 (133.426010) Erubis::StdoutEruby 106.010000 2.130000 108.140000 (108.999193) Erubis::StdoutSimplifiedEruby 97.130000 2.180000 99.310000 (100.104433) -Erubis::PrintOutEruby 109.900000 2.090000 111.990000 (112.854442) -Erubis::PrintOutSimplifiedEruby 93.120000 2.140000 95.260000 ( 96.002970) Erubis::TinyEruby 118.740000 2.360000 121.100000 (122.141380) Erubis::TinyStdoutEruby 86.140000 1.840000 87.980000 ( 88.679196) -Erubis::TinyPrintEruby 86.540000 1.970000 88.510000 ( 89.208078) </pre> <div class="output_caption"> Env: MacOS X 10.4, PowerPC 1.42GHz, Mem1.5GB, Ruby1.8.4</div> @@ -1684,11 +1735,8 @@ Erubis::StringBufferEruby 47.270000 1.980000 49.250000 ( 73.867537) Erubis::SimplifiedEruby 34.310000 1.600000 35.910000 ( 51.762841) Erubis::StdoutEruby 26.240000 1.490000 27.730000 ( 41.840430) Erubis::StdoutSimplifiedEruby 25.380000 1.340000 26.720000 ( 37.231918) -Erubis::PrintOutEruby 26.850000 1.260000 28.110000 ( 38.378227) -Erubis::PrintOutSimplifiedEruby 24.160000 1.280000 25.440000 ( 40.048199) Erubis::TinyEruby 31.690000 1.590000 33.280000 ( 49.862091) Erubis::TinyStdoutEruby 22.550000 1.230000 23.780000 ( 33.316978) -Erubis::TinyPrintEruby 22.340000 1.150000 23.490000 ( 33.577150) </pre> <p>This shows that: </p> @@ -1709,6 +1757,9 @@ Erubis::TinyPrintEruby 22.340000 1.150000 23.490000 ( 33.577150) <br> +<br> + + <a name="command"></a> <h2 class="section1">Command Reference</h2> <a name="command-usage"></a> diff --git a/doc/users-guide.txt b/doc/users-guide.txt index 2549f22..ebe64c6 100644 --- a/doc/users-guide.txt +++ b/doc/users-guide.txt @@ -69,15 +69,13 @@ Erubis is implemented in pure Ruby. It requires Ruby 1.8 or higher. .$$ Basic Example | tut-basic -Here is a most basic example of Erubis. +Here is a basic example of Erubis. .? example1.eruby .-------------------- example1.eruby <ul> {{*<% for item in list %>*}} - <li> - {{*<%= item %>*}} - </li> + <li>{{*<%= item %>*}}</li> {{*<% end %>*}} {{*<%# here is ignored because starting with '#' %>*}} </ul> @@ -103,6 +101,14 @@ $ ruby example1.rb .<<<:! (cd guide.d; ruby example1.rb) .==================== +Erubis has command 'erubis'. Command-line option '-s' shows the compiled source code of eRuby script. + +.? example of command-line option '-s' +.==================== +$ erubis {{*-s*}} example1.eruby +.<<<:! (cd guide.d; erubis -s example1.eruby) +.==================== + .$$ Trimming Spaces | tut-trim @@ -168,7 +174,7 @@ $ ruby example2.rb -.$$ Escape | tut-escape +.$$ Escape | tut-escape Erubis have ability to escape (sanitize) expression. Erubis::Eruby class act as the following: @@ -208,7 +214,7 @@ puts eruby.result(binding()) # get result .? output .==================== $ ruby example3.rb 2> stderr.log -.<<<:! (cd guide.d; ruby example3.rb 2> stderr.log) +.<<<:! (cd guide.d; ruby example3.rb 2> stderr.log) | ruby -pe 'sub! /_out << [E(].*?;|\$stderr.*?;/, "{{*\\&*}}"' .#--- script source --- .#_out = ''; _out << "<ul>\n" .# for item in list @@ -254,14 +260,14 @@ Escaping function (default 'Erubis::XmlHelper.escape_xml()') can be changed by c .? example to override Erubis::Eruby#escaped_expr() .-------------------- class CGIEruby < Erubis::Eruby - def escaped_expr(code) + def {{*escaped_expr(code)*}} return "CGI.escapeHTML((#{code.strip}).to_s)" #return "h(#{code.strip})" end end class LatexEruby < Erubi::Eruby - def escaped_expr(code) + def {{*escaped_expr(code)*}} return "(#{code}).gsub(/[%\\]/,'\\\\\&')" end end @@ -387,6 +393,7 @@ class MyData attr_accessor :val, :list end +## any object can be a context object {{*mydata = MyData.new*}} {{*mydata.val = 'Erubis Example'*}} {{*mydata.list = ['aaa', 'bbb', 'ccc']*}} @@ -397,10 +404,10 @@ puts eruby.evaluate({{*mydata*}}) .-------------------- .? output -.-------------------- +.==================== $ ruby example6.rb .<<<:! (cd guide.d; ruby example6.rb) -.-------------------- +.==================== @@ -519,32 +526,34 @@ end You can specify enhancers in command-line with option '-e'. The following is an example to use some enhancers in command-line. .==================== -$ erubis -l ruby -e Escape,PercentLine,BiPattern example.eruby +$ erubis -se Escape,PercentLine,BiPattern example.eruby .==================== The following is the list of enhancers. -.: {{*EscapeEnhander*}} (language-independent) +.: {{<EscapeEnhander|#escape-enhancer>}} (language-independent) Switch '<%= %>' to escaped and '<%== %>' to unescaped. -.: {{*StdoutEnhancer*}} (only for Eruby) +.: {{<StdoutEnhancer|#stdout-enhancer>}} (only for Eruby) Use $stdout instead of array buffer. -.: {{*PrintOutEnhancer*}} (only for Eruby) +.: {{<PrintOutEnhancer|#printout-enhancer>}} (only for Eruby) Use "print(...)" statement insead of "_out << ...". -.: {{*PrintEnabledEnhancer*}} (only for Eruby) +.: {{<PrintEnabledEnhancer|#printenabled-enhancer>}} (only for Eruby) Enable to use print() in '<% ... %>'. -.: {{*ArrayEnhancer*}} (only for Eruby) +.: {{<ArrayEnhancer|#array-enhancer>}} (only for Eruby) Return array of string instead of returning string. -.: {{*ArrayBufferEnhancer*}} (only for Eruby) +.: {{<ArrayBufferEnhancer|#arraybuffer-enhancer>}} (only for Eruby) Use array buffer. This is included in Erubis::Eruby by default. -.: {{*StringBufferEnhancer*}} (only for Eruby) +.: {{<StringBufferEnhancer|#stringbuffer-enhancer>}} (only for Eruby) Use string buffer. It is a little slower than ArrayBufferEnhancer. -.: {{*SimplifiedEnhancer*}} (language-independent) +.: {{<NoTextEnhancer|#notext-enhancer>}} (language-independent) + Print embedded code only and ignore normal text. +.: {{<SimplifyEnhancer|#simplify-enhancer>}} (language-independent) Make compile faster but don't trim spaces around '<% %>'. -.: {{*BiPatternEnhancer*}} (language-independent) +.: {{<BiPatternEnhancer|#bipattern-enhancer>}} (language-independent) [experimental] Enable to use another embedded pattern with '<% %>'. -.: {{*PercentLineEnhancer*}} (language-independent) +.: {{<PercentLineEnhancer|#percentline-enhancer>}} (language-independent) Regard lines starting with '%' as Ruby code. This is for compatibility with eruby and ERB. -.: {{*HeaderFooterEnhancer*}} (language-independent) +.: {{<HeaderFooterEnhancer|#headerfooter-enhancer>}} (language-independent) [experimental] Enable you to add header and footer in eRuby script. @@ -563,32 +572,40 @@ For example, class BiPatternEruby includes BiPatternEnhancer. -.$$ EscapeEnhancer | escape-enhander +.$$ EscapeEnhancer | escape-enhancer EscapeEnhancer switches '<%= ... %>' to escaped and '<%== ... %>' to unescaped. .? example.eruby .-------------------- example.eruby +<div> <% for item in list %> - <b><%= item %></b> - <b><%== item %></b> + <p><%= item %></p> + <p><%== item %></p> <% end %> +</div> .-------------------- -.? escape-enhancer-test.rb -.-------------------- escape-enhancer-test.rb -require 'erubis' -class EscapedEruby < Erubis::Eruby - include Erubis::EscapeEnhancer -end -eruby = EscapedEruby.new(File.read('example.eruby')) -print eruby.src -.-------------------- +.#.? escape-example.rb +.#.-------------------- escape-example.rb +.#require 'erubis' +.#class EscapedEruby < Erubis::Eruby +.# include Erubis::EscapeEnhancer +.#end +.#eruby = EscapedEruby.new(File.read('example.eruby')) +.#print eruby.src +.#.-------------------- +.# +.#.? compiled source code +.#.==================== +.#$ ruby escape-example.rb +.#.<<<:! (cd guide.d; ruby escape-example.rb) +.#.==================== .? compiled source code .==================== -$ ruby escape-enhancer-test.rb -.<<<:! (cd guide.d; ruby escape-enhancer-test.rb) +$ erubis -se Escape example.eruby +.<<<:! erubis -se Escape guide.d/example.eruby | ruby -pe 'sub! /_out << [E(].*?;/, "{{*\\&*}}"' .==================== EscapeEnhancer is language-independent. @@ -600,20 +617,26 @@ EscapeEnhancer is language-independent. StdoutEnhancer use $sdtdout instead of array buffer. Therefore, you can use 'print' statement in embedded ruby code. -.? stdout-enhancer-test.rb -.-------------------- stdout-enhancer-test.rb -require 'erubis' -class StdoutEruby < Erubis::Eruby - include Erubis::StdoutEnhancer -end -eruby = StdoutEruby.new(File.read('example.eruby')) -print eruby.src -.-------------------- +.#.? stdout-example.rb +.#.-------------------- stdout-example.rb +.#require 'erubis' +.#class StdoutEruby < Erubis::Eruby +.# include Erubis::StdoutEnhancer +.#end +.#eruby = StdoutEruby.new(File.read('example.eruby')) +.#print eruby.src +.#.-------------------- +.# +.#.? compiled source code +.#.==================== +.#$ ruby stdout-example.rb +.#.<<<:! (cd guide.d; ruby stdout-example.rb) +.#.==================== .? compiled source code .==================== -$ ruby stdout-enhancer-test.rb -.<<<:! (cd guide.d; ruby stdout-enhancer-test.rb) +$ erubis -se Stdout example.eruby +.<<<:! erubis -se Stdout guide.d/example.eruby | ruby -pe 'sub! /^_out = .*;/, "{{*\\&*}}"' .==================== StdoutEnhancer is only for Eruby. @@ -624,20 +647,26 @@ StdoutEnhancer is only for Eruby. PrintOutEnhancer makes compiled source code to use 'print(...)' instead of '_out << ...'. -.? printstatement-enhancer-test.rb -.-------------------- printstatement-enhancer-test.rb -require 'erubis' -class PrintOutEruby < Erubis::Eruby - include Erubis::PrintOutEnhancer -end -eruby = PrintOutEruby.new(File.read('example.eruby')) -print eruby.src -.-------------------- +.#.? printstatement-example.rb +.#.-------------------- printstatement-example.rb +.#require 'erubis' +.#class PrintOutEruby < Erubis::Eruby +.# include Erubis::PrintOutEnhancer +.#end +.#eruby = PrintOutEruby.new(File.read('example.eruby')) +.#print eruby.src +.#.-------------------- +.# +.#.? compiled source code +.#.==================== +.#$ ruby printstatement-example.rb +.#.<<<:! (cd guide.d; ruby printstatement-example.rb) +.#.==================== .? compiled source code .==================== -$ ruby printstatement-enhancer-test.rb -.<<<:! (cd guide.d; ruby printstatement-enhancer-test.rb) +$ erubis -se PrintOut example.eruby +.<<<:! erubis -se PrintOut guide.d/example.eruby | ruby -pe 'gsub! /print/, "{{*\\&*}}"' .==================== PrintOutEnhancer is only for Eruby. @@ -648,20 +677,20 @@ PrintOutEnhancer is only for Eruby. PrintEnabledEnhancer enables you to use print() method in '<% ... %>'. -.? printenabled-enhancer-test.eruby -.-------------------- printenabled-enhancer-test.eruby +.? printenabled-example.eruby +.-------------------- printenabled-example.eruby <% for item in @list %> <b>{{*<% print item %>*}}</b> <% end %> .-------------------- -.? printenabled-enhancer-test.rb -.-------------------- printenabled-enhancer-test.rb +.? printenabled-example.rb +.-------------------- printenabled-example.rb require 'erubis' class PrintEnabledEruby < Erubis::Eruby include Erubis::PrintEnabledEnhancer end -input = File.read('printenabled-enhancer-test.eruby') +input = File.read('printenabled-example.eruby') eruby = PrintEnabledEruby.new(input) list = ['aaa', 'bbb', 'ccc'] print eruby.evaluate(:list=>list) @@ -669,8 +698,8 @@ print eruby.evaluate(:list=>list) .? output result .==================== -$ ruby printenabled-enhancer-test.rb -.<<<:! (cd guide.d; ruby printenabled-enhancer-test.rb) +$ ruby printenabled-example.rb +.<<<:! (cd guide.d; ruby printenabled-example.rb) .==================== Notice to use Eruby#evaluate() and not to use Eruby#result(), @@ -684,20 +713,25 @@ PrintEnabledEnhancer is only for Eruby. ArrayEnhancer makes Eruby to return an array of strings. -.? array-enhancer-test.rb -.-------------------- array-enhancer-test.rb -require 'erubis' -class ArrayEruby < Erubis::Eruby - include Erubis::ArrayEnhancer -end -eruby = ArrayEruby.new(File.read('example.eruby')) -print eruby.src -.-------------------- +.#.? array-example.rb +.#.-------------------- array-example.rb +.#require 'erubis' +.#class ArrayEruby < Erubis::Eruby +.# include Erubis::ArrayEnhancer +.#end +.#eruby = ArrayEruby.new(File.read('example.eruby')) +.#print eruby.src +.#.-------------------- +.# +.#.? compiled source code +.#.==================== +.#$ ruby array-example.rb +.#.<<<:! (cd guide.d; ruby array-example.rb) +.#.==================== .? compiled source code .==================== -$ ruby array-enhancer-test.rb -.<<<:! (cd guide.d; ruby array-enhancer-test.rb) +.<<<:! erubis -se Array guide.d/example.eruby | ruby -pe 'sub! /^_out( = \[\];)?/, "{{*\\&*}}"' .==================== ArrayEnhancer is only for Eruby. @@ -718,44 +752,99 @@ ArrayBufferEnhancer is only for Eruby. StringBufferEnhancer makes Eruby to use string buffer. -.? stringbuffer-enhancer-test.rb -.-------------------- stringbuffer-enhancer-test.rb -require 'erubis' -class StringBufferEruby < Erubis::Eruby - include Erubis::StringBufferEnhancer -end -eruby = StringBufferEruby.new(File.read('example.eruby')) -print eruby.src +.#.? stringbuffer-example.rb +.#.-------------------- stringbuffer-example.rb +.#require 'erubis' +.#class StringBufferEruby < Erubis::Eruby +.# include Erubis::StringBufferEnhancer +.#end +.#eruby = StringBufferEruby.new(File.read('example.eruby')) +.#print eruby.src +.#.-------------------- +.# +.#.? compiled source code +.#.==================== +.#$ ruby stringbuffer-example.rb +.#.<<<:! (cd guide.d; ruby stringbuffer-example.rb) +.#.==================== + +.==================== +$ erubis -se StringBuffer example.eruby +.<<<:! erubis -se StringBuffer guide.d/example.eruby | ruby -pe 'sub!(/^_out( = ..;)?/,"{{*\\&*}}")' +.==================== + +.#+++ +.--------- printen +.--------- +.#--- + + + +StringBufferEnhancer is only for Eruby. + + + +.$$ NoTextEnhancer | notext-enhancer + +NoTextEnhancer suppress output of text and prints only embedded code. +This is useful especially when debugging a complex eRuby script. + +.? notext-example.eruby +.-------------------- notext-example.eruby +<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 %> .-------------------- -.? compiled source code +.? output example of NoTextEnhancer .==================== -$ ruby stringbuffer-enhancer-test.rb -.<<<:! (cd guide.d; ruby stringbuffer-enhancer-test.rb) +$ erubis {{*-se NoText*}} notext-example.eruby +.<<<:! (cd guide.d; erubis -se NoText notext-example.eruby) .==================== +NoTextEnhancer is language-independent. It is useful even if you are PHP user, see {{<this section|#topics-php>}}. + .$$ SimplifyEnhancer | simplify-enhancer SimplifyEnhancer makes compiling a little faster but don't trim spaces around '<% %>'. -.? simplify-enhancer-test.rb -.-------------------- simplify-enhancer-test.rb -require 'erubis' -class SimplifiedEruby < Erubis::Eruby - include Erubis::SimplifyEnhancer -end -eruby = SimplifiedEruby.new(File.read('example.eruby')) -print eruby.src -.-------------------- +.#.? simplify-example.rb +.#.-------------------- simplify-example.rb +.#require 'erubis' +.#class SimplifiedEruby < Erubis::Eruby +.# include Erubis::SimplifyEnhancer +.#end +.#eruby = SimplifiedEruby.new(File.read('example.eruby')) +.#print eruby.src +.#.-------------------- +.# +.#.? compiled source code +.#.==================== +.#$ ruby simplify-example.rb +.#.<<<:! (cd guide.d; ruby simplify-example.rb) +.#.==================== .? compiled source code .==================== -$ ruby simplify-enhancer-test.rb -.<<<:! (cd guide.d; ruby simplify-enhancer-test.rb) +$ erubis -se Simplify example.euby +.<<<:! erubis -se Simplify guide.d/example.eruby .==================== +SimplifyEnhancer is language-independent. + .$$ BiPatternEnhancer | bipattern-enhancer @@ -764,30 +853,36 @@ BiPatternEnhancer enables to use another embedded pattern with '<% %>'. By Default, '[= ... =]' is available for expression. You can specify pattern by :bipattern property. -.? bipattern-enhancer-test.rb -.-------------------- bipattern-enhancer-test.rb -require 'erubis' -class BiPatternEruby < Erubis::Eruby - include Erubis::BiPatternEnhancer -end - -input = File.read('bipattern-enhancer-test.rhtml') -eruby = BiPatternEruby.new(input) -print eruby.src -.-------------------- - -.? bipattern-enhancer-test.rhtml -.-------------------- bipattern-enhancer-test.rhtml +.? bipattern-example.rhtml +.-------------------- bipattern-example.rhtml <% for item in list %> <b>{{*[= item =]*}}</b> <b>{{*[== item =]*}}</b> <% end %> .-------------------- +.#.? bipattern-example.rb +.#.-------------------- bipattern-example.rb +.#require 'erubis' +.#class BiPatternEruby < Erubis::Eruby +.# include Erubis::BiPatternEnhancer +.#end +.# +.#input = File.read('bipattern-example.rhtml') +.#eruby = BiPatternEruby.new(input) +.#print eruby.src +.#.-------------------- +.# +.#.? compiled source code +.#.==================== +.#$ ruby bipattern-example.rb +.#.<<<:! (cd guide.d; ruby bipattern-example.rb) +.#.==================== + .? compiled source code .==================== -$ ruby bipattern-enhancer-test.rb -.<<<:! (cd guide.d; ruby bipattern-enhancer-test.rb) +$ erubis -se BiPattern bipattern-example.rhtml +.<<<:! erubis -se BiPattern guide.d/bipattern-example.rhtml | ruby -pe 'sub! /_out << [E(].*;/, "{{*\\&*}}"' .==================== BiPatternEnhancer is language-independent. @@ -799,30 +894,36 @@ BiPatternEnhancer is language-independent. PercentLineEnhancer regards lines starting with '%' as Ruby code. This is for compatibility with eruby and ERB. -.? percentline-enhancer-test.rb -.-------------------- percentline-enhancer-test.rb -require 'erubis' -class PercentLineEruby < Erubis::Eruby - include Erubis::PercentLineEnhancer -end - -input = File.read('percentline-enhancer-test.rhtml') -eruby = PercentLineEruby.new(input) -print eruby.src -.-------------------- - -.? percentline-enhancer-test.rhtml -.-------------------- percentline-enhancer-test.rhtml +.? percentline-example.rhtml +.-------------------- percentline-example.rhtml {{*% for item in list*}} <b><%= item %></b> {{*% end*}} %% lines with '%%' .-------------------- +.#.? percentline-example.rb +.#.-------------------- percentline-example.rb +.#require 'erubis' +.#class PercentLineEruby < Erubis::Eruby +.# include Erubis::PercentLineEnhancer +.#end +.# +.#input = File.read('percentline-example.rhtml') +.#eruby = PercentLineEruby.new(input) +.#print eruby.src +.#.-------------------- +.# +.#.? compiled source code +.#.==================== +.#$ ruby percentline-example.rb +.#.<<<:! (cd guide.d; ruby percentline-example.rb) +.#.==================== + .? compiled source code .==================== -$ ruby percentline-enhancer-test.rb -.<<<:! (cd guide.d; ruby percentline-enhancer-test.rb) +$ erubis -se PercentLine percentline-example.rhtml +.<<<:! erubis -se PercentLine guide.d/percentline-example.rhtml | ruby -pe 'sub! /for.*?list|end/, "{{*\\&*}}"' .==================== PercentLineEnhancer is language-independent. @@ -835,20 +936,8 @@ PercentLineEnhancer is language-independent. HeaderFooterEnhancer enables you to add header and footer in eRuby script. -.? headerfooter-enhancer-test.rb -.-------------------- headerfooter-enhancer-test.rb -require 'erubis' -class HeaderFooterEruby < Erubis::Eruby - include Erubis::HeaderFooterEnhancer -end - -input = File.read('headerfooter-enhancer-test.eruby') -eruby = HeaderFooterEruby.new(input) -print eruby.src -.-------------------- - -.? headerfooter-enhancer-test.eruby -.-------------------- headerfooter-enhancer-test.eruby +.? headerfooter-example.eruby +.-------------------- headerfooter-example.eruby {{*<!--#header:*}} {{*def list_items(items)*}} {{*#-->*}} @@ -860,10 +949,28 @@ print eruby.src {{*#-->*}} .-------------------- +.#.? headerfooter-example.rb +.#.-------------------- headerfooter-example.rb +.#require 'erubis' +.#class HeaderFooterEruby < Erubis::Eruby +.# include Erubis::HeaderFooterEnhancer +.#end +.# +.#input = File.read('headerfooter-example.eruby') +.#eruby = HeaderFooterEruby.new(input) +.#print eruby.src +.#.-------------------- +.# +.#.? compiled source code +.#.==================== +.#$ ruby headerfooter-example.rb +.#.<<<:! (cd guide.d; ruby headerfooter-example.rb) +.#.==================== + .? compiled source code .==================== -$ ruby headerfooter-enhancer-test.rb -.<<<:! (cd guide.d; ruby headerfooter-enhancer-test.rb) +$ erubis -se HeaderFooter headerfooter-example.eruby +.<<<:! erubis -se HeaderFooter guide.d/headerfooter-example.eruby | ruby -pe 'sub! /^(def|end).*$/, "{{*\\&*}}"' .==================== Compare to the following: @@ -884,27 +991,27 @@ Compare to the following: .? compiled source code .==================== $ erubis -s normal-eruby-test.eruby -.<<<:! (cd guide.d; erubis -s normal-eruby-test.eruby) +.<<<:! erubis -s guide.d/normal-eruby-test.eruby | ruby -pe 'sub! /^(def|end).*$/, "{{*\\&*}}"' .==================== 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. .#+++ -.-------------------- headerfooter-enhancer-test2.rb +.-------------------- headerfooter-example2.rb require 'erubis' class HeaderFooterEruby < Erubis::Eruby include Erubis::HeaderFooterEnhancer end -input = File.read('headerfooter-enhancer-test2.rhtml') +input = File.read('headerfooter-example2.rhtml') eruby = HeaderFooterEruby.new(input) print eruby.src .-------------------- .#--- -.? headerfooter-enhancer-test2.rhtml -.-------------------- headerfooter-enhancer-test2.rhtml +.? headerfooter-example2.rhtml +.-------------------- headerfooter-example2.rhtml <?xml version="1.0"?> <html> {{*<!--#header:*}} @@ -917,10 +1024,16 @@ print eruby.src </html> .-------------------- +.#.? compiled source code +.#.==================== +.#$ ruby headerfooter-example2.rb +.#.<<<:! (cd guide.d; ruby headerfooter-example2.rb) +.#.==================== + .? compiled source code .==================== -$ ruby headerfooter-enhancer-test2.rb -.<<<:! (cd guide.d; ruby headerfooter-enhancer-test2.rb) +$ erubis -se HeaderFooter headerfooter-example2.rhtml +.<<<:! erubis -se HeaderFooter guide.d/headerfooter-example2.rhtml | ruby -pe 'sub! /^(def|end).*$/, "{{*\\&*}}"' .==================== HeaderFooterEnhancer is experimental and is language-independent. @@ -951,7 +1064,7 @@ Erubis supports the following language currently: <p>Hello {{*<%= $user %>*}}!</p> <table> <tbody> - {{*<% $i = 0 %>*}} + {{*<% $i = 0; %>*}} {{*<% foreach ($list as $item) { %>*}} {{*<% $i++; %>*}} <tr bgcolor={{*"<%= $i % 2 == 0 ? '#FFCCCC' : '#CCCCFF' %>"*}}> @@ -1014,7 +1127,7 @@ $ erubis -l c example.ec -.?? Java | lang-java +.$$ Java | lang-java .? Example.ejava .-------------------- Example.ejava @@ -1030,6 +1143,7 @@ public class Example { } public String view() { + StringBuffer _out = new StringBuffer(); %>*}} <html> <body> @@ -1047,6 +1161,7 @@ public class Example { <body> </html> {{*<% + return _out.toString(); } public static void main(String[] args) { @@ -1075,7 +1190,7 @@ public class Example { .? compiled source code .==================== -$ erubis -l java example.ejava +$ erubis -b -l java example.ejava .<<<:! (cd guide.d; erubis -l java example.ejava) .==================== @@ -1195,7 +1310,63 @@ $ erubis -l js example.ejs -.$ Benchmark | benchmark +.$ Other Topics | topics + + + +.$$ TinyEruby class | topics-tinyeruby + +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 TinyEruby class. + + + +.$$ NoTextEnhancer in PHP | topics-php + +NoTextEnhancer is quite useful not only for eRuby but also for PHP. +It can "drop" HTML text and show up embedded Ruby/PHP code. + +For example, see the following PHP script. + +.? notext-example.php +.-------------------- notext-example.php +<html> + <body> + <h3>List</h3> + <?php if (!$list || count($list) == 0) { ?> + <p>not found.</p> + <?php } else { ?> + <table> + <tbody> + <?php $i = 0; ?> + <?php foreach ($list as $item) { ?> + <tr bgcolor="<?php echo ++$i % 2 == 1 ? '#FFCCCC' : '#CCCCFF'; ?>"> + <td><?php echo $item; ?></td> + </tr> + <?php } ?> + </tbody> + </table> + <?php } ?> + </body> +</html> +.-------------------- + +This is complex because PHP code and HTML document are mixed. +NoTextEnhancer can separate PHP code from HTML document. + +.? example of using NoTextEnhancer with PHP file +.==================== +.#$ erubis -l php -e NoText -p '<\?php \?>' notext-example.php +.#.<<<:! erubis -l php -e NoText -p '<\?php \?>' guide.d/notext-example.php +$ erubis -l php -e NoText -p '<\?php \?>' notext-example.php | uniq +.<<<:! erubis -l php -e NoText -p '<\?php \?>' guide.d/notext-example.php | uniq +.#erubis -l php -e NoText -p '<\?php \?>' guide.d/notext-example.php | ruby -ne 'print unless /^\s*$/' +.==================== + + + +.$$ Benchmark | topics-benchmark A benchmark script is included in Erubis package at erubis-X.X.X/benchark directory. Here is an example result of benchmark. @@ -1210,11 +1381,11 @@ Erubis::StringBufferEruby 186.130000 2.600000 188.730000 (190.374098) Erubis::SimplifiedEruby 130.100000 2.210000 132.310000 (133.426010) Erubis::StdoutEruby 106.010000 2.130000 108.140000 (108.999193) Erubis::StdoutSimplifiedEruby 97.130000 2.180000 99.310000 (100.104433) -Erubis::PrintOutEruby 109.900000 2.090000 111.990000 (112.854442) -Erubis::PrintOutSimplifiedEruby 93.120000 2.140000 95.260000 ( 96.002970) +.#Erubis::PrintOutEruby 109.900000 2.090000 111.990000 (112.854442) +.#Erubis::PrintOutSimplifiedEruby 93.120000 2.140000 95.260000 ( 96.002970) Erubis::TinyEruby 118.740000 2.360000 121.100000 (122.141380) Erubis::TinyStdoutEruby 86.140000 1.840000 87.980000 ( 88.679196) -Erubis::TinyPrintEruby 86.540000 1.970000 88.510000 ( 89.208078) +.#Erubis::TinyPrintEruby 86.540000 1.970000 88.510000 ( 89.208078) .____________________ .? Env: MacOS X 10.4, PowerPC 1.42GHz, Mem1.5GB, Ruby1.8.4 @@ -1227,11 +1398,11 @@ Erubis::StringBufferEruby 47.270000 1.980000 49.250000 ( 73.867537) Erubis::SimplifiedEruby 34.310000 1.600000 35.910000 ( 51.762841) Erubis::StdoutEruby 26.240000 1.490000 27.730000 ( 41.840430) Erubis::StdoutSimplifiedEruby 25.380000 1.340000 26.720000 ( 37.231918) -Erubis::PrintOutEruby 26.850000 1.260000 28.110000 ( 38.378227) -Erubis::PrintOutSimplifiedEruby 24.160000 1.280000 25.440000 ( 40.048199) +.#Erubis::PrintOutEruby 26.850000 1.260000 28.110000 ( 38.378227) +.#Erubis::PrintOutSimplifiedEruby 24.160000 1.280000 25.440000 ( 40.048199) Erubis::TinyEruby 31.690000 1.590000 33.280000 ( 49.862091) Erubis::TinyStdoutEruby 22.550000 1.230000 23.780000 ( 33.316978) -Erubis::TinyPrintEruby 22.340000 1.150000 23.490000 ( 33.577150) +.#Erubis::TinyPrintEruby 22.340000 1.150000 23.490000 ( 33.577150) .____________________ This shows that: |