PageRenderTime 64ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/External.LCA_RESTRICTED/Languages/Ruby/ruby19/lib/ruby/gems/1.9.1/gems/erubis-2.6.6/doc/users-guide.html

http://github.com/IronLanguages/main
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

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html">
  5. <title>Erubis Users' Guide</title>
  6. <meta name="generator" content="kwaser">
  7. <meta http-equiv="Content-Style-Type" content="text/css">
  8. <link rel="stylesheet" href="docstyle.css" type="text/css">
  9. </head>
  10. <body>
  11. <blockquote>
  12. <div class="mainbody">
  13. <div align="left"><h1>Erubis Users' Guide</h1></div>
  14. <div align="left">
  15. release: 2.6.6<br>
  16. last update: $Date$<br>
  17. </div>
  18. <p>release: 2.6.6
  19. </p>
  20. <a name="preface"></a>
  21. <h2 class="section1">Preface</h2>
  22. <p>Erubis is an implementation of eRuby.
  23. It has the following features.
  24. </p>
  25. <ul type="disc">
  26. <li>Very fast, almost three times faster than ERB and about ten percent faster than eruby (implemented in C)
  27. </li>
  28. <li>File caching of converted Ruby script support
  29. </li>
  30. <li>Auto escaping support
  31. </li>
  32. <li>Auto trimming spaces around '&lt;% %&gt;'
  33. </li>
  34. <li>Embedded pattern changeable (default '&lt;% %&gt;')
  35. </li>
  36. <li>Enable to handle Processing Instructions (PI) as embedded pattern (ex. '&lt;?rb ... ?&gt;')
  37. </li>
  38. <li>Multi-language support (Ruby/PHP/C/Java/Scheme/Perl/Javascript)
  39. </li>
  40. <li>Context object available and easy to combine eRuby template with YAML datafile
  41. </li>
  42. <li>Print statement available
  43. </li>
  44. <li>Easy to expand and customize in subclass
  45. </li>
  46. <li><a href="#rails">Ruby on Rails support</a>
  47. </li>
  48. <li>mod_ruby support|#topcs-modruby
  49. </li>
  50. </ul>
  51. <p>Erubis is implemented in pure Ruby. It requires Ruby 1.8 or higher.
  52. Erubis now supports Ruby 1.9.
  53. </p>
  54. <a name="toc"></a>
  55. <h3 class="section2">Table of Contents</h3>
  56. <ul>
  57. <li><a href="#preface">Preface</a>
  58. <ul>
  59. <li><a href="#toc">Table of Contents</a>
  60. </li>
  61. </ul>
  62. </li>
  63. <li><a href="#install">Installation</a>
  64. </li>
  65. <li><a href="#tutorial">Tutorial</a>
  66. <ul>
  67. <li><a href="#tut-basic">Basic Example</a>
  68. </li>
  69. <li><a href="#tut-trim">Trimming Spaces</a>
  70. </li>
  71. <li><a href="#tut-escape">Escape</a>
  72. </li>
  73. <li><a href="#tut-pattern">Embedded Pattern</a>
  74. </li>
  75. <li><a href="#tut-context">Context Object</a>
  76. </li>
  77. <li><a href="#tut-datafile">Context Data File</a>
  78. </li>
  79. <li><a href="#tut-datastr">Context Data String</a>
  80. </li>
  81. <li><a href="#tut-preamble">Preamble and Postamble</a>
  82. </li>
  83. <li><a href="#tut-pi">Processing Instruction (PI) Converter</a>
  84. </li>
  85. <li><a href="#tut-notext">Retrieve Ruby Code</a>
  86. </li>
  87. </ul>
  88. </li>
  89. <li><a href="#enhancer">Enhancer</a>
  90. <ul>
  91. <li><a href="#escape-enhancer">EscapeEnhancer</a>
  92. </li>
  93. <li><a href="#stdout-enhancer">StdoutEnhancer</a>
  94. </li>
  95. <li><a href="#printout-enhancer">PrintOutEnhancer</a>
  96. </li>
  97. <li><a href="#printenabled-enhancer">PrintEnabledEnhancer</a>
  98. </li>
  99. <li><a href="#array-enhancer">ArrayEnhancer</a>
  100. </li>
  101. <li><a href="#arraybuffer-enhancer">ArrayBufferEnhancer</a>
  102. </li>
  103. <li><a href="#stringbuffer-enhancer">StringBufferEnhancer</a>
  104. </li>
  105. <li><a href="#erbout-enhancer">ErboutEnhancer</a>
  106. </li>
  107. <li><a href="#notext-enhancer">NoTextEnhancer</a>
  108. </li>
  109. <li><a href="#nocode-enhancer">NoCodeEnhancer</a>
  110. </li>
  111. <li><a href="#simplify-enhancer">SimplifyEnhancer</a>
  112. </li>
  113. <li><a href="#bipattern-enhancer">BiPatternEnhancer</a>
  114. </li>
  115. <li><a href="#percentline-enhancer">PercentLineEnhancer</a>
  116. </li>
  117. <li><a href="#headerfooter-enhancer">HeaderFooterEnhancer</a>
  118. </li>
  119. <li><a href="#interpolation-enhancer">InterpolationEnhancer</a>
  120. </li>
  121. <li><a href="#deleteindent-enhancer">DeleteIndentEnhancer</a>
  122. </li>
  123. </ul>
  124. </li>
  125. <li><a href="#lang">Multi-Language Support</a>
  126. <ul>
  127. <li><a href="#lang-php">PHP</a>
  128. </li>
  129. <li><a href="#lang-c">C</a>
  130. </li>
  131. <li><a href="#lang-java">Java</a>
  132. </li>
  133. <li><a href="#lang-scheme">Scheme</a>
  134. </li>
  135. <li><a href="#lang-perl">Perl</a>
  136. </li>
  137. <li><a href="#lang-javascript">JavaScript</a>
  138. </li>
  139. </ul>
  140. </li>
  141. <li><a href="#rails">Ruby on Rails Support</a>
  142. <ul>
  143. <li><a href="#rails-settings">Settings</a>
  144. </li>
  145. <li><a href="#rails-preprocessing">Preprosessing</a>
  146. </li>
  147. <li><a href="#rails-formhelpers">Form Helpers for Preprocessing</a>
  148. </li>
  149. <li><a href="#rails-others">Others</a>
  150. </li>
  151. </ul>
  152. </li>
  153. <li><a href="#topics">Other Topics</a>
  154. <ul>
  155. <li><a href="#'&lt;%= =%&gt;' and '&lt;%= -%&gt;'">'&lt;%= =%&gt;' and '&lt;%= -%&gt;'</a>
  156. </li>
  157. <li><a href="#'&lt;%% %&gt;' and '&lt;%%= %&gt;'">'&lt;%% %&gt;' and '&lt;%%= %&gt;'</a>
  158. </li>
  159. <li><a href="#topics-context-vs-binding">evaluate(context) v.s. result(binding)</a>
  160. </li>
  161. <li><a href="#topics-fasteruby">Class Erubis::FastEruby</a>
  162. </li>
  163. <li><a href="#topics-syntax">Syntax Checking</a>
  164. </li>
  165. <li><a href="#topics-caching">File Caching</a>
  166. </li>
  167. <li><a href="#topics-tinyeruby">Erubis::TinyEruby class</a>
  168. </li>
  169. <li><a href="#topics-php">NoTextEnhancer and NoCodeEnhancer in PHP</a>
  170. </li>
  171. <li><a href="#topcs-modruby">Helper Class for mod_ruby</a>
  172. </li>
  173. <li><a href="#topics-defmethod">Define method</a>
  174. </li>
  175. <li><a href="#topics-benchmark">Benchmark</a>
  176. </li>
  177. </ul>
  178. </li>
  179. <li><a href="#command">Command Reference</a>
  180. <ul>
  181. <li><a href="#command-usage">Usage</a>
  182. </li>
  183. <li><a href="#command-options">Options</a>
  184. </li>
  185. <li><a href="#command-props">Properties</a>
  186. </li>
  187. </ul>
  188. </li>
  189. </ul>
  190. <br>
  191. <br>
  192. <a name="install"></a>
  193. <h2 class="section1">Installation</h2>
  194. <ul type="disc">
  195. <li>If you have installed RubyGems, just type <code>gem install --remote erubis</code>.
  196. <pre class="terminal">$ sudo gem install --remote erubis
  197. </pre>
  198. </li>
  199. </ul>
  200. <ul type="disc">
  201. <li>Else install <a href="http://rubyforge.org/projects/erubis/">abstract</a> at first,
  202. and download erubis_X.X.X.tar.bz2 and install it by setup.rb.
  203. <pre class="terminal">$ tar xjf abstract_X.X.X.tar.bz2
  204. $ cd abstract_X.X.X/
  205. $ sudo ruby setup.rb
  206. $ cd ..
  207. $ tar xjf erubis_X.X.X.tar.bz2
  208. $ cd erubis_X.X.X/
  209. $ sudo ruby setup.rb
  210. </pre>
  211. </li>
  212. </ul>
  213. <ul type="disc">
  214. <li>(Optional) 'contrib/inline-require' enables you to merge 'lib/**/*.rb' into 'bin/erubis'.
  215. <pre class="terminal">$ tar xjf erubis_X.X.X.tar.bz2
  216. $ cd erubis_X.X.X/
  217. $ unset RUBYLIB
  218. $ contrib/inline-require -I lib bin/erubis &gt; contrib/erubis
  219. </pre>
  220. </li>
  221. </ul>
  222. <br>
  223. <a name="tutorial"></a>
  224. <h2 class="section1">Tutorial</h2>
  225. <a name="tut-basic"></a>
  226. <h3 class="section2">Basic Example</h3>
  227. <p>Here is a basic example of Erubis.
  228. </p>
  229. <a name="example1.eruby"></a>
  230. <div class="program_caption">
  231. example1.eruby</div>
  232. <pre class="program">&lt;ul&gt;
  233. <strong>&lt;% for item in list %&gt;</strong>
  234. &lt;li&gt;<strong>&lt;%= item %&gt;</strong>&lt;/li&gt;
  235. <strong>&lt;% end %&gt;</strong>
  236. <strong>&lt;%# here is ignored because starting with '#' %&gt;</strong>
  237. &lt;/ul&gt;
  238. </pre>
  239. <a name="example1.rb"></a>
  240. <div class="program_caption">
  241. example1.rb</div>
  242. <pre class="program">require 'erubis'
  243. input = File.read('example1.eruby')
  244. eruby = <strong>Erubis::Eruby.new(input)</strong> # create Eruby object
  245. puts "---------- script source ---"
  246. puts <strong>eruby.src</strong> # print script source
  247. puts "---------- result ----------"
  248. list = ['aaa', 'bbb', 'ccc']
  249. puts <strong>eruby.result(binding())</strong> # get result
  250. ## or puts eruby.result(<strong>:list=&gt;list</strong>) # or pass Hash instead of Binding
  251. ## # or
  252. ## eruby = Erubis::Eruby.new
  253. ## input = File.read('example1.eruby')
  254. ## src = eruby.convert(input)
  255. ## eval src
  256. </pre>
  257. <a name="example1.result"></a>
  258. <div class="terminal_caption">
  259. output</div>
  260. <pre class="terminal">$ ruby example1.rb
  261. ---------- script source ---
  262. _buf = ''; _buf &lt;&lt; '&lt;ul&gt;
  263. '; for item in list
  264. _buf &lt;&lt; ' &lt;li&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/li&gt;
  265. '; end
  266. _buf &lt;&lt; '&lt;/ul&gt;
  267. ';
  268. _buf.to_s
  269. ---------- result ----------
  270. &lt;ul&gt;
  271. &lt;li&gt;aaa&lt;/li&gt;
  272. &lt;li&gt;bbb&lt;/li&gt;
  273. &lt;li&gt;ccc&lt;/li&gt;
  274. &lt;/ul&gt;
  275. </pre>
  276. <p>Erubis has command 'erubis'. Command-line option '-x' shows the compiled source code of eRuby script.
  277. </p>
  278. <a name="example1_x.result"></a>
  279. <div class="terminal_caption">
  280. example of command-line option '-x'</div>
  281. <pre class="terminal">$ erubis <strong>-x</strong> example1.eruby
  282. _buf = ''; _buf &lt;&lt; '&lt;ul&gt;
  283. '; for item in list
  284. _buf &lt;&lt; ' &lt;li&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/li&gt;
  285. '; end
  286. _buf &lt;&lt; '&lt;/ul&gt;
  287. ';
  288. _buf.to_s
  289. </pre>
  290. <br>
  291. <a name="tut-trim"></a>
  292. <h3 class="section2">Trimming Spaces</h3>
  293. <p>Erubis deletes spaces around '&lt;% %&gt;' automatically, while it leaves spaces around '&lt;%= %&gt;'.
  294. </p>
  295. <a name="example2.eruby.comment_filter"></a>
  296. <div class="program_caption">
  297. example2.eruby</div>
  298. <pre class="program">&lt;ul&gt;
  299. &lt;% for item in list %&gt; # trimmed
  300. &lt;li&gt;
  301. &lt;%= item %&gt; # not trimmed
  302. &lt;/li&gt;
  303. &lt;% end %&gt; # trimmed
  304. &lt;/ul&gt;
  305. </pre>
  306. <a name="example2_x.result"></a>
  307. <div class="terminal_caption">
  308. compiled source code</div>
  309. <pre class="terminal">$ erubis -x example2.eruby
  310. _buf = ''; _buf &lt;&lt; '&lt;ul&gt;
  311. '; for item in list
  312. _buf &lt;&lt; ' &lt;li&gt;
  313. '; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '
  314. '; _buf &lt;&lt; ' &lt;/li&gt;
  315. '; end
  316. _buf &lt;&lt; '&lt;/ul&gt;
  317. ';
  318. _buf.to_s
  319. </pre>
  320. <p>If you want leave spaces around '&lt;% %&gt;', add command-line property '--trim=false'.
  321. </p>
  322. <a name="example2_trim.result"></a>
  323. <div class="terminal_caption">
  324. compiled source code with command-line property '--trim=false'</div>
  325. <pre class="terminal">$ erubis -x <strong>--trim=false</strong> example2.eruby
  326. _buf = ''; _buf &lt;&lt; '&lt;ul&gt;
  327. '; _buf &lt;&lt; ' '; for item in list ; _buf &lt;&lt; '
  328. '; _buf &lt;&lt; ' &lt;li&gt;
  329. '; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '
  330. '; _buf &lt;&lt; ' &lt;/li&gt;
  331. '; _buf &lt;&lt; ' '; end ; _buf &lt;&lt; '
  332. '; _buf &lt;&lt; '&lt;/ul&gt;
  333. ';
  334. _buf.to_s
  335. </pre>
  336. <p>Or add option <code>:trim=&gt;false</code> to Erubis::Eruby.new().
  337. </p>
  338. <a name="example2.rb"></a>
  339. <div class="program_caption">
  340. example2.rb</div>
  341. <pre class="program">require 'erubis'
  342. input = File.read('example2.eruby')
  343. eruby = Erubis::Eruby.new(input<strong>, :trim=&gt;false</strong>)
  344. puts "----- script source ---"
  345. puts eruby.src # print script source
  346. puts "----- result ----------"
  347. list = ['aaa', 'bbb', 'ccc']
  348. puts eruby.result(binding()) # get result
  349. </pre>
  350. <a name="example2.result"></a>
  351. <div class="terminal_caption">
  352. output</div>
  353. <pre class="terminal">$ ruby example2.rb
  354. ----- script source ---
  355. _buf = ''; _buf &lt;&lt; '&lt;ul&gt;
  356. '; <strong>_buf &lt;&lt; ' ';</strong> for item in list ; _buf &lt;&lt; '
  357. '; _buf &lt;&lt; ' &lt;li&gt;
  358. '; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '
  359. '; _buf &lt;&lt; ' &lt;/li&gt;
  360. '; <strong>_buf &lt;&lt; ' ';</strong> end ; _buf &lt;&lt; '
  361. '; _buf &lt;&lt; '&lt;/ul&gt;
  362. ';
  363. _buf.to_s
  364. ----- result ----------
  365. &lt;ul&gt;
  366. &lt;li&gt;
  367. aaa
  368. &lt;/li&gt;
  369. &lt;li&gt;
  370. bbb
  371. &lt;/li&gt;
  372. &lt;li&gt;
  373. ccc
  374. &lt;/li&gt;
  375. &lt;/ul&gt;
  376. </pre>
  377. <br>
  378. <a name="tut-escape"></a>
  379. <h3 class="section2">Escape</h3>
  380. <p>Erubis has ability to escape (sanitize) expression.
  381. Erubis::Eruby class act as the following:
  382. </p>
  383. <ul type="disc">
  384. <li><code>&lt;%= <em>expr</em> %&gt;</code> - not escaped.
  385. </li>
  386. <li><code>&lt;%== <em>expr</em> %&gt;</code> - escaped.
  387. </li>
  388. <li><code>&lt;%=== <em>expr</em> %&gt;</code> - out to $stderr.
  389. </li>
  390. <li><code>&lt;%==== <em>expr</em> %&gt;</code> - ignored.
  391. </li>
  392. </ul>
  393. <p>Erubis::EscapedEruby<sup>(<a href="#fnref:1" name="fnlink:1">*1</a>)</sup> class handle '&lt;%= %&gt;' as escaped and '&lt;%== %&gt;' as not escaped.
  394. It means that using Erubis::EscapedEruby you can escape expression by default.
  395. Also Erubis::XmlEruby class (which is equivalent to Erubis::EscapedEruby) is provided for compatibility with Erubis 1.1.
  396. </p>
  397. <a name="example3.eruby"></a>
  398. <div class="program_caption">
  399. example3.eruby</div>
  400. <pre class="program">&lt;% for item in list %&gt;
  401. &lt;p&gt;<strong>&lt;%=</strong> item <strong>%&gt;</strong>&lt;/p&gt;
  402. &lt;p&gt;<strong>&lt;%==</strong> item <strong>%&gt;</strong>&lt;/p&gt;
  403. &lt;p&gt;<strong>&lt;%===</strong> item <strong>%&gt;</strong>&lt;/p&gt;
  404. &lt;% end %&gt;
  405. </pre>
  406. <a name="example3.rb"></a>
  407. <div class="program_caption">
  408. example3.rb</div>
  409. <pre class="program">require 'erubis'
  410. input = File.read('example3.eruby')
  411. eruby = Erubis::<strong>EscapedEruby</strong>.new(input) # or Erubis::XmlEruby
  412. puts "----- script source ---"
  413. puts eruby.src # print script source
  414. puts "----- result ----------"
  415. <strong>list = ['&lt;aaa&gt;', 'b&amp;b', '"ccc"']</strong>
  416. puts eruby.result(binding()) # get result
  417. </pre>
  418. <a name="example3.result.split_filter"></a>
  419. <div class="terminal_caption">
  420. output</div>
  421. <pre class="terminal">$ ruby example3.rb 2&gt; stderr.log
  422. ----- script source ---
  423. _buf = ''; for item in list
  424. _buf &lt;&lt; ' &lt;p&gt;'; <strong>_buf &lt;&lt; Erubis::XmlHelper.escape_xml( item )</strong>; _buf &lt;&lt; '&lt;/p&gt;
  425. &lt;p&gt;'; <strong>_buf &lt;&lt; ( item ).to_s</strong>; _buf &lt;&lt; '&lt;/p&gt;
  426. &lt;p&gt;'; <strong>$stderr.puts("*** debug: item=#{(item).inspect}")</strong>; _buf &lt;&lt; '&lt;/p&gt;
  427. '; end
  428. _buf.to_s
  429. ----- result ----------
  430. &lt;p&gt;<strong>&amp;lt;aaa&amp;gt;</strong>&lt;/p&gt;
  431. &lt;p&gt;&lt;aaa&gt;&lt;/p&gt;
  432. &lt;p&gt;&lt;/p&gt;
  433. &lt;p&gt;<strong>b&amp;amp;b</strong>&lt;/p&gt;
  434. &lt;p&gt;b&amp;b&lt;/p&gt;
  435. &lt;p&gt;&lt;/p&gt;
  436. &lt;p&gt;<strong>&amp;quot;ccc&amp;quot;</strong>&lt;/p&gt;
  437. &lt;p&gt;"ccc"&lt;/p&gt;
  438. &lt;p&gt;&lt;/p&gt;
  439. $ cat stderr.log
  440. *** debug: item="&lt;aaa&gt;"
  441. *** debug: item="b&amp;b"
  442. *** debug: item="\"ccc\""
  443. </pre>
  444. <p>The command-line option '-e' will do the same action as Erubis::EscapedEruby.
  445. This option is available for any language.
  446. </p>
  447. <a name="example3_e.result"></a>
  448. <pre class="terminal">$ erubis -l ruby <strong>-e</strong> example3.eruby
  449. _buf = ''; for item in list
  450. _buf &lt;&lt; ' &lt;p&gt;'; _buf &lt;&lt; Erubis::XmlHelper.escape_xml( item ); _buf &lt;&lt; '&lt;/p&gt;
  451. &lt;p&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/p&gt;
  452. &lt;p&gt;'; $stderr.puts("*** debug: item=#{(item).inspect}"); _buf &lt;&lt; '&lt;/p&gt;
  453. '; end
  454. _buf.to_s
  455. </pre>
  456. <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.
  457. </p>
  458. <div class="program_caption">
  459. example to override Erubis::Eruby#escaped_expr()</div>
  460. <pre class="program">class CGIEruby &lt; Erubis::Eruby
  461. def <strong>escaped_expr(code)</strong>
  462. return "CGI.escapeHTML((#{code.strip}).to_s)"
  463. #return "h(#{code.strip})"
  464. end
  465. end
  466. class LatexEruby &lt; Erubi::Eruby
  467. def <strong>escaped_expr(code)</strong>
  468. return "(#{code}).gsub(/[%\\]/,'\\\\\&amp;')"
  469. end
  470. end
  471. </pre>
  472. <div class="footnote">
  473. <dl compact>
  474. <dt>(<a name="fnref:1" href="#fnlink:1">*1</a>)</dt>
  475. <dd>Erubis::EscapedEruby class includes Erubis::EscapeEnhancer which swtches the action of '&lt;%= %&gt;' and '&lt;%== %&gt;'.</dd>
  476. </dl>
  477. </div>
  478. <br>
  479. <a name="tut-pattern"></a>
  480. <h3 class="section2">Embedded Pattern</h3>
  481. <p>You can change embedded pattern '<code>&lt;% %&gt;</code>' to another by command-line option '-p' or option '<code>:pattern=&gt;...</code>' of Erubis::Eruby.new().
  482. </p>
  483. <a name="example4.eruby"></a>
  484. <div class="program_caption">
  485. example4.eruby</div>
  486. <pre class="program"><strong>&lt;!--%</strong> for item in list <strong>%--&gt;</strong>
  487. &lt;p&gt;<strong>&lt;!--%=</strong> item <strong>%--&gt;</strong>&lt;/p&gt;
  488. <strong>&lt;!--%</strong> end <strong>%--&gt;</strong>
  489. </pre>
  490. <a name="example4_x.result"></a>
  491. <div class="terminal_caption">
  492. compiled source code with command-line option '-p'</div>
  493. <pre class="terminal">$ erubis -x <strong>-p '&lt;!--% %--&gt;'</strong> example4.eruby
  494. _buf = ''; for item in list
  495. _buf &lt;&lt; ' &lt;p&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/p&gt;
  496. '; end
  497. _buf.to_s
  498. </pre>
  499. <a name="example4.rb"></a>
  500. <div class="program_caption">
  501. example4.rb</div>
  502. <pre class="program">require 'erubis'
  503. input = File.read('example4.eruby')
  504. eruby = Erubis::Eruby.new(input<strong>, :pattern=&gt;'&lt;!--% %--&gt;'</strong>)
  505. # or '&lt;(?:!--)?% %(?:--)?&gt;'
  506. puts "---------- script source ---"
  507. puts eruby.src # print script source
  508. puts "---------- result ----------"
  509. list = ['aaa', 'bbb', 'ccc']
  510. puts eruby.result(binding()) # get result
  511. </pre>
  512. <a name="example4.result"></a>
  513. <div class="terminal_caption">
  514. output</div>
  515. <pre class="terminal">$ ruby example4.rb
  516. ---------- script source ---
  517. _buf = ''; for item in list
  518. _buf &lt;&lt; ' &lt;p&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/p&gt;
  519. '; end
  520. _buf.to_s
  521. ---------- result ----------
  522. &lt;p&gt;aaa&lt;/p&gt;
  523. &lt;p&gt;bbb&lt;/p&gt;
  524. &lt;p&gt;ccc&lt;/p&gt;
  525. </pre>
  526. <p>It is able to specify regular expression with :pattern option.
  527. Notice that you must use '<code>(?: )</code>' instead of '<code>( )</code>' for grouping.
  528. For example, '<code>&lt;(!--)?% %(--)?&gt;</code>' will not work while '<code>&lt;(?:!--)?% %(?:--)?&gt;</code>' will work.
  529. </p>
  530. <br>
  531. <a name="tut-context"></a>
  532. <h3 class="section2">Context Object</h3>
  533. <p>Context object is a set of data which are used in eRuby script.
  534. Using context object makes clear which data to be used.
  535. In Erubis, Hash object and Erubis::Context object are available as context object.
  536. </p>
  537. <p>Context data can be accessible via instance variables in eRuby script.
  538. </p>
  539. <a name="example5.eruby"></a>
  540. <div class="program_caption">
  541. example5.eruby</div>
  542. <pre class="program">&lt;span&gt;&lt;%= <strong>@val</strong> %&gt;&lt;/span&gt;
  543. &lt;ul&gt;
  544. &lt;% for item in <strong>@list</strong> %&gt;
  545. &lt;li&gt;&lt;%= item %&gt;&lt;/li&gt;
  546. &lt;% end %&gt;
  547. &lt;/ul&gt;
  548. </pre>
  549. <a name="example5.rb"></a>
  550. <div class="program_caption">
  551. example5.rb</div>
  552. <pre class="program">require 'erubis'
  553. input = File.read('example5.eruby')
  554. eruby = Erubis::Eruby.new(input) # create Eruby object
  555. ## create context object
  556. ## (key means var name, which may be string or symbol.)
  557. <strong>context = {
  558. :val =&gt; 'Erubis Example',
  559. 'list' =&gt; ['aaa', 'bbb', 'ccc'],
  560. }</strong>
  561. ## or
  562. # context = Erubis::Context.new()
  563. # context['val'] = 'Erubis Example'
  564. # context[:list] = ['aaa', 'bbb', 'ccc'],
  565. puts <strong>eruby.evaluate(context)</strong> # get result
  566. </pre>
  567. <a name="example5.result"></a>
  568. <div class="terminal_caption">
  569. output</div>
  570. <pre class="terminal">$ ruby example5.rb
  571. &lt;span&gt;Erubis Example&lt;/span&gt;
  572. &lt;ul&gt;
  573. &lt;li&gt;aaa&lt;/li&gt;
  574. &lt;li&gt;bbb&lt;/li&gt;
  575. &lt;li&gt;ccc&lt;/li&gt;
  576. &lt;/ul&gt;
  577. </pre>
  578. <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'.
  579. 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.
  580. </p>
  581. <p>Here is the definition of Erubis#result() and Erubis#evaluate().
  582. </p>
  583. <div class="program_caption">
  584. definition of result(binding) and evaluate(context)</div>
  585. <pre class="program">def result(_binding=TOPLEVEL_BINDING)
  586. if _binding.is_a?(Hash)
  587. # load hash data as local variable
  588. _h = _binding
  589. _binding = binding()
  590. eval _h.collect{|k,v| "#{k} = _h[#{k.inspect}];"}.join, _binding
  591. end
  592. return <strong>eval(@src, _binding)</strong>
  593. end
  594. def evaluate(_context=Erubis::Context.new)
  595. if _context.is_a?(Hash)
  596. # convert hash object to Context object
  597. _hash = _context
  598. _context = Erubis::Context.new
  599. _hash.each {|k, v| _context[k] = v }
  600. end
  601. return <strong>_context.instance_eval(@src)</strong>
  602. end
  603. </pre>
  604. <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.
  605. </p>
  606. <a name="example6.rb"></a>
  607. <div class="program_caption">
  608. example6.rb</div>
  609. <pre class="program">class MyData
  610. attr_accessor :val, :list
  611. end
  612. ## any object can be a context object
  613. <strong>mydata = MyData.new</strong>
  614. <strong>mydata.val = 'Erubis Example'</strong>
  615. <strong>mydata.list = ['aaa', 'bbb', 'ccc']</strong>
  616. require 'erubis'
  617. eruby = Erubis::Eruby.new(File.read('example5.eruby'))
  618. puts eruby.evaluate(<strong>mydata</strong>)
  619. </pre>
  620. <a name="example6.result"></a>
  621. <div class="terminal_caption">
  622. output</div>
  623. <pre class="terminal">$ ruby example6.rb
  624. &lt;span&gt;Erubis Example&lt;/span&gt;
  625. &lt;ul&gt;
  626. &lt;li&gt;aaa&lt;/li&gt;
  627. &lt;li&gt;bbb&lt;/li&gt;
  628. &lt;li&gt;ccc&lt;/li&gt;
  629. &lt;/ul&gt;
  630. </pre>
  631. <p>It is recommended to use 'Erubis::Eruby#evaluate(context)' rather than 'Erubis::Eruby#result(binding())' because the latter has some problems.
  632. See <a href="#topics-context-vs-binding">evaluate(context) v.s. result(binding)</a> section for details.
  633. </p>
  634. <br>
  635. <a name="tut-datafile"></a>
  636. <h3 class="section2">Context Data File</h3>
  637. <p>Command-line option '-f' specifies context data file.
  638. Erubis load context data file and use it as context data.
  639. Context data file can be YAML file ('*.yaml' or '*.yml') or Ruby script ('*.rb').
  640. </p>
  641. <a name="example7.eruby"></a>
  642. <div class="program_caption">
  643. example7.eruby</div>
  644. <pre class="program">&lt;h1&gt;&lt;%= <strong>@title</strong> %&gt;&lt;/h1&gt;
  645. &lt;ul&gt;
  646. &lt;% for user in <strong>@users</strong> %&gt;
  647. &lt;li&gt;
  648. &lt;a href="mailto:&lt;%= user['mail']%&gt;"&gt;&lt;%= user['name'] %&gt;&lt;/a&gt;
  649. &lt;/li&gt;
  650. &lt;% end %&gt;
  651. &lt;/ul&gt;
  652. </pre>
  653. <a name="context.yaml"></a>
  654. <div class="program_caption">
  655. context.yaml</div>
  656. <pre class="program"><strong>title:</strong> Users List
  657. <strong>users:</strong>
  658. - name: foo
  659. mail: foo@mail.com
  660. - name: bar
  661. mail: bar@mail.net
  662. - name: baz
  663. mail: baz@mail.org
  664. </pre>
  665. <a name="context.rb"></a>
  666. <div class="program_caption">
  667. context.rb</div>
  668. <pre class="program">@title = 'Users List'
  669. @users = [
  670. { 'name'=&gt;'foo', 'mail'=&gt;'foo@mail.com' },
  671. { 'name'=&gt;'bar', 'mail'=&gt;'bar@mail.net' },
  672. { 'name'=&gt;'baz', 'mail'=&gt;'baz@mail.org' },
  673. ]
  674. </pre>
  675. <a name="example7.result.split_filter"></a>
  676. <div class="terminal_caption">
  677. example of command-line option '-f'</div>
  678. <pre class="terminal">$ erubis <strong>-f context.yaml</strong> example7.eruby
  679. &lt;h1&gt;Users List&lt;/h1&gt;
  680. &lt;ul&gt;
  681. &lt;li&gt;
  682. &lt;a href="mailto:foo@mail.com"&gt;foo&lt;/a&gt;
  683. &lt;/li&gt;
  684. &lt;li&gt;
  685. &lt;a href="mailto:bar@mail.net"&gt;bar&lt;/a&gt;
  686. &lt;/li&gt;
  687. &lt;li&gt;
  688. &lt;a href="mailto:baz@mail.org"&gt;baz&lt;/a&gt;
  689. &lt;/li&gt;
  690. &lt;/ul&gt;
  691. $ erubis <strong>-f context.rb</strong> example7.eruby
  692. &lt;h1&gt;Users List&lt;/h1&gt;
  693. &lt;ul&gt;
  694. &lt;li&gt;
  695. &lt;a href="mailto:foo@mail.com"&gt;foo&lt;/a&gt;
  696. &lt;/li&gt;
  697. &lt;li&gt;
  698. &lt;a href="mailto:bar@mail.net"&gt;bar&lt;/a&gt;
  699. &lt;/li&gt;
  700. &lt;li&gt;
  701. &lt;a href="mailto:baz@mail.org"&gt;baz&lt;/a&gt;
  702. &lt;/li&gt;
  703. &lt;/ul&gt;
  704. </pre>
  705. <p>Command-line option '-S' converts keys of mapping in YAML data file from string into symbol.
  706. Command-line option '-B' invokes 'Erubis::Eruby#result(binding())' instead of 'Erubis::Eruby#evaluate(context)'.
  707. </p>
  708. <br>
  709. <a name="tut-datastr"></a>
  710. <h3 class="section2">Context Data String</h3>
  711. <p>Command-line option '-c <em>str</em>' enables you to specify context data in command-line.
  712. <em>str</em> can be YAML flow-style or Ruby code.
  713. </p>
  714. <a name="example8.eruby"></a>
  715. <div class="program_caption">
  716. example8.eruby</div>
  717. <pre class="program">&lt;h1&gt;&lt;%= @title %&gt;&lt;/h1&gt;
  718. &lt;ul&gt;
  719. &lt;% for item in @list %&gt;
  720. &lt;li&gt;&lt;%= item %&gt;&lt;/li&gt;
  721. &lt;% end %&gt;
  722. &lt;/ul&gt;
  723. </pre>
  724. <a name="example8_yaml.result"></a>
  725. <div class="terminal_caption">
  726. example of YAML flow style</div>
  727. <pre class="terminal">$ erubis <strong>-c '{title: Example, list: [AAA, BBB, CCC]}'</strong> example8.eruby
  728. &lt;h1&gt;Example&lt;/h1&gt;
  729. &lt;ul&gt;
  730. &lt;li&gt;AAA&lt;/li&gt;
  731. &lt;li&gt;BBB&lt;/li&gt;
  732. &lt;li&gt;CCC&lt;/li&gt;
  733. &lt;/ul&gt;
  734. </pre>
  735. <a name="example8_ruby.result"></a>
  736. <div class="terminal_caption">
  737. example of Ruby code</div>
  738. <pre class="terminal">$ erubis <strong>-c '@title="Example"; @list=%w[AAA BBB CCC]'</strong> example8.eruby
  739. &lt;h1&gt;Example&lt;/h1&gt;
  740. &lt;ul&gt;
  741. &lt;li&gt;AAA&lt;/li&gt;
  742. &lt;li&gt;BBB&lt;/li&gt;
  743. &lt;li&gt;CCC&lt;/li&gt;
  744. &lt;/ul&gt;
  745. </pre>
  746. <br>
  747. <a name="tut-preamble"></a>
  748. <h3 class="section2">Preamble and Postamble</h3>
  749. <p>The first line ('_buf = '';') in the compiled source code is called preamble
  750. and the last line ('_buf.to_s') is called postamble.
  751. </p>
  752. <p>Command-line option '-b' skips the output of preamble and postamble.
  753. </p>
  754. <a name="example9.eruby"></a>
  755. <div class="program_caption">
  756. example9.eruby</div>
  757. <pre class="program">&lt;% for item in @list %&gt;
  758. &lt;b&gt;&lt;%= item %&gt;&lt;/b&gt;
  759. &lt;% end %&gt;
  760. </pre>
  761. <a name="example9.result.split_filter"></a>
  762. <div class="terminal_caption">
  763. compiled source code with and without command-line option '-b'</div>
  764. <pre class="terminal">$ erubis -x example9.eruby
  765. <strong>_buf = '';</strong> for item in @list
  766. _buf &lt;&lt; ' &lt;b&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/b&gt;
  767. '; end
  768. <strong>_buf.to_s</strong>
  769. $ erubis -x <strong>-b</strong> example9.eruby
  770. for item in @list
  771. _buf &lt;&lt; ' &lt;b&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/b&gt;
  772. '; end
  773. </pre>
  774. <p>Erubis::Eruby.new option '<code>:preamble=&gt;false</code>' and '<code>:postamble=&gt;false</code>' also suppress output of preamble or postamle.
  775. </p>
  776. <a name="example9.rb"></a>
  777. <div class="program_caption">
  778. example9.rb</div>
  779. <pre class="program">require 'erubis'
  780. input = File.read('example9.eruby')
  781. eruby1 = Erubis::Eruby.new(input)
  782. eruby2 = Erubis::Eruby.new(input, <strong>:preamble=&gt;false, :postamble=&gt;false</strong>)
  783. puts eruby1.src # print preamble and postamble
  784. puts "--------------"
  785. puts eruby2.src # don't print preamble and postamble
  786. </pre>
  787. <a name="example9.result"></a>
  788. <div class="terminal_caption">
  789. output</div>
  790. <pre class="terminal">$ ruby example9.rb
  791. _buf = ''; for item in @list
  792. _buf &lt;&lt; ' &lt;b&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/b&gt;
  793. '; end
  794. _buf.to_s
  795. --------------
  796. for item in @list
  797. _buf &lt;&lt; ' &lt;b&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/b&gt;
  798. '; end
  799. </pre>
  800. <br>
  801. <a name="tut-pi"></a>
  802. <h3 class="section2">Processing Instruction (PI) Converter</h3>
  803. <p>Erubis can parse Processing Instructions (PI) as embedded pattern.
  804. </p>
  805. <ul type="disc">
  806. <li>'<code>&lt;?rb <em>...</em> ?&gt;</code>' represents Ruby statement.
  807. </li>
  808. <li>'<code>@{<em>...</em>}@</code>' represents escaped expression value.
  809. </li>
  810. <li>'<code>@!{<em>...</em>}@</code>' represents normal expression value.
  811. </li>
  812. <li>'<code>@!!{<em>...</em>}@</code>' prints expression value to standard output.
  813. </li>
  814. <li>(experimental) '<code>&lt;%= <em>...</em> %&gt;</code>' is also available to print expression value.
  815. </li>
  816. </ul>
  817. <p>This is more useful than basic embedded pattern ('<code>&lt;% ... &gt;</code>') because PI doesn't break XML or HTML at all.
  818. For example the following XHTML file is well-formed and HTML validator got no errors on this example.
  819. </p>
  820. <a name="example10.xhtml"></a>
  821. <div class="program_caption">
  822. example10.xhtml</div>
  823. <pre class="program">&lt;?xml version="1.0" ?&gt;
  824. <strong>&lt;?rb
  825. lang = 'en'
  826. list = ['&lt;aaa&gt;', 'b&amp;b', '"ccc"']
  827. ?&gt;</strong>
  828. &lt;html lang="<strong>@!{lang}@</strong>"&gt;
  829. &lt;body&gt;
  830. &lt;ul&gt;
  831. <strong>&lt;?rb for item in list ?&gt;</strong>
  832. &lt;li&gt;<strong>@{item}@</strong>&lt;/li&gt;
  833. <strong>&lt;?rb end ?&gt;</strong>
  834. &lt;/ul&gt;
  835. &lt;/body&gt;
  836. &lt;/html&gt;
  837. </pre>
  838. <p>If the command-line property '--pi=<em>name</em>' is specified, erubis command parses input with PI converter.
  839. If <em>name</em> is omitted then the following name is used according to '-l <em>lang</em>'.
  840. </p>
  841. <div align="center">
  842. <table class="table1" border="1" cellspacing="0">
  843. <tr class="tr1">
  844. <th class="th1">'-l' option</th>
  845. <th class="th1">PI name</th>
  846. </tr>
  847. <tr class="tr1">
  848. <td class="td1">-l ruby</td>
  849. <td class="td1">&lt;?rb ... ?&gt;</td>
  850. </tr>
  851. <tr class="tr1">
  852. <td class="td1">-l php</td>
  853. <td class="td1">&lt;?php ... ?&gt;</td>
  854. </tr>
  855. <tr class="tr1">
  856. <td class="td1">-l perl</td>
  857. <td class="td1">&lt;?perl ... ?&gt;</td>
  858. </tr>
  859. <tr class="tr1">
  860. <td class="td1">-l java</td>
  861. <td class="td1">&lt;?java ... ?&gt;</td>
  862. </tr>
  863. <tr class="tr1">
  864. <td class="td1">-l javascript</td>
  865. <td class="td1">&lt;?js ... ?&gt;</td>
  866. </tr>
  867. <tr class="tr1">
  868. <td class="td1">-l scheme</td>
  869. <td class="td1">&lt;?scheme ... ?&gt;</td>
  870. </tr>
  871. </table>
  872. </div>
  873. <a name="example10_x.result"></a>
  874. <div class="terminal_caption">
  875. output</div>
  876. <pre class="terminal">$ erubis -x <strong>--pi</strong> example10.xhtml
  877. _buf = ''; _buf &lt;&lt; '&lt;?xml version="1.0" ?&gt;
  878. ';
  879. lang = 'en'
  880. list = ['&lt;aaa&gt;', 'b&amp;b', '"ccc"']
  881. _buf &lt;&lt; '&lt;html lang="'; _buf &lt;&lt; (lang).to_s; _buf &lt;&lt; '"&gt;
  882. &lt;body&gt;
  883. &lt;ul&gt;
  884. '; for item in list
  885. _buf &lt;&lt; ' &lt;li&gt;'; _buf &lt;&lt; Erubis::XmlHelper.escape_xml(item); _buf &lt;&lt; '&lt;/li&gt;
  886. '; end
  887. _buf &lt;&lt; ' &lt;/ul&gt;
  888. &lt;/body&gt;
  889. &lt;/html&gt;
  890. ';
  891. _buf.to_s
  892. </pre>
  893. <p>Expression character can be changeable by command-line property '--embchar=<em>char</em>. Default is '<code>@</code>'.
  894. </p>
  895. <p>Use Erubis::PI::Eruby instead of Erubis::Eruby if you want to use PI as embedded pattern.
  896. </p>
  897. <a name="example10.rb"></a>
  898. <div class="program_caption">
  899. example10.rb</div>
  900. <pre class="program">require 'erubis'
  901. input = File.read('example10.xhtml')
  902. eruby = Erubis::PI::Eruby.new(input)
  903. print eruby.src
  904. </pre>
  905. <a name="example10.result"></a>
  906. <div class="terminal_caption">
  907. output</div>
  908. <pre class="terminal">$ ruby example10.rb
  909. _buf = ''; _buf &lt;&lt; '&lt;?xml version="1.0" ?&gt;
  910. ';
  911. lang = 'en'
  912. list = ['&lt;aaa&gt;', 'b&amp;b', '"ccc"']
  913. _buf &lt;&lt; '&lt;html lang="'; _buf &lt;&lt; (lang).to_s; _buf &lt;&lt; '"&gt;
  914. &lt;body&gt;
  915. &lt;ul&gt;
  916. '; for item in list
  917. _buf &lt;&lt; ' &lt;li&gt;'; _buf &lt;&lt; Erubis::XmlHelper.escape_xml(item); _buf &lt;&lt; '&lt;/li&gt;
  918. '; end
  919. _buf &lt;&lt; ' &lt;/ul&gt;
  920. &lt;/body&gt;
  921. &lt;/html&gt;
  922. ';
  923. _buf.to_s
  924. </pre>
  925. <p><strong>(experimental)</strong> Erubis supports '&lt;%= ... %&gt;' pattern with PI pattern.
  926. </p>
  927. <div class="program_caption">
  928. example of Rails view template</div>
  929. <pre class="program">&lt;table&gt;
  930. &lt;tr&gt;
  931. &lt;?rb for item in @list ?&gt;
  932. &lt;td&gt;@{item.id}@&lt;/td&gt;
  933. &lt;td&gt;@{item.name}@&lt;/td&gt;
  934. &lt;td&gt;
  935. <strong>&lt;%=</strong> link_to 'Destroy', {:action=&gt;'destroy', :id=&gt;item.id},
  936. :confirm=&gt;'Are you OK?' <strong>%&gt;</strong>
  937. &lt;/td&gt;
  938. &lt;?rb end ?&gt;
  939. &lt;/tr&gt;
  940. &lt;/table&gt;
  941. </pre>
  942. <br>
  943. <a name="tut-notext"></a>
  944. <h3 class="section2">Retrieve Ruby Code</h3>
  945. <p>Similar to '-x', ommand-line option '-X' shows converted Ruby source code.
  946. The difference between '-x' and 'X' is that the former converts text part but the latter ignores it.
  947. It means that you can retrieve Ruby code from eRuby script by '-X' option.
  948. </p>
  949. <p>For example, see the following eRuby script.
  950. This is some complex, so it is difficult to grasp the program code.
  951. </p>
  952. <a name="example11.rhtml"></a>
  953. <div class="program_caption">
  954. example11.rhtml</div>
  955. <pre class="program">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  956. &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  957. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  958. &lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt;
  959. &lt;body&gt;
  960. &lt;h3&gt;List&lt;/h3&gt;
  961. &lt;% if @list.nil? || @list.empty? %&gt;
  962. &lt;p&gt;not found.&lt;/p&gt;
  963. &lt;% else %&gt;
  964. &lt;table&gt;
  965. &lt;tbody&gt;
  966. &lt;% @list.each_with_index do |item, i| %&gt;
  967. &lt;tr bgcolor="&lt;%= i % 2 == 0 ? '#FCC' : '#CCF' %&gt;"&gt;
  968. &lt;td&gt;&lt;%= item %&gt;&lt;/td&gt;
  969. &lt;/tr&gt;
  970. &lt;% end %&gt;
  971. &lt;/tbody&gt;
  972. &lt;/table&gt;
  973. &lt;% end %&gt;
  974. &lt;/body&gt;
  975. &lt;/html&gt;
  976. </pre>
  977. <p>Command-line option '-X' extracts only the ruby code from eRuby script.
  978. </p>
  979. <a name="example11.result"></a>
  980. <div class="terminal_caption">
  981. result</div>
  982. <pre class="terminal">$ erubis <strong>-X</strong> example11.rhtml
  983. _buf = '';
  984. if @list.nil? || @list.empty?
  985. else
  986. @list.each_with_index do |item, i|
  987. _buf &lt;&lt; ( i % 2 == 0 ? '#FCC' : '#CCF' ).to_s;
  988. _buf &lt;&lt; ( item ).to_s;
  989. end
  990. end
  991. _buf.to_s
  992. </pre>
  993. <p>Command-line option '-C' (<strong>c</strong>mpact) deletes empty lines.
  994. </p>
  995. <a name="example11_C.result"></a>
  996. <div class="terminal_caption">
  997. result</div>
  998. <pre class="terminal">$ erubis <strong>-XC</strong> example11.rhtml
  999. _buf = '';
  1000. if @list.nil? || @list.empty?
  1001. else
  1002. @list.each_with_index do |item, i|
  1003. _buf &lt;&lt; ( i % 2 == 0 ? '#FCC' : '#CCF' ).to_s;
  1004. _buf &lt;&lt; ( item ).to_s;
  1005. end
  1006. end
  1007. _buf.to_s
  1008. </pre>
  1009. <p>Option '-U' (<strong>u</strong>nique) converts empty lines into a line.
  1010. </p>
  1011. <a name="example11_U.result"></a>
  1012. <div class="terminal_caption">
  1013. result</div>
  1014. <pre class="terminal">$ erubis <strong>-XU</strong> example11.rhtml
  1015. _buf = '';
  1016. if @list.nil? || @list.empty?
  1017. else
  1018. @list.each_with_index do |item, i|
  1019. _buf &lt;&lt; ( i % 2 == 0 ? '#FCC' : '#CCF' ).to_s;
  1020. _buf &lt;&lt; ( item ).to_s;
  1021. end
  1022. end
  1023. _buf.to_s
  1024. </pre>
  1025. <p>Option '-N' (<strong>n</strong>umber) adds line number.
  1026. It is available with '-C' or '-U'.
  1027. </p>
  1028. <a name="example11_N.result"></a>
  1029. <div class="terminal_caption">
  1030. result</div>
  1031. <pre class="terminal">$ erubis <strong>-XNU</strong> example11.rhtml
  1032. 1: _buf = '';
  1033. 7: if @list.nil? || @list.empty?
  1034. 9: else
  1035. 12: @list.each_with_index do |item, i|
  1036. 13: _buf &lt;&lt; ( i % 2 == 0 ? '#FCC' : '#CCF' ).to_s;
  1037. 14: _buf &lt;&lt; ( item ).to_s;
  1038. 16: end
  1039. 19: end
  1040. 22: _buf.to_s
  1041. </pre>
  1042. <p>Command-line option '-X' is available with PHP script.
  1043. </p>
  1044. <a name="example11.php"></a>
  1045. <div class="program_caption">
  1046. example11.php</div>
  1047. <pre class="program">&lt;?xml version="1.0"?&gt;
  1048. &lt;html&gt;
  1049. &lt;body&gt;
  1050. &lt;h3&gt;List&lt;/h3&gt;
  1051. &lt;?php if (!$list) { ?&gt;
  1052. &lt;p&gt;not found.&lt;/p&gt;
  1053. &lt;?php } else { ?&gt;
  1054. &lt;table&gt;
  1055. &lt;tbody&gt;
  1056. &lt;?php $i = 0; ?&gt;
  1057. &lt;?php foreach ($list as $item) { ?&gt;
  1058. &lt;tr bgcolor="&lt;?php echo ++$i % 2 == 1 ? '#FCC' : '#CCF'; ?&gt;"&gt;
  1059. &lt;td&gt;&lt;?php echo $item; ?&gt;&lt;/td&gt;
  1060. &lt;/tr&gt;
  1061. &lt;?php } ?&gt;
  1062. &lt;/tbody&gt;
  1063. &lt;/table&gt;
  1064. &lt;?php } ?&gt;
  1065. &lt;/body&gt;
  1066. &lt;/html&gt;
  1067. </pre>
  1068. <a name="example11_php.result"></a>
  1069. <div class="terminal_caption">
  1070. result</div>
  1071. <pre class="terminal">$ erubis -XNU <strong>-l php</strong> <strong>--pi=php</strong> --trim=false example11.php
  1072. 5: &lt;?php if (!$list) { ?&gt;
  1073. 7: &lt;?php } else { ?&gt;
  1074. 10: &lt;?php $i = 0; ?&gt;
  1075. 11: &lt;?php foreach ($list as $item) { ?&gt;
  1076. 12: &lt;?php echo ++$i % 2 == 1 ? '#FCC' : '#CCF'; ?&gt;
  1077. 13: &lt;?php echo $item; ?&gt;
  1078. 15: &lt;?php } ?&gt;
  1079. 18: &lt;?php } ?&gt;
  1080. </pre>
  1081. <br>
  1082. <br>
  1083. <a name="enhancer"></a>
  1084. <h2 class="section1">Enhancer</h2>
  1085. <p>Enhancer is a module to add a certain feature into Erubis::Eruby class.
  1086. Enhancer may be language-independent or only for Erubis::Eruby class.
  1087. </p>
  1088. <p>To use enhancers, define subclass and include them.
  1089. 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>.
  1090. </p>
  1091. <pre class="program">class MyEruby &lt; Erubis::Eruby
  1092. include EscapeEnhancer
  1093. include PercentLineEnhancer
  1094. include BiPatternEnhancer
  1095. end
  1096. </pre>
  1097. <p>You can specify enhancers in command-line with option '-E'.
  1098. The following is an example to use some enhancers in command-line.
  1099. </p>
  1100. <pre class="terminal">$ erubis -xE Escape,PercentLine,BiPattern example.eruby
  1101. </pre>
  1102. <p>The following is the list of enhancers.
  1103. </p>
  1104. <dl class="dl1">
  1105. <dt class="dt1">
  1106. <a href="#escape-enhancer">EscapeEnhander</a> (language-independent)</dt>
  1107. <dd class="dd1">
  1108. Switch '&lt;%= %&gt;' to escaped and '&lt;%== %&gt;' to unescaped.
  1109. </dd>
  1110. <dt class="dt1">
  1111. <a href="#stdout-enhancer">StdoutEnhancer</a> (only for Eruby)</dt>
  1112. <dd class="dd1">
  1113. Use $stdout instead of array buffer.
  1114. </dd>
  1115. <dt class="dt1">
  1116. <a href="#printout-enhancer">PrintOutEnhancer</a> (only for Eruby)</dt>
  1117. <dd class="dd1">
  1118. Use "print(...)" statement insead of "_buf &lt;&lt; ...".
  1119. </dd>
  1120. <dt class="dt1">
  1121. <a href="#printenabled-enhancer">PrintEnabledEnhancer</a> (only for Eruby)</dt>
  1122. <dd class="dd1">
  1123. Enable to use print() in '&lt;% ... %&gt;'.
  1124. </dd>
  1125. <dt class="dt1">
  1126. <a href="#array-enhancer">ArrayEnhancer</a> (only for Eruby)</dt>
  1127. <dd class="dd1">
  1128. Return array of string instead of returning string.
  1129. </dd>
  1130. <dt class="dt1">
  1131. <a href="#arraybuffer-enhancer">ArrayBufferEnhancer</a> (only for Eruby)</dt>
  1132. <dd class="dd1">
  1133. Use array buffer. It is a little slower than StringBufferEnhancer.
  1134. </dd>
  1135. <dt class="dt1">
  1136. <a href="#stringbuffer-enhancer">StringBufferEnhancer</a> (only for Eruby)</dt>
  1137. <dd class="dd1">
  1138. Use string buffer. This is included in Erubis::Eruby by default.
  1139. </dd>
  1140. <dt class="dt1">
  1141. <a href="#erbout-enhancer">ErboutEnhancer</a> (only for Eruby)</dt>
  1142. <dd class="dd1">
  1143. Set '_erbout = _buf = "";' to be compatible with ERB.
  1144. </dd>
  1145. <dt class="dt1">
  1146. <a href="#notext-enhancer">NoTextEnhancer</a> (language-independent)</dt>
  1147. <dd class="dd1">
  1148. Print embedded code only and ignore normal text.
  1149. </dd>
  1150. <dt class="dt1">
  1151. <a href="#nocode-enhancer">NoCodeEnhancer</a> (language-independent)</dt>
  1152. <dd class="dd1">
  1153. Print normal text only and ignore code.
  1154. </dd>
  1155. <dt class="dt1">
  1156. <a href="#simplify-enhancer">SimplifyEnhancer</a> (language-independent)</dt>
  1157. <dd class="dd1">
  1158. Make compile faster but don't trim spaces around '&lt;% %&gt;'.
  1159. </dd>
  1160. <dt class="dt1">
  1161. <a href="#bipattern-enhancer">BiPatternEnhancer</a> (language-independent)</dt>
  1162. <dd class="dd1">
  1163. [experimental] Enable to use another embedded pattern with '&lt;% %&gt;'.
  1164. </dd>
  1165. <dt class="dt1">
  1166. <a href="#percentline-enhancer">PercentLineEnhancer</a> (language-independent)</dt>
  1167. <dd class="dd1">
  1168. Regard lines starting with '%' as Ruby code. This is for compatibility with eruby and ERB.
  1169. </dd>
  1170. <dt class="dt1">
  1171. <a href="#headerfooter-enhancer">HeaderFooterEnhancer</a> (language-independent)</dt>
  1172. <dd class="dd1">
  1173. [experimental] Enable you to add header and footer in eRuby script.
  1174. </dd>
  1175. <dt class="dt1">
  1176. <a href="#interpolation-enhancer">InterpolationEnhancer</a> (only for Eruby)</dt>
  1177. <dd class="dd1">
  1178. [experimental] convert '&lt;p&gt;&lt;%= text %&gt;&lt;/p&gt;' into '_buf &lt;&lt; %Q`&lt;p&gt;#{text}&lt;/p&gt;`'.
  1179. </dd>
  1180. <dt class="dt1">
  1181. <a href="#deleteindent-enhancer">DeleteIndentEnhancer</a> (language-independent)</dt>
  1182. <dd class="dd1">
  1183. [experimental] delete indentation of HTML file and eliminate page size.
  1184. </dd>
  1185. </dl>
  1186. <p>If you required 'erubis/engine/enhanced', Eruby subclasses which include each enhancers are defined.
  1187. For example, class BiPatternEruby includes BiPatternEnhancer.
  1188. </p>
  1189. <a name="escape-enhancer"></a>
  1190. <h3 class="section2">EscapeEnhancer</h3>
  1191. <p>EscapeEnhancer switches '&lt;%= ... %&gt;' to escaped and '&lt;%== ... %&gt;' to unescaped.
  1192. </p>
  1193. <a name="example.eruby"></a>
  1194. <div class="program_caption">
  1195. example.eruby</div>
  1196. <pre class="program">&lt;div&gt;
  1197. &lt;% for item in list %&gt;
  1198. &lt;p&gt;&lt;%= item %&gt;&lt;/p&gt;
  1199. &lt;p&gt;&lt;%== item %&gt;&lt;/p&gt;
  1200. &lt;% end %&gt;
  1201. &lt;/div&gt;
  1202. </pre>
  1203. <a name="escape_example.result"></a>
  1204. <div class="terminal_caption">
  1205. compiled source code</div>
  1206. <pre class="terminal">$ erubis -xE Escape example.eruby
  1207. _buf = ''; _buf &lt;&lt; '&lt;div&gt;
  1208. '; for item in list
  1209. _buf &lt;&lt; ' &lt;p&gt;'; <strong>_buf &lt;&lt; Erubis::XmlHelper.escape_xml( item );</strong> _buf &lt;&lt; '&lt;/p&gt;
  1210. &lt;p&gt;'; <strong>_buf &lt;&lt; ( item ).to_s;</strong> _buf &lt;&lt; '&lt;/p&gt;
  1211. '; end
  1212. _buf &lt;&lt; '&lt;/div&gt;
  1213. ';
  1214. _buf.to_s
  1215. </pre>
  1216. <p>EscapeEnhancer is language-independent.
  1217. </p>
  1218. <br>
  1219. <a name="stdout-enhancer"></a>
  1220. <h3 class="section2">StdoutEnhancer</h3>
  1221. <p>StdoutEnhancer use $sdtdout instead of array buffer.
  1222. Therefore, you can use 'print' statement in embedded ruby code.
  1223. </p>
  1224. <a name="stdout_exmple.result"></a>
  1225. <div class="terminal_caption">
  1226. compiled source code</div>
  1227. <pre class="terminal">$ erubis -xE Stdout example.eruby
  1228. <strong>_buf = $stdout;</strong> _buf &lt;&lt; '&lt;div&gt;
  1229. '; for item in list
  1230. _buf &lt;&lt; ' &lt;p&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/p&gt;
  1231. &lt;p&gt;'; _buf &lt;&lt; Erubis::XmlHelper.escape_xml( item ); _buf &lt;&lt; '&lt;/p&gt;
  1232. '; end
  1233. _buf &lt;&lt; '&lt;/div&gt;
  1234. ';
  1235. <strong>''</strong>
  1236. </pre>
  1237. <p>StdoutEnhancer is only for Eruby.
  1238. </p>
  1239. <br>
  1240. <a name="printout-enhancer"></a>
  1241. <h3 class="section2">PrintOutEnhancer</h3>
  1242. <p>PrintOutEnhancer makes compiled source code to use 'print(...)' instead of '_buf &lt;&lt; ...'.
  1243. </p>
  1244. <a name="printstatement_example.result"></a>
  1245. <div class="terminal_caption">
  1246. compiled source code</div>
  1247. <pre class="terminal">$ erubis -xE PrintOut example.eruby
  1248. <strong>print</strong> '&lt;div&gt;
  1249. '; for item in list
  1250. <strong>print</strong> ' &lt;p&gt;'; <strong>print</strong>(( item ).to_s); <strong>print</strong> '&lt;/p&gt;
  1251. &lt;p&gt;'; <strong>print</strong> Erubis::XmlHelper.escape_xml( item ); <strong>print</strong> '&lt;/p&gt;
  1252. '; end
  1253. <strong>print</strong> '&lt;/div&gt;
  1254. ';
  1255. </pre>
  1256. <p>PrintOutEnhancer is only for Eruby.
  1257. </p>
  1258. <br>
  1259. <a name="printenabled-enhancer"></a>
  1260. <h3 class="section2">PrintEnabledEnhancer</h3>
  1261. <p>PrintEnabledEnhancer enables you to use print() method in '&lt;% ... %&gt;'.
  1262. </p>
  1263. <a name="printenabled-example.eruby"></a>
  1264. <div class="program_caption">
  1265. printenabled-example.eruby</div>
  1266. <pre class="program">&lt;% for item in @list %&gt;
  1267. &lt;b&gt;<strong>&lt;% print item %&gt;</strong>&lt;/b&gt;
  1268. &lt;% end %&gt;
  1269. </pre>
  1270. <a name="printenabled-example.rb"></a>
  1271. <div class="program_caption">
  1272. printenabled-example.rb</div>
  1273. <pre class="program">require 'erubis'
  1274. class PrintEnabledEruby &lt; Erubis::Eruby
  1275. include Erubis::PrintEnabledEnhancer
  1276. end
  1277. input = File.read('printenabled-example.eruby')
  1278. eruby = PrintEnabledEruby.new(input)
  1279. list = ['aaa', 'bbb', 'ccc']
  1280. print eruby.evaluate(:list=&gt;list)
  1281. </pre>
  1282. <a name="printenable_example.result"></a>
  1283. <div class="terminal_caption">
  1284. output result</div>
  1285. <pre class="terminal">$ ruby printenabled-example.rb
  1286. &lt;b&gt;aaa&lt;/b&gt;
  1287. &lt;b&gt;bbb&lt;/b&gt;
  1288. &lt;b&gt;ccc&lt;/b&gt;
  1289. </pre>
  1290. <p>Notice to use Eruby#evaluate() and not to use Eruby#result(),
  1291. because print() method in '&lt;% ... %&gt;' invokes not Kernel#print() but PrintEnabledEnhancer#print().
  1292. </p>
  1293. <p>PrintEnabledEnhancer is only for Eruby.
  1294. </p>
  1295. <br>
  1296. <a name="array-enhancer"></a>
  1297. <h3 class="section2">ArrayEnhancer</h3>
  1298. <p>ArrayEnhancer makes Eruby to return an array of strings.
  1299. </p>
  1300. <a name="array_example.result"></a>
  1301. <div class="terminal_caption">
  1302. compiled source code</div>
  1303. <pre class="terminal">$ erubis -xE Array example.eruby
  1304. <strong>_buf = [];</strong> _buf &lt;&lt; '&lt;div&gt;
  1305. '; for item in list
  1306. _buf &lt;&lt; ' &lt;p&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/p&gt;
  1307. &lt;p&gt;'; _buf &lt;&lt; Erubis::XmlHelper.escape_xml( item ); _buf &lt;&lt; '&lt;/p&gt;
  1308. '; end
  1309. _buf &lt;&lt; '&lt;/div&gt;
  1310. ';
  1311. <strong>_buf</strong>
  1312. </pre>
  1313. <p>ArrayEnhancer is only for Eruby.
  1314. </p>
  1315. <br>
  1316. <a name="arraybuffer-enhancer"></a>
  1317. <h3 class="section2">ArrayBufferEnhancer</h3>
  1318. <p>ArrayBufferEnhancer makes Eruby to use array buffer.
  1319. Array buffer is a litte slower than String buffer.
  1320. </p>
  1321. <p>ArrayBufferEnhancer is only for Eruby.
  1322. </p>
  1323. <a name="arraybuffer_example.result"></a>
  1324. <div class="terminal_caption">
  1325. compiled source code</div>
  1326. <pre class="terminal">$ erubis -xE ArrayBuffer example.eruby
  1327. <strong>_buf = [];</strong> _buf &lt;&lt; '&lt;div&gt;
  1328. '; for item in list
  1329. _buf &lt;&lt; ' &lt;p&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/p&gt;
  1330. &lt;p&gt;'; _buf &lt;&lt; Erubis::XmlHelper.escape_xml( item ); _buf &lt;&lt; '&lt;/p&gt;
  1331. '; end
  1332. _buf &lt;&lt; '&lt;/div&gt;
  1333. ';
  1334. <strong>_buf.join</strong>
  1335. </pre>
  1336. <br>
  1337. <a name="stringbuffer-enhancer"></a>
  1338. <h3 class="section2">StringBufferEnhancer</h3>
  1339. <p>StringBufferEnhancer makes Eruby to use string buffer.
  1340. String buffer is a little faster than array buffer.
  1341. Erubis::Eruby includes this enhancer by default.
  1342. </p>
  1343. <a name="stringbuffer_example.result"></a>
  1344. <div class="terminal_caption">
  1345. compiled source code</div>
  1346. <pre class="terminal">$ erubis -xE StringBuffer example.eruby
  1347. <strong>_buf = '';</strong> _buf &lt;&lt; '&lt;div&gt;
  1348. '; for item in list
  1349. _buf &lt;&lt; ' &lt;p&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/p&gt;
  1350. &lt;p&gt;'; _buf &lt;&lt; Erubis::XmlHelper.escape_xml( item ); _buf &lt;&lt; '&lt;/p&gt;
  1351. '; end
  1352. _buf &lt;&lt; '&lt;/div&gt;
  1353. ';
  1354. <strong>_buf.to_s</strong>
  1355. </pre>
  1356. <p>StringBufferEnhancer is only for Eruby.
  1357. </p>
  1358. <br>
  1359. <a name="erbout-enhancer"></a>
  1360. <h3 class="section2">ErboutEnhancer</h3>
  1361. <p>ErboutEnhancer makes Eruby to be compatible with ERB.
  1362. This is useful especially for Ruby on Rails.
  1363. </p>
  1364. <div class="terminal_caption">
  1365. compiled source code</div>
  1366. <pre class="terminal">$ erubis -xE Erbout example.eruby
  1367. <strong>_erbout = _buf = '';</strong> _buf &lt;&lt; '&lt;div&gt;
  1368. '; for item in list
  1369. _buf &lt;&lt; ' &lt;p&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/p&gt;
  1370. &lt;p&gt;'; _buf &lt;&lt; Erubis::XmlHelper.escape_xml( item ); _buf &lt;&lt; '&lt;/p&gt;
  1371. '; end
  1372. _buf &lt;&lt; '&lt;/div&gt;
  1373. ';
  1374. _buf.to_s
  1375. </pre>
  1376. <p>ErboutEnhancer is only for Eruby.
  1377. </p>
  1378. <br>
  1379. <a name="notext-enhancer"></a>
  1380. <h3 class="section2">NoTextEnhancer</h3>
  1381. <p>NoTextEnhancer suppress output of text and prints only embedded code.
  1382. This is useful especially when debugging a complex eRuby script.
  1383. </p>
  1384. <a name="notext-example.eruby"></a>
  1385. <div class="program_caption">
  1386. notext-example.eruby</div>
  1387. <pre class="program">&lt;h3&gt;List&lt;/h3&gt;
  1388. &lt;% if !@list || @list.empty? %&gt;
  1389. &lt;p&gt;not found.&lt;/p&gt;
  1390. &lt;% else %&gt;
  1391. &lt;table&gt;
  1392. &lt;tbody&gt;
  1393. &lt;% @list.each_with_index do |item, i| %&gt;
  1394. &lt;tr bgcolor="&lt;%= i%2 == 0 ? '#FFCCCC' : '#CCCCFF' %&gt;"&gt;
  1395. &lt;td&gt;&lt;%= item %&gt;&lt;/td&gt;
  1396. &lt;/tr&gt;
  1397. &lt;% end %&gt;
  1398. &lt;/tbody&gt;
  1399. &lt;/table&gt;
  1400. &lt;% end %&gt;
  1401. </pre>
  1402. <a name="notext_example.result"></a>
  1403. <div class="terminal_caption">
  1404. output example of NoTextEnhancer</div>
  1405. <pre class="terminal">$ erubis -xE NoText notext-example.eruby
  1406. _buf = '';
  1407. if !@list || @list.empty?
  1408. else
  1409. @list.each_with_index do |item, i|
  1410. _buf &lt;&lt; ( i%2 == 0 ? '#FFCCCC' : '#CCCCFF' ).to_s;
  1411. _buf &lt;&lt; ( item ).to_s;
  1412. end
  1413. end
  1414. _buf.to_s
  1415. </pre>
  1416. <p>NoTextEnhancer is language-independent. It is useful even if you are PHP user, see <a href="#topics-php">this section</a>.
  1417. </p>
  1418. <br>
  1419. <a name="nocode-enhancer"></a>
  1420. <h3 class="section2">NoCodeEnhancer</h3>
  1421. <p>NoCodeEnhancer suppress output of embedded code and prints only normal text.
  1422. This is useful especially when validating HTML tags.
  1423. </p>
  1424. <a name="nocode-example.eruby"></a>
  1425. <div class="program_caption">
  1426. nocode-example.eruby</div>
  1427. <pre class="program">&lt;h3&gt;List&lt;/h3&gt;
  1428. &lt;% if !@list || @list.empty? %&gt;
  1429. &lt;p&gt;not found.&lt;/p&gt;
  1430. &lt;% else %&gt;
  1431. &lt;table&gt;
  1432. &lt;tbody&gt;
  1433. &lt;% @list.each_with_index do |item, i| %&gt;
  1434. &lt;tr bgcolor="&lt;%= i%2 == 0 ? '#FFCCCC' : '#CCCCFF' %&gt;"&gt;
  1435. &lt;td&gt;&lt;%= item %&gt;&lt;/td&gt;
  1436. &lt;/tr&gt;
  1437. &lt;% end %&gt;
  1438. &lt;/tbody&gt;
  1439. &lt;/table&gt;
  1440. &lt;% end %&gt;
  1441. </pre>
  1442. <a name="nocode_example.result"></a>
  1443. <div class="terminal_caption">
  1444. output example of NoCodeEnhancer</div>
  1445. <pre class="terminal">$ erubis -xE NoCode notext-example.eruby
  1446. &lt;h3&gt;List&lt;/h3&gt;
  1447. &lt;p&gt;not found.&lt;/p&gt;
  1448. &lt;table&gt;
  1449. &lt;tbody&gt;
  1450. &lt;tr bgcolor=""&gt;
  1451. &lt;td&gt;&lt;/td&gt;
  1452. &lt;/tr&gt;
  1453. &lt;/tbody&gt;
  1454. &lt;/table&gt;
  1455. </pre>
  1456. <p>NoCodeEnhancer is language-independent. It is useful even if you are PHP user, see <a href="#topics-php">this section</a>.
  1457. </p>
  1458. <br>
  1459. <a name="simplify-enhancer"></a>
  1460. <h3 class="section2">SimplifyEnhancer</h3>
  1461. <p>SimplifyEnhancer makes compiling a little faster but don't trim spaces around '&lt;% %&gt;'.
  1462. </p>
  1463. <a name="simplify_example.result"></a>
  1464. <div class="terminal_caption">
  1465. compiled source code</div>
  1466. <pre class="terminal">$ erubis -xE Simplify example.eruby
  1467. _buf = ''; _buf &lt;&lt; '&lt;div&gt;
  1468. '; for item in list ; _buf &lt;&lt; '
  1469. &lt;p&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/p&gt;
  1470. &lt;p&gt;'; _buf &lt;&lt; Erubis::XmlHelper.escape_xml( item ); _buf &lt;&lt; '&lt;/p&gt;
  1471. '; end ; _buf &lt;&lt; '
  1472. &lt;/div&gt;
  1473. ';
  1474. _buf.to_s
  1475. </pre>
  1476. <p>SimplifyEnhancer is language-independent.
  1477. </p>
  1478. <br>
  1479. <a name="bipattern-enhancer"></a>
  1480. <h3 class="section2">BiPatternEnhancer</h3>
  1481. <p>BiPatternEnhancer enables to use another embedded pattern with '&lt;% %&gt;'.
  1482. By Default, '[= ... =]' is available for expression.
  1483. You can specify pattern by :bipattern property.
  1484. </p>
  1485. <a name="bipattern-example.rhtml"></a>
  1486. <div class="program_caption">
  1487. bipattern-example.rhtml</div>
  1488. <pre class="program">&lt;% for item in list %&gt;
  1489. &lt;b&gt;<strong>[= item =]</strong>&lt;/b&gt;
  1490. &lt;b&gt;<strong>[== item

Large files files are truncated, but you can click here to view the full file