PageRenderTime 41ms CodeModel.GetById 6ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  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 =]</strong>&lt;/b&gt;
  1491. &lt;% end %&gt;
  1492. </pre>
  1493. <a name="bipattern_example.result"></a>
  1494. <div class="terminal_caption">
  1495. compiled source code</div>
  1496. <pre class="terminal">$ erubis -xE BiPattern bipattern-example.rhtml
  1497. _buf = ''; for item in list
  1498. _buf &lt;&lt; ' &lt;b&gt;'; <strong>_buf &lt;&lt; ( item ).to_s;</strong> _buf &lt;&lt; '&lt;/b&gt;
  1499. &lt;b&gt;'; <strong>_buf &lt;&lt; Erubis::XmlHelper.escape_xml( item );</strong> _buf &lt;&lt; '&lt;/b&gt;
  1500. '; end
  1501. _buf.to_s
  1502. </pre>
  1503. <p>BiPatternEnhancer is language-independent.
  1504. </p>
  1505. <br>
  1506. <a name="percentline-enhancer"></a>
  1507. <h3 class="section2">PercentLineEnhancer</h3>
  1508. <p>PercentLineEnhancer regards lines starting with '%' as Ruby code.
  1509. This is for compatibility with eruby and ERB.
  1510. </p>
  1511. <a name="percentline-example.rhtml"></a>
  1512. <div class="program_caption">
  1513. percentline-example.rhtml</div>
  1514. <pre class="program"><strong>% for item in list</strong>
  1515. &lt;b&gt;&lt;%= item %&gt;&lt;/b&gt;
  1516. <strong>% end</strong>
  1517. %% lines with '%%'
  1518. </pre>
  1519. <a name="percentline_example.result"></a>
  1520. <div class="terminal_caption">
  1521. compiled source code</div>
  1522. <pre class="terminal">$ erubis -xE PercentLine percentline-example.rhtml
  1523. _buf = ''; <strong>for item in list</strong>
  1524. _buf &lt;&lt; ' &lt;b&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/b&gt;
  1525. '; <strong>end</strong>
  1526. _buf &lt;&lt; '% lines with \'%%\'
  1527. ';
  1528. _buf.to_s
  1529. </pre>
  1530. <p>PercentLineEnhancer is language-independent.
  1531. </p>
  1532. <br>
  1533. <a name="headerfooter-enhancer"></a>
  1534. <h3 class="section2">HeaderFooterEnhancer</h3>
  1535. <p>[experimental]
  1536. </p>
  1537. <p>HeaderFooterEnhancer enables you to add header and footer in eRuby script.
  1538. </p>
  1539. <a name="headerfooter-example.eruby"></a>
  1540. <div class="program_caption">
  1541. headerfooter-example.eruby</div>
  1542. <pre class="program"><strong>&lt;!--#header:</strong>
  1543. <strong>def list_items(items)</strong>
  1544. <strong>#--&gt;</strong>
  1545. &lt;% for item in items %&gt;
  1546. &lt;b&gt;&lt;%= item %&gt;&lt;/b&gt;
  1547. &lt;% end %&gt;
  1548. <strong>&lt;!--#footer:</strong>
  1549. <strong>end</strong>
  1550. <strong>#--&gt;</strong>
  1551. </pre>
  1552. <a name="headerfooter_example.result"></a>
  1553. <div class="terminal_caption">
  1554. compiled source code</div>
  1555. <pre class="terminal">$ erubis -xE HeaderFooter headerfooter-example.eruby
  1556. <strong>def list_items(items)</strong>
  1557. _buf = ''; for item in items
  1558. _buf &lt;&lt; ' &lt;b&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/b&gt;
  1559. '; end
  1560. _buf.to_s
  1561. <strong>end</strong>
  1562. </pre>
  1563. <p>Compare to the following:
  1564. </p>
  1565. <a name="normal-eruby-test.eruby"></a>
  1566. <div class="program_caption">
  1567. normal-eruby-test.eruby</div>
  1568. <pre class="program"><strong>&lt;%</strong>
  1569. <strong>def list_items(items)</strong>
  1570. <strong>%&gt;</strong>
  1571. &lt;% for item in items %&gt;
  1572. &lt;li&gt;&lt;%= item %&gt;&lt;/li&gt;
  1573. &lt;% end %&gt;
  1574. <strong>&lt;%</strong>
  1575. <strong>end</strong>
  1576. <strong>%&gt;</strong>
  1577. </pre>
  1578. <a name="normal_eruby_test.result"></a>
  1579. <div class="terminal_caption">
  1580. compiled source code</div>
  1581. <pre class="terminal">$ erubis -x normal-eruby-test.eruby
  1582. _buf = '';
  1583. <strong>def list_items(items)</strong>
  1584. for item in items
  1585. _buf &lt;&lt; '&lt;li&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/li&gt;
  1586. '; end
  1587. <strong>end</strong>
  1588. _buf.to_s
  1589. </pre>
  1590. <p>Header and footer can be in any position in eRuby script,
  1591. that is, header is no need to be in the head of eRuby script.
  1592. </p>
  1593. <a name="headerfooter-example2.rhtml"></a>
  1594. <div class="program_caption">
  1595. headerfooter-example2.rhtml</div>
  1596. <pre class="program">&lt;?xml version="1.0"?&gt;
  1597. &lt;html&gt;
  1598. <strong>&lt;!--#header:</strong>
  1599. <strong>def page(list)</strong>
  1600. <strong>#--&gt;</strong>
  1601. :
  1602. <strong>&lt;!--#footer:</strong>
  1603. <strong>end</strong>
  1604. <strong>#--&gt;</strong>
  1605. &lt;/html&gt;
  1606. </pre>
  1607. <a name="headerfooter_example2.result"></a>
  1608. <div class="terminal_caption">
  1609. compiled source code</div>
  1610. <pre class="terminal">$ erubis -xE HeaderFooter headerfooter-example2.rhtml
  1611. <strong>def page(list)</strong>
  1612. _buf = ''; _buf &lt;&lt; '&lt;?xml version="1.0"?&gt;
  1613. &lt;html&gt;
  1614. '; _buf &lt;&lt; ' :
  1615. '; _buf &lt;&lt; '&lt;/html&gt;
  1616. ';
  1617. _buf.to_s
  1618. <strong>end</strong>
  1619. </pre>
  1620. <p>HeaderFooterEnhancer is experimental and is language-independent.
  1621. </p>
  1622. <br>
  1623. <a name="interpolation-enhancer"></a>
  1624. <h3 class="section2">InterpolationEnhancer</h3>
  1625. <p>[experimental]
  1626. </p>
  1627. <p>InterpolationEnhancer converts "&lt;h1&gt;&lt;%= title %&gt;&lt;/h1&gt;" into
  1628. "_buf &lt;&lt; %Q`&lt;h1&gt;#{ title }&lt;/h1&gt;`".
  1629. This makes Eruby a litter faster because method call of String#&lt;&lt; are eliminated
  1630. by expression interpolations.
  1631. </p>
  1632. <div class="program_caption">
  1633. InterpolationEnhancer elmininates method call of String#&lt;&lt;.</div>
  1634. <pre class="program">## Assume that input is '&lt;a href="&lt;%=url%&gt;"&gt;&lt;%=name%&gt;&lt;/a&gt;'.
  1635. ## Eruby convert input into the following code. String#&lt;&lt; is called 5 times.
  1636. _buf &lt;&lt; '&lt;a href="'; _buf &lt;&lt; (url).to_s; _buf &lt;&lt; '"&gt;'; _buf &lt;&lt; (name).to_s; _buf &lt;&lt; '&lt;/a&gt;';
  1637. ## If InterpolationEnhancer is used, String#&lt;&lt; is called only once.
  1638. _buf &lt;&lt; %Q`&lt;a href="#{url}"&gt;#{name}&lt;/a&gt;`;
  1639. </pre>
  1640. <a name="interpolation_example.result"></a>
  1641. <div class="terminal_caption">
  1642. compiled source code</div>
  1643. <pre class="terminal">$ erubis -xE Interpolation example.eruby
  1644. _buf = ''; _buf &lt;&lt; <strong>%Q`</strong>&lt;div&gt;\n<strong>`</strong>
  1645. for item in list
  1646. _buf &lt;&lt; <strong>%Q`</strong> &lt;p&gt;<strong>#{ item }</strong>&lt;/p&gt;
  1647. &lt;p&gt;<strong>#{Erubis::XmlHelper.escape_xml( item )}</strong>&lt;/p&gt;\n<strong>`</strong>
  1648. end
  1649. _buf &lt;&lt; <strong>%Q`</strong>&lt;/div&gt;\n<strong>`</strong>
  1650. _buf.to_s
  1651. </pre>
  1652. <p>Erubis provides Erubis::FastEruby class which includes InterpolationEnhancer.
  1653. You can use Erubis::FastEruby class instead of Erubis::Eruby class.
  1654. </p>
  1655. <p>InterpolationEnhancer is only for Eruby.
  1656. </p>
  1657. <br>
  1658. <a name="deleteindent-enhancer"></a>
  1659. <h3 class="section2">DeleteIndentEnhancer</h3>
  1660. <p>[experimental]
  1661. DeleteIndentEnhancer deletes indentation of HTML file.
  1662. </p>
  1663. <a name="interpolation_example.result"></a>
  1664. <div class="terminal_caption">
  1665. compiled source code</div>
  1666. <pre class="terminal">$ erubis -xE DeleteIndent example.eruby
  1667. _buf = ''; _buf &lt;&lt; '&lt;div&gt;
  1668. '; for item in list
  1669. _buf &lt;&lt; '&lt;p&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/p&gt;
  1670. &lt;p&gt;'; _buf &lt;&lt; Erubis::XmlHelper.escape_xml( item ); _buf &lt;&lt; '&lt;/p&gt;
  1671. '; end
  1672. _buf &lt;&lt; '&lt;/div&gt;
  1673. ';
  1674. _buf.to_s
  1675. </pre>
  1676. <p>Notice that DeleteIndentEnhancer isn't intelligent.
  1677. It deletes indentations even if they are in &lt;PRE&gt;&lt;/PRE&gt;.
  1678. </p>
  1679. <p>DeleteIndentEnhancer is language-independent.
  1680. </p>
  1681. <br>
  1682. <br>
  1683. <a name="lang"></a>
  1684. <h2 class="section1">Multi-Language Support</h2>
  1685. <p>Erubis supports the following languages<sup>(<a href="#fnref:2" name="fnlink:2">*2</a>)</sup>:
  1686. </p>
  1687. <ul type="disc">
  1688. <li>Ruby
  1689. </li>
  1690. <li><a href="#lang-php">PHP</a>
  1691. </li>
  1692. <li><a href="#lang-c">C</a>
  1693. </li>
  1694. <li><a href="#lang-java">Java</a>
  1695. </li>
  1696. <li><a href="#lang-scheme">Scheme</a>
  1697. </li>
  1698. <li><a href="#lang-perl">Perl</a>
  1699. </li>
  1700. <li><a href="#lang-javascript">JavaScript</a>
  1701. </li>
  1702. </ul>
  1703. <div class="footnote">
  1704. <dl compact>
  1705. <dt>(<a name="fnref:2" href="#fnlink:2">*2</a>)</dt>
  1706. <dd>If you need template engine in pure PHP/Perl/JavaScript, try <a href="http://www.kuwata-lab.com/tenjin/">Tenjin</a> (<a href="http://www.kuwata-lab.com/tenjin/">http://www.kuwata-lab.com/tenjin/</a>). Tenjin is a very fast and full-featured template engine implemented in pure PHP/Perl/JavaScript.</dd>
  1707. </dl>
  1708. </div>
  1709. <a name="lang-php"></a>
  1710. <h3 class="section2">PHP</h3>
  1711. <a name="example.ephp"></a>
  1712. <div class="program_caption">
  1713. example.ephp</div>
  1714. <pre class="program">&lt;?xml version="1.0"?&gt;
  1715. &lt;html&gt;
  1716. &lt;body&gt;
  1717. &lt;p&gt;Hello <strong>&lt;%= $user %&gt;</strong>!&lt;/p&gt;
  1718. &lt;table&gt;
  1719. &lt;tbody&gt;
  1720. <strong>&lt;% $i = 0; %&gt;</strong>
  1721. <strong>&lt;% foreach ($list as $item) { %&gt;</strong>
  1722. <strong>&lt;% $i++; %&gt;</strong>
  1723. &lt;tr bgcolor=<strong>"&lt;%= $i % 2 == 0 ? '#FFCCCC' : '#CCCCFF' %&gt;"</strong>&gt;
  1724. &lt;td&gt;<strong>&lt;%= $i %&gt;</strong>&lt;/td&gt;
  1725. &lt;td&gt;<strong>&lt;%== $item %&gt;</strong>&lt;/td&gt;
  1726. &lt;/tr&gt;
  1727. <strong>&lt;% } %&gt;</strong>
  1728. &lt;/tbody&gt;
  1729. &lt;/table&gt;
  1730. &lt;/body&gt;
  1731. &lt;/html&gt;
  1732. </pre>
  1733. <a name="example_php.result"></a>
  1734. <div class="terminal_caption">
  1735. compiled source code</div>
  1736. <pre class="terminal">$ erubis -l php example.ephp
  1737. &lt;&lt;?php ?&gt;?xml version="1.0"?&gt;
  1738. &lt;html&gt;
  1739. &lt;body&gt;
  1740. &lt;p&gt;Hello &lt;?php echo $user; ?&gt;!&lt;/p&gt;
  1741. &lt;table&gt;
  1742. &lt;tbody&gt;
  1743. &lt;?php $i = 0; ?&gt;
  1744. &lt;?php foreach ($list as $item) { ?&gt;
  1745. &lt;?php $i++; ?&gt;
  1746. &lt;tr bgcolor="&lt;?php echo $i % 2 == 0 ? '#FFCCCC' : '#CCCCFF'; ?&gt;"&gt;
  1747. &lt;td&gt;&lt;?php echo $i; ?&gt;&lt;/td&gt;
  1748. &lt;td&gt;&lt;?php echo htmlspecialchars($item); ?&gt;&lt;/td&gt;
  1749. &lt;/tr&gt;
  1750. &lt;?php } ?&gt;
  1751. &lt;/tbody&gt;
  1752. &lt;/table&gt;
  1753. &lt;/body&gt;
  1754. &lt;/html&gt;
  1755. </pre>
  1756. <br>
  1757. <a name="lang-c"></a>
  1758. <h3 class="section2">C</h3>
  1759. <a name="example.ec"></a>
  1760. <div class="program_caption">
  1761. example.ec</div>
  1762. <pre class="program"><strong>&lt;%
  1763. #include &lt;stdio.h&gt;
  1764. int main(int argc, char *argv[])
  1765. {
  1766. int i;
  1767. %&gt;</strong>
  1768. &lt;html&gt;
  1769. &lt;body&gt;
  1770. &lt;p&gt;Hello <strong>&lt;%= "%s", argv[0] %&gt;</strong>!&lt;/p&gt;
  1771. &lt;table&gt;
  1772. &lt;tbody&gt;
  1773. <strong>&lt;% for (i = 1; i &lt; argc; i++) { %&gt;</strong>
  1774. &lt;tr bgcolor="<strong>&lt;%= i % 2 == 0 ? "#FFCCCC" : "#CCCCFF" %&gt;</strong>"&gt;
  1775. &lt;td&gt;<strong>&lt;%= "%d", i %&gt;</strong>&lt;/td&gt;
  1776. &lt;td&gt;<strong>&lt;%= "%s", argv[i] %&gt;</strong>&lt;/td&gt;
  1777. &lt;/tr&gt;
  1778. <strong>&lt;% } %&gt;</strong>
  1779. &lt;/tbody&gt;
  1780. &lt;/table&gt;
  1781. &lt;/body&gt;
  1782. &lt;/html&gt;
  1783. <strong>&lt;%
  1784. return 0;
  1785. }
  1786. %&gt;</strong>
  1787. </pre>
  1788. <a name="example_c.result"></a>
  1789. <div class="terminal_caption">
  1790. compiled source code</div>
  1791. <pre class="terminal">$ erubis -l c example.ec
  1792. #line 1 "example.ec"
  1793. #include &lt;stdio.h&gt;
  1794. int main(int argc, char *argv[])
  1795. {
  1796. int i;
  1797. fputs("&lt;html&gt;\n"
  1798. " &lt;body&gt;\n"
  1799. " &lt;p&gt;Hello ", stdout); fprintf(stdout, "%s", argv[0]); fputs("!&lt;/p&gt;\n"
  1800. " &lt;table&gt;\n"
  1801. " &lt;tbody&gt;\n", stdout);
  1802. for (i = 1; i &lt; argc; i++) {
  1803. fputs(" &lt;tr bgcolor=\"", stdout); fprintf(stdout, i % 2 == 0 ? "#FFCCCC" : "#CCCCFF"); fputs("\"&gt;\n"
  1804. " &lt;td&gt;", stdout); fprintf(stdout, "%d", i); fputs("&lt;/td&gt;\n"
  1805. " &lt;td&gt;", stdout); fprintf(stdout, "%s", argv[i]); fputs("&lt;/td&gt;\n"
  1806. " &lt;/tr&gt;\n", stdout);
  1807. }
  1808. fputs(" &lt;/tbody&gt;\n"
  1809. " &lt;/table&gt;\n"
  1810. " &lt;/body&gt;\n"
  1811. "&lt;/html&gt;\n", stdout);
  1812. return 0;
  1813. }
  1814. </pre>
  1815. <br>
  1816. <a name="lang-java"></a>
  1817. <h3 class="section2">Java</h3>
  1818. <a name="Example.ejava"></a>
  1819. <div class="program_caption">
  1820. Example.ejava</div>
  1821. <pre class="program"><strong>&lt;%
  1822. import java.util.*;
  1823. public class Example {
  1824. private String user;
  1825. private String[] list;
  1826. public example(String user, String[] list) {
  1827. this.user = user;
  1828. this.list = list;
  1829. }
  1830. public String view() {
  1831. StringBuffer _buf = new StringBuffer();
  1832. %&gt;</strong>
  1833. &lt;html&gt;
  1834. &lt;body&gt;
  1835. &lt;p&gt;Hello <strong>&lt;%= user %&gt;</strong>!&lt;/p&gt;
  1836. &lt;table&gt;
  1837. &lt;tbody&gt;
  1838. <strong>&lt;% for (int i = 0; i &lt; list.length; i++) { %&gt;</strong>
  1839. &lt;tr bgcolor=<strong>"&lt;%= i % 2 == 0 ? "#FFCCCC" : "#CCCCFF" %&gt;"</strong>&gt;
  1840. &lt;td&gt;<strong>&lt;%= i + 1 %&gt;</strong>&lt;/td&gt;
  1841. &lt;td&gt;<strong>&lt;%== list[i] %&gt;</strong>&lt;/td&gt;
  1842. &lt;/tr&gt;
  1843. <strong>&lt;% } %&gt;</strong>
  1844. &lt;/tbody&gt;
  1845. &lt;/table&gt;
  1846. &lt;body&gt;
  1847. &lt;/html&gt;
  1848. <strong>&lt;%
  1849. return _buf.toString();
  1850. }
  1851. public static void main(String[] args) {
  1852. String[] list = { "&lt;aaa&gt;", "b&amp;b", "\"ccc\"" };
  1853. Example ex = Example.new("Erubis", list);
  1854. System.out.print(ex.view());
  1855. }
  1856. public static String escape(String s) {
  1857. StringBuffer sb = new StringBuffer();
  1858. for (int i = 0; i &lt; s.length(); i++) {
  1859. char ch = s.charAt(i);
  1860. switch (ch) {
  1861. case '&lt;': sb.append("&amp;lt;"); break;
  1862. case '&gt;': sb.append("&amp;gt;"); break;
  1863. case '&amp;': sb.append("&amp;amp;"); break;
  1864. case '"': sb.append("&amp;quot;"); break;
  1865. default: sb.append(ch);
  1866. }
  1867. }
  1868. return sb.toString();
  1869. }
  1870. }
  1871. %&gt;</strong>
  1872. </pre>
  1873. <a name="example_java.result"></a>
  1874. <div class="terminal_caption">
  1875. compiled source code</div>
  1876. <pre class="terminal">$ erubis -b -l java example.ejava
  1877. import java.util.*;
  1878. public class Example {
  1879. private String user;
  1880. private String[] list;
  1881. public example(String user, String[] list) {
  1882. this.user = user;
  1883. this.list = list;
  1884. }
  1885. public String view() {
  1886. StringBuffer _buf = new StringBuffer();
  1887. _buf.append("&lt;html&gt;\n"
  1888. + " &lt;body&gt;\n"
  1889. + " &lt;p&gt;Hello "); _buf.append(user); _buf.append("!&lt;/p&gt;\n"
  1890. + " &lt;table&gt;\n"
  1891. + " &lt;tbody&gt;\n");
  1892. for (int i = 0; i &lt; list.length; i++) {
  1893. _buf.append(" &lt;tr bgcolor=\""); _buf.append(i % 2 == 0 ? "#FFCCCC" : "#CCCCFF"); _buf.append("\"&gt;\n"
  1894. + " &lt;td&gt;"); _buf.append(i + 1); _buf.append("&lt;/td&gt;\n"
  1895. + " &lt;td&gt;"); _buf.append(escape(list[i])); _buf.append("&lt;/td&gt;\n"
  1896. + " &lt;/tr&gt;\n");
  1897. }
  1898. _buf.append(" &lt;/tbody&gt;\n"
  1899. + " &lt;/table&gt;\n"
  1900. + " &lt;body&gt;\n"
  1901. + "&lt;/html&gt;\n");
  1902. return _buf.toString();
  1903. }
  1904. public static void main(String[] args) {
  1905. String[] list = { "&lt;aaa&gt;", "b&amp;b", "\"ccc\"" };
  1906. Example ex = Example.new("Erubis", list);
  1907. System.out.print(ex.view());
  1908. }
  1909. public static String escape(String s) {
  1910. StringBuffer sb = new StringBuffer();
  1911. for (int i = 0; i &lt; s.length(); i++) {
  1912. char ch = s.charAt(i);
  1913. switch (ch) {
  1914. case '&lt;': sb.append("&amp;lt;"); break;
  1915. case '&gt;': sb.append("&amp;gt;"); break;
  1916. case '&amp;': sb.append("&amp;amp;"); break;
  1917. case '"': sb.append("&amp;quot;"); break;
  1918. default: sb.append(ch);
  1919. }
  1920. }
  1921. return sb.toString();
  1922. }
  1923. }
  1924. </pre>
  1925. <br>
  1926. <a name="lang-scheme"></a>
  1927. <h3 class="section2">Scheme</h3>
  1928. <a name="example.escheme"></a>
  1929. <div class="program_caption">
  1930. example.escheme</div>
  1931. <pre class="program">&lt;html&gt;
  1932. &lt;body&gt;
  1933. <strong>&lt;%
  1934. (let ((user "Erubis")
  1935. (items '("&lt;aaa&gt;" "b&amp;b" "\"ccc\""))
  1936. (i 0))
  1937. %&gt;</strong>
  1938. &lt;p&gt;Hello <strong>&lt;%= user %&gt;</strong>!&lt;/p&gt;
  1939. &lt;table&gt;
  1940. <strong>&lt;%
  1941. (for-each
  1942. (lambda (item)
  1943. (set! i (+ i 1))
  1944. %&gt;</strong>
  1945. &lt;tr bgcolor="<strong>&lt;%= (if (= (modulo i 2) 0) "#FFCCCC" "#CCCCFF") %&gt;</strong>"&gt;
  1946. &lt;td&gt;<strong>&lt;%= i %&gt;</strong>&lt;/td&gt;
  1947. &lt;td&gt;<strong>&lt;%= item %&gt;</strong>&lt;/td&gt;
  1948. &lt;/tr&gt;
  1949. <strong>&lt;%
  1950. ) ; lambda end
  1951. items) ; for-each end
  1952. %&gt;</strong>
  1953. &lt;/table&gt;
  1954. <strong>&lt;%
  1955. ) ; let end
  1956. %&gt;</strong>
  1957. &lt;/body&gt;
  1958. &lt;/html&gt;
  1959. </pre>
  1960. <a name="example_scheme.result"></a>
  1961. <div class="terminal_caption">
  1962. compiled source code</div>
  1963. <pre class="terminal">$ erubis -l scheme example.escheme
  1964. (let ((_buf '())) (define (_add x) (set! _buf (cons x _buf))) (_add "&lt;html&gt;
  1965. &lt;body&gt;\n")
  1966. (let ((user "Erubis")
  1967. (items '("&lt;aaa&gt;" "b&amp;b" "\"ccc\""))
  1968. (i 0))
  1969. (_add " &lt;p&gt;Hello ")(_add user)(_add "!&lt;/p&gt;
  1970. &lt;table&gt;\n")
  1971. (for-each
  1972. (lambda (item)
  1973. (set! i (+ i 1))
  1974. (_add " &lt;tr bgcolor=\"")(_add (if (= (modulo i 2) 0) "#FFCCCC" "#CCCCFF"))(_add "\"&gt;
  1975. &lt;td&gt;")(_add i)(_add "&lt;/td&gt;
  1976. &lt;td&gt;")(_add item)(_add "&lt;/td&gt;
  1977. &lt;/tr&gt;\n")
  1978. ) ; lambda end
  1979. items) ; for-each end
  1980. (_add " &lt;/table&gt;\n")
  1981. ) ; let end
  1982. (_add " &lt;/body&gt;
  1983. &lt;/html&gt;\n")
  1984. (reverse _buf))
  1985. </pre>
  1986. <a name="example_scheme_display.result"></a>
  1987. <div class="terminal_caption">
  1988. compiled source code (with <code>--func=display</code> property)</div>
  1989. <pre class="terminal">$ erubis -l scheme --func=display example.escheme
  1990. (display "&lt;html&gt;
  1991. &lt;body&gt;\n")
  1992. (let ((user "Erubis")
  1993. (items '("&lt;aaa&gt;" "b&amp;b" "\"ccc\""))
  1994. (i 0))
  1995. (display " &lt;p&gt;Hello ")(display user)(display "!&lt;/p&gt;
  1996. &lt;table&gt;\n")
  1997. (for-each
  1998. (lambda (item)
  1999. (set! i (+ i 1))
  2000. (display " &lt;tr bgcolor=\"")(display (if (= (modulo i 2) 0) "#FFCCCC" "#CCCCFF"))(display "\"&gt;
  2001. &lt;td&gt;")(display i)(display "&lt;/td&gt;
  2002. &lt;td&gt;")(display item)(display "&lt;/td&gt;
  2003. &lt;/tr&gt;\n")
  2004. ) ; lambda end
  2005. items) ; for-each end
  2006. (display " &lt;/table&gt;\n")
  2007. ) ; let end
  2008. (display " &lt;/body&gt;
  2009. &lt;/html&gt;\n")
  2010. </pre>
  2011. <br>
  2012. <a name="lang-perl"></a>
  2013. <h3 class="section2">Perl</h3>
  2014. <a name="example.eperl"></a>
  2015. <div class="program_caption">
  2016. example.eprl</div>
  2017. <pre class="program"><strong>&lt;%
  2018. my $user = 'Erubis';
  2019. my @list = ('&lt;aaa&gt;', 'b&amp;b', '"ccc"');
  2020. %&gt;</strong>
  2021. &lt;html&gt;
  2022. &lt;body&gt;
  2023. &lt;p&gt;Hello <strong>&lt;%= $user %&gt;</strong>!&lt;/p&gt;
  2024. &lt;table&gt;
  2025. <strong>&lt;% $i = 0; %&gt;</strong>
  2026. <strong>&lt;% for $item (@list) { %&gt;</strong>
  2027. &lt;tr bgcolor=<strong>&lt;%= ++$i % 2 == 0 ? '#FFCCCC' : '#CCCCFF' %&gt;</strong>"&gt;
  2028. &lt;td&gt;<strong>&lt;%= $i %&gt;</strong>&lt;/td&gt;
  2029. &lt;td&gt;<strong>&lt;%= $item %&gt;</strong>&lt;/td&gt;
  2030. &lt;/tr&gt;
  2031. <strong>&lt;% } %&gt;</strong>
  2032. &lt;/table&gt;
  2033. &lt;/body&gt;
  2034. &lt;/html&gt;
  2035. </pre>
  2036. <a name="example_perl.result"></a>
  2037. <div class="terminal_caption">
  2038. compiled source code</div>
  2039. <pre class="terminal">$ erubis -l perl example.eperl
  2040. use HTML::Entities;
  2041. my $user = 'Erubis';
  2042. my @list = ('&lt;aaa&gt;', 'b&amp;b', '"ccc"');
  2043. print('&lt;html&gt;
  2044. &lt;body&gt;
  2045. &lt;p&gt;Hello '); print($user); print('!&lt;/p&gt;
  2046. &lt;table&gt;
  2047. '); $i = 0;
  2048. for $item (@list) {
  2049. print(' &lt;tr bgcolor='); print(++$i % 2 == 0 ? '#FFCCCC' : '#CCCCFF'); print('"&gt;
  2050. &lt;td&gt;'); print($i); print('&lt;/td&gt;
  2051. &lt;td&gt;'); print($item); print('&lt;/td&gt;
  2052. &lt;/tr&gt;
  2053. '); }
  2054. print(' &lt;/table&gt;
  2055. &lt;/body&gt;
  2056. &lt;/html&gt;
  2057. ');
  2058. </pre>
  2059. <br>
  2060. <a name="lang-javascript"></a>
  2061. <h3 class="section2">JavaScript</h3>
  2062. <a name="example.ejs"></a>
  2063. <div class="program_caption">
  2064. example.ejs</div>
  2065. <pre class="program"><strong>&lt;%
  2066. var user = 'Erubis';
  2067. var list = ['&lt;aaa&gt;', 'b&amp;b', '"ccc"'];
  2068. %&gt;</strong>
  2069. &lt;html&gt;
  2070. &lt;body&gt;
  2071. &lt;p&gt;Hello <strong>&lt;%= user %&gt;</strong>!&lt;/p&gt;
  2072. &lt;table&gt;
  2073. &lt;tbody&gt;
  2074. <strong>&lt;% var i; %&gt;</strong>
  2075. <strong>&lt;% for (i = 0; i &lt; list.length; i++) { %&gt;</strong>
  2076. &lt;tr bgcolor="<strong>&lt;%= i % 2 == 0 ? '#FFCCCC' : '#CCCCFF' %&gt;</strong>"&gt;
  2077. &lt;td&gt;<strong>&lt;%= i + 1 %&gt;</strong>&lt;/td&gt;
  2078. &lt;td&gt;<strong>&lt;%= list[i] %&gt;</strong>&lt;/td&gt;
  2079. &lt;/tr&gt;
  2080. <strong>&lt;% } %&gt;</strong>
  2081. &lt;/tbody&gt;
  2082. &lt;/table&gt;
  2083. &lt;/body&gt;
  2084. &lt;/html&gt;
  2085. </pre>
  2086. <a name="example_js.result"></a>
  2087. <div class="terminal_caption">
  2088. compiled source code</div>
  2089. <pre class="terminal">$ erubis -l js example.ejs
  2090. var _buf = [];
  2091. var user = 'Erubis';
  2092. var list = ['&lt;aaa&gt;', 'b&amp;b', '"ccc"'];
  2093. _buf.push("&lt;html&gt;\n\
  2094. &lt;body&gt;\n\
  2095. &lt;p&gt;Hello "); _buf.push(user); _buf.push("!&lt;/p&gt;\n\
  2096. &lt;table&gt;\n\
  2097. &lt;tbody&gt;\n");
  2098. var i;
  2099. for (i = 0; i &lt; list.length; i++) {
  2100. _buf.push(" &lt;tr bgcolor=\""); _buf.push(i % 2 == 0 ? '#FFCCCC' : '#CCCCFF'); _buf.push("\"&gt;\n\
  2101. &lt;td&gt;"); _buf.push(i + 1); _buf.push("&lt;/td&gt;\n\
  2102. &lt;td&gt;"); _buf.push(list[i]); _buf.push("&lt;/td&gt;\n\
  2103. &lt;/tr&gt;\n");
  2104. }
  2105. _buf.push(" &lt;/tbody&gt;\n\
  2106. &lt;/table&gt;\n\
  2107. &lt;/body&gt;\n\
  2108. &lt;/html&gt;\n");
  2109. document.write(_buf.join(""));
  2110. </pre>
  2111. <p>If command-line option '<code>--docwrite=false</code>' is specified,
  2112. '<code>_buf.join("");</code>' is used instead of '<code>document.write(_buf.join(""));</code>'.
  2113. This is useful when passing converted source code to eval() function in JavaScript.
  2114. </p>
  2115. <p>You can pass <code>:docwrite=&gt;false</code> to Erubis::Ejavascript.new() in your Ruby script.
  2116. </p>
  2117. <pre class="program">s = File.read('example.jshtml')
  2118. engine = Erubis::Ejavascript.new(s, <code>:docwrite=&gt;false</code>)
  2119. </pre>
  2120. <p>If you want to specify any JavaScript code, use '--postamble=...'.
  2121. </p>
  2122. <p>Notice that default value of 'docwrite' property will be false in the future release.
  2123. </p>
  2124. <br>
  2125. <br>
  2126. <a name="rails"></a>
  2127. <h2 class="section1">Ruby on Rails Support</h2>
  2128. <p>Erubis supports Ruby on Rails.
  2129. This section describes how to use Erubis with Ruby on Rails.
  2130. </p>
  2131. <a name="rails-settings"></a>
  2132. <h3 class="section2">Settings</h3>
  2133. <p>Add the following code to your 'config/environment.rb' and restart web server.
  2134. This replaces ERB in Rails by Erubis entirely.
  2135. </p>
  2136. <div class="program_caption">
  2137. config/environment.rb</div>
  2138. <pre class="program">require 'erubis/helpers/rails_helper'
  2139. #Erubis::Helpers::RailsHelper.engine_class = Erubis::Eruby # or Erubis::FastEruby
  2140. #Erubis::Helpers::RailsHelper.init_properties = {}
  2141. #Erubis::Helpers::RailsHelper.show_src = nil
  2142. #Erubis::Helpers::RailsHelper.preprocessing = false
  2143. </pre>
  2144. <p>Options:
  2145. </p>
  2146. <dl class="dl2">
  2147. <dt class="dt2">
  2148. Erubis::Helpers::RailsHelper.engine_class (=Erubis::Eruby)</dt>
  2149. <dd class="dd2">
  2150. <p> Erubis engine class (default Erubis::Eruby).
  2151. </p>
  2152. </dd>
  2153. <dt class="dt2">
  2154. Erubis::Helpers::RailsHelper.init_properties (={})</dt>
  2155. <dd class="dd2">
  2156. <p> Optional arguments for Erubis::Eruby#initialize() method (default {}).
  2157. </p>
  2158. </dd>
  2159. <dt class="dt2">
  2160. Erubis::Helpers::RailsHelper.show_src (=nil)</dt>
  2161. <dd class="dd2">
  2162. <p> Whether to print converted Ruby code into log file.
  2163. If true, Erubis prints coverted code into log file.
  2164. If false, Erubis doesn't.
  2165. If nil, Erubis prints when ENV['RAILS_ENV'] == 'development'.
  2166. Default is nil.
  2167. </p>
  2168. </dd>
  2169. <dt class="dt2">
  2170. Erubis::Helpers::RailsHelper.preprocessing (=false)</dt>
  2171. <dd class="dd2">
  2172. <p> Enable preprocessing if true (default false).
  2173. </p>
  2174. </dd>
  2175. </dl>
  2176. <br>
  2177. <a name="rails-preprocessing"></a>
  2178. <h3 class="section2">Preprosessing</h3>
  2179. <p>Erubis supports preprocessing of template files.
  2180. Preprocessing make your Ruby on Rails application about 20-40 percent faster.
  2181. To enable preprocessing, set Erubis::Helpers::RailsHelper.preprocessing to true in your 'environment.rb' file.
  2182. </p>
  2183. <p>For example, assume the following template.
  2184. This is slow because link_to() method is called every time when template is rendered.
  2185. </p>
  2186. <pre class="program">&lt;%= link_to 'Create', :action=&gt;'create' %&gt;
  2187. </pre>
  2188. <p>The following is faster than the above, but not flexible because url is fixed.
  2189. </p>
  2190. <pre class="program">&lt;a href="/users/create"&gt;Create&lt;/a&gt;
  2191. </pre>
  2192. <p>Preprocessing solves this problem.
  2193. If you use '[%= %]' instead of '&lt;%= %&gt;', preprocessor evaluate it only once when template is loaded.
  2194. </p>
  2195. <pre class="program"><strong>[%= link_to 'Create', :action=&gt;'create'%]</strong>
  2196. </pre>
  2197. <p>The above is evaluated by preprocessor and replaced to the following code automatically.
  2198. </p>
  2199. <pre class="program">&lt;a href="/users/create"&gt;Create&lt;/a&gt;
  2200. </pre>
  2201. <p>Notice that this is done only once when template file is loaded.
  2202. It means that link_to() method is not called when template is rendered.
  2203. </p>
  2204. <p>If link_to() method have variable arguments, use <code>_?()</code> helper method.
  2205. </p>
  2206. <pre class="program">&lt;% for user in @users %&gt;
  2207. [%= link_to <strong>_?('user.name')</strong>, :action=&gt;'show', :id=&gt;<strong>_?('user.id')</strong> %]
  2208. &lt;% end %&gt;
  2209. </pre>
  2210. <p>The above is evaluated by preprocessor when template is loaded and expanded into the following code.
  2211. This will be much faster because link_to() method is not called when rendering.
  2212. </p>
  2213. <pre class="program">&lt;% for user in @users %&gt;
  2214. &lt;a href="/users/show/<strong>&lt;%=user.id%&gt;</strong>"&gt;<strong>&lt;%=user.name%&gt;</strong>&lt;/a&gt;
  2215. &lt;% end %&gt;
  2216. </pre>
  2217. <p>Preprocessing statement (<code>[% %]</code>) is also available as well as preprocessing expression (<code>[%= %]</code>).
  2218. </p>
  2219. <pre class="program">&lt;select name="state"&gt;
  2220. &lt;option value=""&gt;-&lt;/option&gt;
  2221. <strong>[% for code in states.keys.sort %]</strong>
  2222. &lt;option value="<strong>[%= code %]</strong>"&gt;<strong>[%= states[code] %]</strong>&lt;/option&gt;
  2223. <strong>[% end %]</strong>
  2224. &lt;/select&gt;
  2225. </pre>
  2226. <p>The above will be evaluated by preprocessor and expanded into the following when template is loaded.
  2227. In the result, rendering speed will be much faster because for-loop is not executed when rendering.
  2228. </p>
  2229. <pre class="program">&lt;select name="state"&gt;
  2230. &lt;option value=""&gt;-&lt;/option&gt;
  2231. &lt;option value="AK"&gt;Alaska&lt;/option&gt;
  2232. &lt;option value="AL"&gt;Alabama&lt;/option&gt;
  2233. &lt;option value="AR"&gt;Arkansas&lt;/option&gt;
  2234. &lt;option value="AS"&gt;American Samoa&lt;/option&gt;
  2235. &lt;option value="AZ"&gt;Arizona&lt;/option&gt;
  2236. &lt;option value="CA"&gt;California&lt;/option&gt;
  2237. &lt;option value="CO"&gt;Colorado&lt;/option&gt;
  2238. ....
  2239. &lt;/select&gt;
  2240. </pre>
  2241. <p>Notice that it is not recommended to use preprocessing with tag helpers,
  2242. because tag helpers generate different html code when form parameter has errors or not.
  2243. </p>
  2244. <p>Helper methods of Ruby on Rails are divided into two groups.
  2245. </p>
  2246. <ul type="disc">
  2247. <li>link_to() or _() (method of gettext package) are not need to call for every time
  2248. as template is rendered because it returns same value when same arguments are passed.
  2249. These methods can be got faster by preprocessing.
  2250. </li>
  2251. <li>Tag helper methods should be called for every time as template is rendered
  2252. because it may return differrent value even if the same arguments are passed.
  2253. Preprocessing is not available with these methods.
  2254. </li>
  2255. </ul>
  2256. <p>In Ruby on Rails 2.0, <code>_?('user_id')</code> is OK but <code>_?('user.id')</code> is NG
  2257. because the latter contains period ('.') character.
  2258. </p>
  2259. <pre class="program">&lt;!-- NG in Rails 2.0, because _?('') contains period --&gt;
  2260. [%= link_to 'Edit', edit_user_path(<strong>_?('@user.id')</strong>) %]
  2261. [%= link_to 'Show', <strong>@user</strong> %]
  2262. [%= link_to 'Delete', <strong>@user</strong>, :confirm=&gt;'OK?', :method=&gt;:delete %]
  2263. &lt;!-- OK in Rails 2.0 --&gt;
  2264. <strong>&lt;%= user_id = @user.id %&gt;</strong>
  2265. [%= link_to 'Edit', edit_user_path(<strong>_?('user_id')</strong>) %]
  2266. [%= link_to 'Show', <strong>:action=&gt;'show', :id=&gt;_?('user_id')</strong> %]
  2267. [%= link_to 'Delete', <strong>{:action=&gt;'destroy', :id=&gt;_?('user_id')}</strong>,
  2268. {:confirm=&gt;'OK?', :method=&gt;:delete} %]
  2269. </pre>
  2270. <br>
  2271. <a name="rails-formhelpers"></a>
  2272. <h3 class="section2">Form Helpers for Preprocessing</h3>
  2273. <p><strong>(Experimental)</strong>
  2274. </p>
  2275. <p>Erubis provides form helper methods for preprocessing.
  2276. These are defined in 'erubis/helpers/rails_form_helper.rb'.
  2277. If you want to use it, require it and include Erubis::Helpers::RailsFormHelper in 'app/helpers/applition_helper.rb'
  2278. </p>
  2279. <div class="program_caption">
  2280. app/helpers/xxx_helper.rb</div>
  2281. <pre class="program">require 'erubis/helpers/rails_form_helper'
  2282. module ApplicationHelper
  2283. include Erubis::Helpers::RailsFormHelper
  2284. end
  2285. </pre>
  2286. <p>Form helper methods defined in Erubis::Helpers::RailsFormHelper are named as 'pp_xxxx'
  2287. ('pp' represents preprocessing).
  2288. </p>
  2289. <p>Assume the following view template:
  2290. </p>
  2291. <div class="program_caption">
  2292. _form.rhtml</div>
  2293. <pre class="program"> &lt;p&gt;
  2294. Name: &lt;%= text_field :user, :name %&gt;
  2295. &lt;/p&gt;
  2296. &lt;p&gt;
  2297. Name: <strong>[%= pp_text_field :user, :name %]</strong>
  2298. &lt;/p&gt;
  2299. </pre>
  2300. <p>Erubis preprocessor converts it to the following eRuby string:
  2301. </p>
  2302. <div class="program_caption">
  2303. preprocessed</div>
  2304. <pre class="program"> &lt;p&gt;
  2305. Name: &lt;%= text_field :user, :name %&gt;
  2306. &lt;/p&gt;
  2307. &lt;p&gt;
  2308. Name: <strong>&lt;input id="stock_name" name="stock[name]" size="30" type="text" value="&lt;%=h @stock.name%&gt;" /&gt;</strong>
  2309. &lt;/p&gt;
  2310. </pre>
  2311. <p>Erubis converts it to the following Ruby code:
  2312. </p>
  2313. <div class="program_caption">
  2314. Ruby code</div>
  2315. <pre class="program"> _buf &lt;&lt; ' &lt;p&gt;
  2316. Name: '; _buf &lt;&lt; ( text_field :stock, :name ).to_s; _buf &lt;&lt; '
  2317. '; _buf &lt;&lt; ' &lt;/p&gt;
  2318. &lt;p&gt;
  2319. Name: &lt;input id="stock_name" name="stock[name]" size="30" type="text" value="'; _buf &lt;&lt; (h @stock.name).to_s; _buf &lt;&lt; '" /&gt;
  2320. &lt;/p&gt;
  2321. ';
  2322. </pre>
  2323. <p>The above Ruby code shows that text_field() is called everytime when rendering,
  2324. but pp_text_field() is called only once when template is loaded.
  2325. This means that pp_text_field() with preprocessing makes view layer very fast.
  2326. </p>
  2327. <p>Module Erubis::Helpers::RailsFormHelper defines the following form helper methods.
  2328. </p>
  2329. <ul type="disc">
  2330. <li>pp_render_partial(basename)
  2331. </li>
  2332. <li>pp_form_tag(url_for_options={}, options={}, *parameters_for_url, &amp;block)
  2333. </li>
  2334. <li>pp_text_field(object_name, method, options={})
  2335. </li>
  2336. <li>pp_password_field(object_name, method, options={})
  2337. </li>
  2338. <li>pp_hidden_field(object_name, method, options={})
  2339. </li>
  2340. <li>pp_file_field(object_name, method, options={})
  2341. </li>
  2342. <li>pp_text_area(object_name, method, options={})
  2343. </li>
  2344. <li>pp_check_box(object_name, method, options={}, checked_value="1", unchecked_value="0")
  2345. </li>
  2346. <li>pp_radio_button(object_name, method, tag_value, options={})
  2347. </li>
  2348. <li>pp_select(object, method, collection, options={}, html_options={})
  2349. </li>
  2350. <li>pp_collection_select(object, method, collection, value_method, text_method, options={}, html_options={})
  2351. </li>
  2352. <li>pp_country_select(object, method, priority_countries=nil, options={}, html_options={})
  2353. </li>
  2354. <li>pp_time_zone_select(object, method, priority_zones=nil, options={}, html_options={})
  2355. </li>
  2356. <li>pp_submit_tag(value="Save changes", options={})
  2357. </li>
  2358. <li>pp_image_submit_tag(source, options={})
  2359. </li>
  2360. </ul>
  2361. <p>Notice that pp_form_for() is not provided.
  2362. </p>
  2363. <p><span style="color:#FF0000">CAUTION:</span> These are experimental and may not work in Ruby on Rails 2.0.
  2364. </p>
  2365. <br>
  2366. <a name="rails-others"></a>
  2367. <h3 class="section2">Others</h3>
  2368. <ul type="disc">
  2369. <li>ActionView::Helpers::CaptureHelper#capture() and ActionView::Helpers::Texthelper#concat() are available.
  2370. </li>
  2371. </ul>
  2372. <ul type="disc">
  2373. <li>Form helper methods are not tested in Ruby on Rails 2.0.
  2374. </li>
  2375. </ul>
  2376. <ul type="disc">
  2377. <li>ERB::Util.h() is redefined if you require 'erubis/helpers/rails_helper.rb'.
  2378. Original definition of ERB::Util.h() is the following and it is slow
  2379. because it scans string four times.
  2380. <pre class="program"> def html_escape(s)
  2381. s.to_s.gsub(/&amp;/, "&amp;amp;").gsub(/\"/, "&amp;quot;").gsub(/&gt;/, "&amp;gt;").gsub(/&lt;/, "&amp;lt;")
  2382. end
  2383. alias h html_escape
  2384. </pre>
  2385. <p> New definition in 'erubis/helpers/rails_helper.rb' is faster than the above
  2386. because it scans string only once.
  2387. </p>
  2388. <pre class="program"> ESCAPE_TABLE = { '&amp;'=&gt;'&amp;amp;', '&lt;'=&gt;'&amp;lt;', '&gt;'=&gt;'&amp;gt;', '"'=&gt;'&amp;quot;', "'"=&gt;'&amp;#039;', }
  2389. def h(value)
  2390. value.to_s.gsub(/[&amp;&lt;&gt;"]/) { |s| ESCAPE_TABLE[s] }
  2391. end
  2392. </pre>
  2393. <p> Notice that the new definition may be slow if string contains
  2394. many '&lt; &gt; &amp; "' characters because block is call many time.
  2395. You should use ERB::Util.html_hscape() if string contains a lot of '&lt; &gt; &amp; "'
  2396. characters.
  2397. </p>
  2398. </li>
  2399. </ul>
  2400. <br>
  2401. <br>
  2402. <a name="topics"></a>
  2403. <h2 class="section1">Other Topics</h2>
  2404. <a name="'&lt;%= =%&gt;' and '&lt;%= -%&gt;'"></a>
  2405. <h3 class="section2">'&lt;%= =%&gt;' and '&lt;%= -%&gt;'</h3>
  2406. <p>Since 2.6.0, '&lt;%= -%&gt;' remove tail spaces and newline.
  2407. This is for compatibiliy with ERB when trim mode is '-'.
  2408. '&lt;%= =%&gt;' also removes tail spaces and newlines, and this is
  2409. Erubis-original enhancement (cooler than '&lt;%= -%&gt;', isn't it?).
  2410. </p>
  2411. <a name="tailnewline.rhtml.comment_filter"></a>
  2412. <div class="program_caption">
  2413. tailnewline.rhtml</div>
  2414. <pre class="program">&lt;div&gt;
  2415. &lt;%= @var -%&gt; # or &lt;%= @var =%&gt;
  2416. &lt;/div&gt;
  2417. </pre>
  2418. <div class="terminal_caption">
  2419. result (version 2.5.0):</div>
  2420. <pre class="terminal">$ erubis -c '{var: "AAA\n"}' tailnewline.rhtml
  2421. &lt;div&gt;
  2422. AAA
  2423. &lt;/div&gt;
  2424. </pre>
  2425. <a name="tail_260.result"></a>
  2426. <div class="terminal_caption">
  2427. result (version 2.6.0):</div>
  2428. <pre class="terminal">$ erubis -c '{var: "AAA\n"}' tailnewline.rhtml
  2429. &lt;div&gt;
  2430. AAA
  2431. &lt;/div&gt;
  2432. </pre>
  2433. <br>
  2434. <a name="'&lt;%% %&gt;' and '&lt;%%= %&gt;'"></a>
  2435. <h3 class="section2">'&lt;%% %&gt;' and '&lt;%%= %&gt;'</h3>
  2436. <p>Since 2.6.0, '&lt;%% %&gt;' and '&lt;%%= %&gt;' are converted into '&lt;% %&gt;' and '&lt;%= %&gt;' respectively.
  2437. This is for compatibility with ERB.
  2438. </p>
  2439. <div class="program_caption">
  2440. doublepercent.rhtml:</div>
  2441. <pre class="program">&lt;ul&gt;
  2442. &lt;%% for item in @list %&gt;
  2443. &lt;li&gt;&lt;%%= item %&gt;&lt;/li&gt;
  2444. &lt;%% end %&gt;
  2445. &lt;/ul&gt;
  2446. </pre>
  2447. <div class="terminal_caption">
  2448. result:</div>
  2449. <pre class="terminal">$ erubis doublepercent.rhtml
  2450. &lt;ul&gt;
  2451. &lt;% for item in @list %&gt;
  2452. &lt;li&gt;&lt;%= item %&gt;&lt;/li&gt;
  2453. &lt;% end %&gt;
  2454. &lt;/ul&gt;
  2455. </pre>
  2456. <br>
  2457. <a name="topics-context-vs-binding"></a>
  2458. <h3 class="section2">evaluate(context) v.s. result(binding)</h3>
  2459. <p>It is recommended to use 'Erubis::Eruby#evaluate(context)' instead of 'Erubis::Eruby#result(binding)' because Ruby's Binding object has some problems.
  2460. </p>
  2461. <ul type="disc">
  2462. <li>It is not able to specify variables to use.
  2463. Using binding() method, all of local variables are passed to templates.
  2464. </li>
  2465. <li>Changing local variables in templates may affect to varialbes in main program.
  2466. If you assign '10' to local variable 'x' in templates, it may change variable 'x' in main program unintendedly.
  2467. </li>
  2468. </ul>
  2469. <p>The following example shows that assignment of some values into variable 'x' in templates affect to local variable 'x' in main program unintendedly.
  2470. </p>
  2471. <a name="template1.rhtml"></a>
  2472. <div class="program_caption">
  2473. template1.rhtml (intended to be passed 'items' from main program)</div>
  2474. <pre class="program">&lt;% for <strong>x</strong> in <strong>items</strong> %&gt;
  2475. item = &lt;%= x %&gt;
  2476. &lt;% end %&gt;
  2477. ** debug: local variables=&lt;%= local_variables().inspect() %&gt;
  2478. </pre>
  2479. <a name="main_program1.rb"></a>
  2480. <div class="program_caption">
  2481. main_program1.rb (intended to pass 'items' to template)</div>
  2482. <pre class="program">require 'erubis'
  2483. eruby = Erubis::Eruby.new(File.read('template1.rhtml'))
  2484. items = ['foo', 'bar', 'baz']
  2485. x = 1
  2486. ## local variable 'x' and 'eruby' are passed to template as well as 'items'!
  2487. print <strong>eruby.result(binding())</strong>
  2488. ## local variable 'x' is changed unintendedly because it is changed in template!
  2489. puts "** debug: x=#{x.inspect}" #=&gt; "baz"
  2490. </pre>
  2491. <a name="main_program1.result"></a>
  2492. <div class="terminal_caption">
  2493. Result:</div>
  2494. <pre class="terminal">$ ruby main_program1.rb
  2495. item = foo
  2496. item = bar
  2497. item = baz
  2498. ** debug: local variables=["eruby", "items", "x", "_buf"]
  2499. ** debug: x="baz"
  2500. </pre>
  2501. <p>This problem is caused because Ruby's Binding class is poor to use in template engine.
  2502. Binding class should support the following features.
  2503. </p>
  2504. <pre class="program">b = Binding.new # create empty Binding object
  2505. b['x'] = 1 # set local variables using binding object
  2506. </pre>
  2507. <p>But the above features are not implemented in Ruby.
  2508. </p>
  2509. <p>A pragmatic solution is to use 'Erubis::Eruby#evaluate(context)' instead of 'Erubis::Eruby#result(binding)'.
  2510. 'evaluate(context)' uses Erubis::Context object and instance variables instead of Binding object and local variables.
  2511. </p>
  2512. <a name="template2.rhtml"></a>
  2513. <div class="program_caption">
  2514. template2.rhtml (intended to be passed '@items' from main program)</div>
  2515. <pre class="program">&lt;% for <strong>x</strong> in <strong>@items</strong> %&gt;
  2516. item = &lt;%= x %&gt;
  2517. &lt;% end %&gt;
  2518. ** debug: local variables=&lt;%= local_variables().inspect() %&gt;
  2519. </pre>
  2520. <a name="main_program2.rb"></a>
  2521. <div class="program_caption">
  2522. main_program2.rb (intended to pass '@items' to template)</div>
  2523. <pre class="program">require 'erubis'
  2524. eruby = Erubis::Eruby.new(File.read('template2.rhtml'))
  2525. items = ['foo', 'bar', 'baz']
  2526. x = 1
  2527. ## only 'items' are passed to template
  2528. print <strong>eruby.evaluate(:items=&gt;items)</strong>
  2529. ## local variable 'x' is not changed!
  2530. puts "** debug: x=#{x.inspect}" #=&gt; 1
  2531. </pre>
  2532. <a name="main_program2.result"></a>
  2533. <div class="terminal_caption">
  2534. Result:</div>
  2535. <pre class="terminal">$ ruby main_program2.rb
  2536. item = foo
  2537. item = bar
  2538. item = baz
  2539. ** debug: local variables=["_context", "x", "_buf"]
  2540. ** debug: x=1
  2541. </pre>
  2542. <br>
  2543. <a name="topics-fasteruby"></a>
  2544. <h3 class="section2">Class Erubis::FastEruby</h3>
  2545. <p>[experimental]
  2546. </p>
  2547. <p>Erubis provides Erubis::FastEruby class which includes <a href="#interpolation-enhancer">InterpolationEnhancer</a> and <a href="#topics-benchmark">works faster than Erubis::Eruby class</a>.
  2548. If you desire more speed, try Erubis::FastEruby class.
  2549. </p>
  2550. <a name="fasteruby.rhtml"></a>
  2551. <div class="program_caption">
  2552. File 'fasteruby.rhtml':</div>
  2553. <pre class="program">&lt;html&gt;
  2554. &lt;body&gt;
  2555. &lt;h1&gt;&lt;%== @title %&gt;&lt;/h1&gt;
  2556. &lt;table&gt;
  2557. &lt;% i = 0 %&gt;
  2558. &lt;% for item in @list %&gt;
  2559. &lt;% i += 1 %&gt;
  2560. &lt;tr&gt;
  2561. &lt;td&gt;&lt;%= i %&gt;&lt;/td&gt;
  2562. &lt;td&gt;&lt;%== item %&gt;&lt;/td&gt;
  2563. &lt;/tr&gt;
  2564. &lt;% end %&gt;
  2565. &lt;/table&gt;
  2566. &lt;/body&gt;
  2567. &lt;/html&gt;
  2568. </pre>
  2569. <a name="fasteruby.rb"></a>
  2570. <div class="program_caption">
  2571. File 'fasteruby.rb':</div>
  2572. <pre class="program">require 'erubis'
  2573. input = File.read('fasteruby.rhtml')
  2574. eruby = <strong>Erubis::FastEruby.new(input)</strong> # create Eruby object
  2575. puts "---------- script source ---"
  2576. puts eruby.src
  2577. puts "---------- result ----------"
  2578. context = { :title=&gt;'Example', :list=&gt;['aaa', 'bbb', 'ccc'] }
  2579. output = eruby.evaluate(context)
  2580. print output
  2581. </pre>
  2582. <a name="fasteruby.result"></a>
  2583. <div class="terminal_caption">
  2584. output</div>
  2585. <pre class="terminal">$ ruby fasteruby.rb
  2586. ---------- script source ---
  2587. _buf = ''; _buf &lt;&lt; <strong>%Q`</strong>&lt;html&gt;
  2588. &lt;body&gt;
  2589. &lt;h1&gt;<strong>#{Erubis::XmlHelper.escape_xml( @title )}</strong>&lt;/h1&gt;
  2590. &lt;table&gt;\n<strong>`</strong>
  2591. i = 0
  2592. for item in @list
  2593. i += 1
  2594. _buf &lt;&lt; <strong>%Q`</strong> &lt;tr&gt;
  2595. &lt;td&gt;<strong>#{ i }</strong>&lt;/td&gt;
  2596. &lt;td&gt;<strong>#{Erubis::XmlHelper.escape_xml( item )}</strong>&lt;/td&gt;
  2597. &lt;/tr&gt;\n<strong>`</strong>
  2598. end
  2599. _buf &lt;&lt; <strong>%Q`</strong> &lt;/table&gt;
  2600. &lt;/body&gt;
  2601. &lt;/html&gt;\n<strong>`</strong>
  2602. _buf.to_s
  2603. ---------- result ----------
  2604. &lt;html&gt;
  2605. &lt;body&gt;
  2606. &lt;h1&gt;Example&lt;/h1&gt;
  2607. &lt;table&gt;
  2608. &lt;tr&gt;
  2609. &lt;td&gt;1&lt;/td&gt;
  2610. &lt;td&gt;aaa&lt;/td&gt;
  2611. &lt;/tr&gt;
  2612. &lt;tr&gt;
  2613. &lt;td&gt;2&lt;/td&gt;
  2614. &lt;td&gt;bbb&lt;/td&gt;
  2615. &lt;/tr&gt;
  2616. &lt;tr&gt;
  2617. &lt;td&gt;3&lt;/td&gt;
  2618. &lt;td&gt;ccc&lt;/td&gt;
  2619. &lt;/tr&gt;
  2620. &lt;/table&gt;
  2621. &lt;/body&gt;
  2622. &lt;/html&gt;
  2623. </pre>
  2624. <br>
  2625. <a name="topics-syntax"></a>
  2626. <h3 class="section2">Syntax Checking</h3>
  2627. <p>Command-line option '-z' checks syntax. It is similar to 'erubis -x file.rhtml | ruby -wc', but it can take several file names.
  2628. </p>
  2629. <div class="terminal_caption">
  2630. example of command-line option '-z'</div>
  2631. <pre class="terminal">$ erubis <strong>-z</strong> app/views/*/*.rhtml
  2632. Syntax OK
  2633. </pre>
  2634. <br>
  2635. <a name="topics-caching"></a>
  2636. <h3 class="section2">File Caching</h3>
  2637. <p>Erubis::Eruby.load_file(filename) convert file into Ruby script and return Eruby object.
  2638. In addition, it caches converted Ruby script into cache file (filename + '.cache') if cache file is old or not exist.
  2639. If cache file exists and is newer than eruby file, Erubis::Eruby.load_file() loads cache file.
  2640. </p>
  2641. <div class="program_caption">
  2642. example of Erubis::Eruby.load_file()</div>
  2643. <pre class="program">require 'erubis'
  2644. filename = 'example.rhtml'
  2645. eruby = <strong>Erubis::Eruby.load_file(filename)</strong>
  2646. cachename = filename + '.cache'
  2647. if test(?f, cachename)
  2648. puts "*** cache file '#{cachename}' created."
  2649. end
  2650. </pre>
  2651. <p>Since 2.6.0, it is able to specify cache filename.
  2652. </p>
  2653. <div class="program_caption">
  2654. specify cache filename.</div>
  2655. <pre class="program">filename = 'example.rhtml'
  2656. eruby = Erubis::Eruby.load_file(filename, :cachename=&gt;filename+'.cache')
  2657. </pre>
  2658. <p>Caching makes Erubis about 40-50 percent faster than no-caching.
  2659. See <a href="#topics-benchmark">benchmark</a> for details.
  2660. </p>
  2661. <br>
  2662. <a name="topics-tinyeruby"></a>
  2663. <h3 class="section2">Erubis::TinyEruby class</h3>
  2664. <p>Erubis::TinyEruby class in 'erubis/tiny.rb' is the smallest implementation of eRuby.
  2665. If you don't need any enhancements of Erubis and only require simple eRuby implementation,
  2666. try Erubis::TinyEruby class.
  2667. </p>
  2668. <br>
  2669. <a name="topics-php"></a>
  2670. <h3 class="section2">NoTextEnhancer and NoCodeEnhancer in PHP</h3>
  2671. <p>NoTextEnhancer and NoCodEnahncer are quite useful not only for eRuby but also for PHP.
  2672. The former "drops" HTML text and show up embedded Ruby/PHP code
  2673. and the latter drops embedded Ruby/PHP code and leave HTML text.
  2674. </p>
  2675. <p>For example, see the following PHP script.
  2676. </p>
  2677. <a name="notext-example.php"></a>
  2678. <div class="program_caption">
  2679. notext-example.php</div>
  2680. <pre class="program">&lt;html&gt;
  2681. &lt;body&gt;
  2682. &lt;h3&gt;List&lt;/h3&gt;
  2683. &lt;?php if (!$list || count($list) == 0) { ?&gt;
  2684. &lt;p&gt;not found.&lt;/p&gt;
  2685. &lt;?php } else { ?&gt;
  2686. &lt;table&gt;
  2687. &lt;tbody&gt;
  2688. &lt;?php $i = 0; ?&gt;
  2689. &lt;?php foreach ($list as $item) { ?&gt;
  2690. &lt;tr bgcolor="&lt;?php echo ++$i % 2 == 1 ? '#FFCCCC' : '#CCCCFF'; ?&gt;"&gt;
  2691. &lt;td&gt;&lt;?php echo $item; ?&gt;&lt;/td&gt;
  2692. &lt;/tr&gt;
  2693. &lt;?php } ?&gt;
  2694. &lt;/tbody&gt;
  2695. &lt;/table&gt;
  2696. &lt;?php } ?&gt;
  2697. &lt;/body&gt;
  2698. &lt;/html&gt;
  2699. </pre>
  2700. <p>This is complex because PHP code and HTML document are mixed.
  2701. NoTextEnhancer can separate PHP code from HTML document.
  2702. </p>
  2703. <a name="notext-php.result"></a>
  2704. <div class="terminal_caption">
  2705. example of using NoTextEnhancer with PHP file</div>
  2706. <pre class="terminal">$ erubis -l php --pi=php -N -E NoText --trim=false notext-example.php
  2707. 1:
  2708. 2:
  2709. 3:
  2710. 4: &lt;?php if (!$list || count($list) == 0) { ?&gt;
  2711. 5:
  2712. 6: &lt;?php } else { ?&gt;
  2713. 7:
  2714. 8:
  2715. 9: &lt;?php $i = 0; ?&gt;
  2716. 10: &lt;?php foreach ($list as $item) { ?&gt;
  2717. 11: &lt;?php echo ++$i % 2 == 1 ? '#FFCCCC' : '#CCCCFF'; ?&gt;
  2718. 12: &lt;?php echo $item; ?&gt;
  2719. 13:
  2720. 14: &lt;?php } ?&gt;
  2721. 15:
  2722. 16:
  2723. 17: &lt;?php } ?&gt;
  2724. 18:
  2725. 19:
  2726. </pre>
  2727. <p>In the same way, NoCodeEnhancer can extract HTML tags.
  2728. </p>
  2729. <a name="nocode-php.result"></a>
  2730. <div class="terminal_caption">
  2731. example of using NoCodeEnhancer with PHP file</div>
  2732. <pre class="terminal">$ erubis -l php --pi=php -N -E NoCode --trim=false notext-example.php
  2733. 1: &lt;html&gt;
  2734. 2: &lt;body&gt;
  2735. 3: &lt;h3&gt;List&lt;/h3&gt;
  2736. 4:
  2737. 5: &lt;p&gt;not found.&lt;/p&gt;
  2738. 6:
  2739. 7: &lt;table&gt;
  2740. 8: &lt;tbody&gt;
  2741. 9:
  2742. 10:
  2743. 11: &lt;tr bgcolor=""&gt;
  2744. 12: &lt;td&gt;&lt;/td&gt;
  2745. 13: &lt;/tr&gt;
  2746. 14:
  2747. 15: &lt;/tbody&gt;
  2748. 16: &lt;/table&gt;
  2749. 17:
  2750. 18: &lt;/body&gt;
  2751. 19: &lt;/html&gt;
  2752. </pre>
  2753. <br>
  2754. <a name="topcs-modruby"></a>
  2755. <h3 class="section2">Helper Class for mod_ruby</h3>
  2756. <p>Thanks Andrew R Jackson, he developed 'erubis-run.rb' which enables you to use Erubis with mod_ruby.
  2757. </p>
  2758. <ol type="1">
  2759. <li>Copy 'erubis-2.6.6/contrib/erubis-run.rb' to the 'RUBYLIBDIR/apache' directory (for example '/usr/local/lib/ruby/1.8/apache') which contains 'ruby-run.rb', 'eruby-run.rb', and so on.
  2760. <pre class="terminal">$ cd erubis-2.6.6/
  2761. $ sudo copy contrib/erubis-run.rb /usr/local/lib/ruby/1.8/apache/
  2762. </pre>
  2763. </li>
  2764. <li>Add the following example to your 'httpd.conf' (for example '/usr/local/apache2/conf/httpd.conf')
  2765. <pre class="program">LoadModule ruby_module modules/mod_ruby.so
  2766. &lt;IfModule mod_ruby.c&gt;
  2767. RubyRequire apache/ruby-run
  2768. RubyRequire apache/eruby-run
  2769. RubyRequire apache/erubis-run
  2770. &lt;Location /erubis&gt;
  2771. SetHandler ruby-object
  2772. RubyHandler Apache::ErubisRun.instance
  2773. &lt;/Location&gt;
  2774. &lt;Files *.rhtml&gt;
  2775. SetHandler ruby-object
  2776. RubyHandler Apache::ErubisRun.instance
  2777. &lt;/Files&gt;
  2778. &lt;/IfModule&gt;
  2779. </pre>
  2780. </li>
  2781. <li>Restart Apache web server.
  2782. <pre class="terminal">$ sudo /usr/local/apache2/bin/apachectl stop
  2783. $ sudo /usr/local/apache2/bin/apachectl start
  2784. </pre>
  2785. </li>
  2786. <li>Create *.rhtml file, for example:
  2787. <pre class="program">&lt;html&gt;
  2788. &lt;body&gt;
  2789. Now is &lt;%= Time.now %&gt;
  2790. Erubis version is &lt;%= Erubis::VERSION %&gt;
  2791. &lt;/body&gt;
  2792. &lt;/html&gt;
  2793. </pre>
  2794. </li>
  2795. <li>Change mode of your directory to be writable by web server process.
  2796. <pre class="terminal">$ cd /usr/local/apache2/htdocs/erubis
  2797. $ sudo chgrp daemon .
  2798. $ sudo chmod 775 .
  2799. </pre>
  2800. </li>
  2801. <li>Access the *.rhtml file and you'll get the web page.
  2802. </li>
  2803. </ol>
  2804. <p>You must set your directories to be writable by web server process, because
  2805. Apache::ErubisRun calls Erubis::Eruby.load_file() internally which creates cache files
  2806. in the same directory in which '*.rhtml' file exists.
  2807. </p>
  2808. <br>
  2809. <a name="topics-defmethod"></a>
  2810. <h3 class="section2">Define method</h3>
  2811. <p>Erubis::Eruby#def_method() defines instance method or singleton method.
  2812. </p>
  2813. <a name="def_method.rb"></a>
  2814. <pre class="program">require 'erubis'
  2815. s = "hello &lt;%= name %&gt;"
  2816. eruby = Erubis::Eruby.new(s)
  2817. filename = 'hello.rhtml'
  2818. ## define instance method to Dummy class (or module)
  2819. class Dummy; end
  2820. <strong>eruby.def_method(Dummy, 'render(name)', filename)</strong> # filename is optional
  2821. p Dummy.new.render('world') #=&gt; "hello world"
  2822. ## define singleton method to dummy object
  2823. obj = Object.new
  2824. <strong>eruby.def_method(obj, 'render(name)', filename)</strong> # filename is optional
  2825. p obj.render('world') #=&gt; "hello world"
  2826. </pre>
  2827. <br>
  2828. <a name="topics-benchmark"></a>
  2829. <h3 class="section2">Benchmark</h3>
  2830. <p>A benchmark script is included in Erubis package at 'erubis-2.6.6/benchark/' directory.
  2831. Here is an example result of benchmark.
  2832. </p>
  2833. <div class="terminal_caption">
  2834. MacOS X 10.4 Tiger, Intel CoreDuo 1.83GHz, Ruby1.8.6, eruby1.0.5, gcc4.0.1</div>
  2835. <pre class="terminal">$ cd erubis-2.6.6/benchmark/
  2836. $ ruby bench.rb -n 10000 -m execute
  2837. *** ntimes=10000, testmode=execute
  2838. user system total real
  2839. eruby 12.720000 0.240000 <strong>12.960000</strong> ( 12.971888)
  2840. ERB 36.760000 0.350000 <strong>37.110000</strong> ( 37.112019)
  2841. ERB(cached) 11.990000 0.440000 <strong>12.430000</strong> ( 12.430375)
  2842. Erubis::Eruby 10.840000 0.300000 <strong>11.140000</strong> ( 11.144426)
  2843. Erubis::Eruby(cached) 7.540000 0.410000 <strong>7.950000</strong> ( 7.969305)
  2844. Erubis::FastEruby 10.440000 0.300000 <strong>10.740000</strong> ( 10.737808)
  2845. Erubis::FastEruby(cached) 6.940000 0.410000 <strong>7.350000</strong> ( 7.353666)
  2846. Erubis::TinyEruby 9.550000 0.290000 9.840000 ( 9.851729)
  2847. Erubis::ArrayBufferEruby 11.010000 0.300000 11.310000 ( 11.314339)
  2848. Erubis::PrintOutEruby 11.640000 0.290000 11.930000 ( 11.942141)
  2849. Erubis::StdoutEruby 11.590000 0.300000 11.890000 ( 11.886512)
  2850. </pre>
  2851. <p>This shows that...
  2852. </p>
  2853. <ul type="disc">
  2854. <li>Erubis::Eruby runs more than 10 percent faster than eruby.
  2855. </li>
  2856. <li>Erubis::Eruby runs about 3 times faster than ERB.
  2857. </li>
  2858. <li>Caching (by Erubis::Eruby.load_file()) makes Erubis about 40-50 percent faster.
  2859. </li>
  2860. <li>Erubis::FastEruby is a litte faster than Erubis::Eruby.
  2861. </li>
  2862. <li>Array buffer (ArrayBufferEnhancer) is a little slower than string buffer (StringBufferEnhancer which Erubis::Eruby includes)
  2863. </li>
  2864. <li>$stdout and print() make Erubis a little slower.
  2865. </li>
  2866. <li>Erubis::TinyEruby (at 'erubis/tiny.rb') is the fastest in all eRuby implementations when no caching.
  2867. </li>
  2868. </ul>
  2869. <p>Escaping HTML characters (such as '&lt; &gt; &amp; "') makes Erubis more faster than eruby and ERB,
  2870. because Erubis::XmlHelper#escape_xml() works faster than CGI.escapeHTML() and ERB::Util#h().
  2871. The following shows that Erubis runs more than 40 percent (when no-cached) or 90 percent (when cached) faster than eruby if HTML characters are escaped.
  2872. </p>
  2873. <div class="terminal_caption">
  2874. When escaping HTML characters with option '-e'</div>
  2875. <pre class="terminal">$ ruby bench.rb -n 10000 -m execute -ep
  2876. *** ntimes=10000, testmode=execute
  2877. user system total real
  2878. eruby 21.700000 0.290000 <strong>21.990000</strong> ( 22.050687)
  2879. ERB 45.140000 0.390000 <strong>45.530000</strong> ( 45.536976)
  2880. ERB(cached) 20.340000 0.470000 <strong>20.810000</strong> ( 20.822653)
  2881. Erubis::Eruby 14.830000 0.310000 <strong>15.140000</strong> ( 15.147930)
  2882. Erubis::Eruby(cached) 11.090000 0.420000 <strong>11.510000</strong> ( 11.514954)
  2883. Erubis::FastEruby 14.850000 0.310000 <strong>15.160000</strong> ( 15.172499)
  2884. Erubis::FastEruby(cached) 10.970000 0.430000 <strong>11.400000</strong> ( 11.399605)
  2885. Erubis::ArrayBufferEruby 14.970000 0.300000 15.270000 ( 15.281061)
  2886. Erubis::PrintOutEruby 15.780000 0.300000 16.080000 ( 16.088289)
  2887. Erubis::StdoutEruby 15.840000 0.310000 16.150000 ( 16.235338)
  2888. </pre>
  2889. <br>
  2890. <br>
  2891. <a name="command"></a>
  2892. <h2 class="section1">Command Reference</h2>
  2893. <a name="command-usage"></a>
  2894. <h3 class="section2">Usage</h3>
  2895. <p>erubis [..options..] [<em>file</em> ...]
  2896. </p>
  2897. <br>
  2898. <a name="command-options"></a>
  2899. <h3 class="section2">Options</h3>
  2900. <dl class="dl3" compact>
  2901. <dt class="dt3"><b>
  2902. -h, --help </b></dt>
  2903. <dd class="dd3">
  2904. Help.
  2905. </dd>
  2906. <dt class="dt3"><b>
  2907. -v </b></dt>
  2908. <dd class="dd3">
  2909. Release version.
  2910. </dd>
  2911. <dt class="dt3"><b>
  2912. -x </b></dt>
  2913. <dd class="dd3">
  2914. Show compiled source.
  2915. </dd>
  2916. <dt class="dt3"><b>
  2917. -X </b></dt>
  2918. <dd class="dd3">
  2919. Show compiled source but only Ruby code.
  2920. This is equivarent to '-E NoText'.
  2921. </dd>
  2922. <dt class="dt3"><b>
  2923. -N </b></dt>
  2924. <dd class="dd3">
  2925. Numbering: add line numbers. (for '-x/-X')
  2926. </dd>
  2927. <dt class="dt3"><b>
  2928. -U </b></dt>
  2929. <dd class="dd3">
  2930. Unique mode: zip empty lines into a line. (for '-x/-X')
  2931. </dd>
  2932. <dt class="dt3"><b>
  2933. -C </b></dt>
  2934. <dd class="dd3">
  2935. Compact: remove empty lines. (for '-x/-X')
  2936. </dd>
  2937. <dt class="dt3"><b>
  2938. -b </b></dt>
  2939. <dd class="dd3">
  2940. Body only: no preamble nor postamble. (for '-x/-X')
  2941. This is equivarent to '--preamble=false --postamble=false'.
  2942. </dd>
  2943. <dt class="dt3"><b>
  2944. -z </b></dt>
  2945. <dd class="dd3">
  2946. Syntax checking.
  2947. </dd>
  2948. <dt class="dt3"><b>
  2949. -e </b></dt>
  2950. <dd class="dd3">
  2951. Escape. This is equivarent to '-E Escape'.
  2952. </dd>
  2953. <dt class="dt3"><b>
  2954. -p pattern </b></dt>
  2955. <dd class="dd3">
  2956. Embedded pattern (default '&lt;% %&gt;').
  2957. This is equivarent to '--pattern=<em>pattern</em>'.
  2958. </dd>
  2959. <dt class="dt3"><b>
  2960. -l lang </b></dt>
  2961. <dd class="dd3">
  2962. Language name.
  2963. This option makes erubis command to compile script but no execute.
  2964. </dd>
  2965. <dt class="dt3"><b>
  2966. -E enhacers </b></dt>
  2967. <dd class="dd3">
  2968. Enhancer name (Escape, PercentLine, ...).
  2969. It is able to specify several enhancer name separating with ','
  2970. (ex. -f Escape,PercentLine,HeaderFooter).
  2971. </dd>
  2972. <dt class="dt3"><b>
  2973. -I path </b></dt>
  2974. <dd class="dd3">
  2975. Require library path ($:).
  2976. It is able to specify several paths separating with ','
  2977. (ex. -f path1,path2,path3).
  2978. </dd>
  2979. <dt class="dt3"><b>
  2980. -K kanji </b></dt>
  2981. <dd class="dd3">
  2982. Kanji code (euc, sjis, utf8, or none) (default none).
  2983. </dd>
  2984. <dt class="dt3"><b>
  2985. -f datafile </b></dt>
  2986. <dd class="dd3">
  2987. Context data file in YAML format ('*.yaml', '*.yml') or
  2988. Ruby script ('*.rb').
  2989. It is able to specify several filenames separating with ','
  2990. (ex. -f file1,file2,file3).
  2991. </dd>
  2992. <dt class="dt3"><b>
  2993. -c context </b></dt>
  2994. <dd class="dd3">
  2995. Context data string in YAML inline style or Ruby code.
  2996. </dd>
  2997. <dt class="dt3"><b>
  2998. -T </b></dt>
  2999. <dd class="dd3">
  3000. Don't expand tab characters in YAML file.
  3001. </dd>
  3002. <dt class="dt3"><b>
  3003. -S </b></dt>
  3004. <dd class="dd3">
  3005. Convert mapping key from string to symbol in YAML file.
  3006. </dd>
  3007. <dt class="dt3"><b>
  3008. -B </b></dt>
  3009. <dd class="dd3">
  3010. invoke Eruby#result() instead of Eruby#evaluate()
  3011. </dd>
  3012. <dt class="dt3"><b>
  3013. --pi[=name] </b></dt>
  3014. <dd class="dd3">
  3015. parse '&lt;?name ... ?&gt;' instead of '&lt;% ... %&gt;'
  3016. </dd>
  3017. <dt class="dt3"><b>
  3018. --trim=false </b></dt>
  3019. <dd class="dd3">
  3020. No trimming spaces around '&lt;% %&gt;'.
  3021. </dd>
  3022. </dl>
  3023. <br>
  3024. <a name="command-props"></a>
  3025. <h3 class="section2">Properties</h3>
  3026. <p>Some Eruby classes can take optional properties to change it's compile option.
  3027. For example, property '--indent=" "' may change indentation of compiled source code.
  3028. Try 'erubis -h' for details.
  3029. </p>
  3030. <br>
  3031. <br>
  3032. </div>
  3033. </blockquote>
  3034. </body>
  3035. </html>