users-guide.html 110 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551
  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. <p>release: 2.7.0
  15. </p>
  16. <a name="preface"></a>
  17. <h2 class="section1">Preface</h2>
  18. <p>Erubis is an implementation of eRuby.
  19. It has the following features.
  20. </p>
  21. <ul type="disc">
  22. <li>Very fast, almost three times faster than ERB and about ten percent faster than eruby (implemented in C)
  23. </li>
  24. <li>File caching of converted Ruby script support
  25. </li>
  26. <li>Auto escaping support
  27. </li>
  28. <li>Auto trimming spaces around '&lt;% %&gt;'
  29. </li>
  30. <li>Embedded pattern changeable (default '&lt;% %&gt;')
  31. </li>
  32. <li>Enable to handle Processing Instructions (PI) as embedded pattern (ex. '&lt;?rb ... ?&gt;')
  33. </li>
  34. <li>Multi-language support (Ruby/PHP/C/Java/Scheme/Perl/Javascript)
  35. </li>
  36. <li>Context object available and easy to combine eRuby template with YAML datafile
  37. </li>
  38. <li>Print statement available
  39. </li>
  40. <li>Easy to expand and customize in subclass
  41. </li>
  42. <li><a href="#rails">Ruby on Rails support</a>
  43. </li>
  44. <li>mod_ruby support|#topcs-modruby
  45. </li>
  46. </ul>
  47. <p>Erubis is implemented in pure Ruby. It requires Ruby 1.8 or higher.
  48. Erubis now supports Ruby 1.9.
  49. </p>
  50. <a name="toc"></a>
  51. <h3 class="section2">Table of Contents</h3>
  52. <ul>
  53. <li><a href="#preface">Preface</a>
  54. <ul>
  55. <li><a href="#toc">Table of Contents</a>
  56. </li>
  57. </ul>
  58. </li>
  59. <li><a href="#install">Installation</a>
  60. </li>
  61. <li><a href="#tutorial">Tutorial</a>
  62. <ul>
  63. <li><a href="#tut-basic">Basic Example</a>
  64. </li>
  65. <li><a href="#tut-trim">Trimming Spaces</a>
  66. </li>
  67. <li><a href="#tut-escape">Escape</a>
  68. </li>
  69. <li><a href="#tut-pattern">Embedded Pattern</a>
  70. </li>
  71. <li><a href="#tut-context">Context Object</a>
  72. </li>
  73. <li><a href="#tut-datafile">Context Data File</a>
  74. </li>
  75. <li><a href="#tut-datastr">Context Data String</a>
  76. </li>
  77. <li><a href="#tut-preamble">Preamble and Postamble</a>
  78. </li>
  79. <li><a href="#tut-pi">Processing Instruction (PI) Converter</a>
  80. </li>
  81. <li><a href="#tut-notext">Retrieve Ruby Code</a>
  82. </li>
  83. </ul>
  84. </li>
  85. <li><a href="#enhancer">Enhancer</a>
  86. <ul>
  87. <li><a href="#escape-enhancer">EscapeEnhancer</a>
  88. </li>
  89. <li><a href="#stdout-enhancer">StdoutEnhancer</a>
  90. </li>
  91. <li><a href="#printout-enhancer">PrintOutEnhancer</a>
  92. </li>
  93. <li><a href="#printenabled-enhancer">PrintEnabledEnhancer</a>
  94. </li>
  95. <li><a href="#array-enhancer">ArrayEnhancer</a>
  96. </li>
  97. <li><a href="#arraybuffer-enhancer">ArrayBufferEnhancer</a>
  98. </li>
  99. <li><a href="#stringbuffer-enhancer">StringBufferEnhancer</a>
  100. </li>
  101. <li><a href="#erbout-enhancer">ErboutEnhancer</a>
  102. </li>
  103. <li><a href="#notext-enhancer">NoTextEnhancer</a>
  104. </li>
  105. <li><a href="#nocode-enhancer">NoCodeEnhancer</a>
  106. </li>
  107. <li><a href="#simplify-enhancer">SimplifyEnhancer</a>
  108. </li>
  109. <li><a href="#bipattern-enhancer">BiPatternEnhancer</a>
  110. </li>
  111. <li><a href="#percentline-enhancer">PercentLineEnhancer</a>
  112. </li>
  113. <li><a href="#prefixedline-enhancer">PrefixedLineEnhancer</a>
  114. </li>
  115. <li><a href="#headerfooter-enhancer">HeaderFooterEnhancer</a>
  116. </li>
  117. <li><a href="#interpolation-enhancer">InterpolationEnhancer</a>
  118. </li>
  119. <li><a href="#deleteindent-enhancer">DeleteIndentEnhancer</a>
  120. </li>
  121. </ul>
  122. </li>
  123. <li><a href="#lang">Multi-Language Support</a>
  124. <ul>
  125. <li><a href="#lang-php">PHP</a>
  126. </li>
  127. <li><a href="#lang-c">C</a>
  128. </li>
  129. <li><a href="#lang-cpp">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="#topics-fasteruby"><code>Erubis::FastEruby</code> Class</a>
  156. </li>
  157. <li><a href="#topics-bufvar"><code>:bufvar</code> Option</a>
  158. </li>
  159. <li><a href="#topics-trimspaces">'&lt;%= =%&gt;' and '&lt;%= -%&gt;'</a>
  160. </li>
  161. <li><a href="#topics-doublepercent">'&lt;%% %&gt;' and '&lt;%%= %&gt;'</a>
  162. </li>
  163. <li><a href="#topics-context-vs-binding">evaluate(context) v.s. result(binding)</a>
  164. </li>
  165. <li><a href="#topics-fasteruby">Class Erubis::FastEruby</a>
  166. </li>
  167. <li><a href="#topics-syntax">Syntax Checking</a>
  168. </li>
  169. <li><a href="#topics-caching">File Caching</a>
  170. </li>
  171. <li><a href="#topics-tinyeruby">Erubis::TinyEruby class</a>
  172. </li>
  173. <li><a href="#topics-php">NoTextEnhancer and NoCodeEnhancer in PHP</a>
  174. </li>
  175. <li><a href="#topcs-modruby">Helper Class for mod_ruby</a>
  176. </li>
  177. <li><a href="#topics-index-cgi">Helper CGI Script for Apache</a>
  178. </li>
  179. <li><a href="#topics-defmethod">Define method</a>
  180. </li>
  181. <li><a href="#topics-benchmark">Benchmark</a>
  182. </li>
  183. </ul>
  184. </li>
  185. <li><a href="#command">Command Reference</a>
  186. <ul>
  187. <li><a href="#command-usage">Usage</a>
  188. </li>
  189. <li><a href="#command-options">Options</a>
  190. </li>
  191. <li><a href="#command-props">Properties</a>
  192. </li>
  193. </ul>
  194. </li>
  195. </ul>
  196. <br>
  197. <br>
  198. <a name="install"></a>
  199. <h2 class="section1">Installation</h2>
  200. <ul type="disc">
  201. <li>If you have installed RubyGems, just type <code>gem install --remote erubis</code>.
  202. <pre class="terminal">$ sudo gem install --remote erubis
  203. </pre>
  204. </li>
  205. </ul>
  206. <ul type="disc">
  207. <li>Else install <a href="http://rubyforge.org/projects/erubis/">abstract</a> at first,
  208. and download erubis_X.X.X.tar.bz2 and install it by setup.rb.
  209. <pre class="terminal">$ tar xjf abstract_X.X.X.tar.bz2
  210. $ cd abstract_X.X.X/
  211. $ sudo ruby setup.rb
  212. $ cd ..
  213. $ tar xjf erubis_X.X.X.tar.bz2
  214. $ cd erubis_X.X.X/
  215. $ sudo ruby setup.rb
  216. </pre>
  217. </li>
  218. </ul>
  219. <ul type="disc">
  220. <li>(Optional) 'contrib/inline-require' enables you to merge 'lib/**/*.rb' into 'bin/erubis'.
  221. <pre class="terminal">$ tar xjf erubis_X.X.X.tar.bz2
  222. $ cd erubis_X.X.X/
  223. $ unset RUBYLIB
  224. $ contrib/inline-require -I lib bin/erubis &gt; contrib/erubis
  225. </pre>
  226. </li>
  227. </ul>
  228. <br>
  229. <a name="tutorial"></a>
  230. <h2 class="section1">Tutorial</h2>
  231. <a name="tut-basic"></a>
  232. <h3 class="section2">Basic Example</h3>
  233. <p>Here is a basic example of Erubis.
  234. </p>
  235. <a name="example1.eruby"></a>
  236. <div class="program_caption">
  237. example1.eruby</div>
  238. <pre class="program">&lt;ul&gt;
  239. <strong>&lt;% for item in list %&gt;</strong>
  240. &lt;li&gt;<strong>&lt;%= item %&gt;</strong>&lt;/li&gt;
  241. <strong>&lt;% end %&gt;</strong>
  242. <strong>&lt;%# here is ignored because starting with '#' %&gt;</strong>
  243. &lt;/ul&gt;
  244. </pre>
  245. <a name="example1.rb"></a>
  246. <div class="program_caption">
  247. example1.rb</div>
  248. <pre class="program">require 'erubis'
  249. input = File.read('example1.eruby')
  250. eruby = <strong>Erubis::Eruby.new(input)</strong> # create Eruby object
  251. puts "---------- script source ---"
  252. puts <strong>eruby.src</strong> # print script source
  253. puts "---------- result ----------"
  254. list = ['aaa', 'bbb', 'ccc']
  255. puts <strong>eruby.result(binding())</strong> # get result
  256. ## or puts eruby.result(<strong>:list=&gt;list</strong>) # or pass Hash instead of Binding
  257. ## # or
  258. ## eruby = Erubis::Eruby.new
  259. ## input = File.read('example1.eruby')
  260. ## src = eruby.convert(input)
  261. ## eval src
  262. </pre>
  263. <a name="example1.result"></a>
  264. <div class="terminal_caption">
  265. output</div>
  266. <pre class="terminal">$ ruby example1.rb
  267. ---------- script source ---
  268. _buf = ''; _buf &lt;&lt; '&lt;ul&gt;
  269. '; for item in list
  270. _buf &lt;&lt; ' &lt;li&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/li&gt;
  271. '; end
  272. _buf &lt;&lt; '&lt;/ul&gt;
  273. ';
  274. _buf.to_s
  275. ---------- result ----------
  276. &lt;ul&gt;
  277. &lt;li&gt;aaa&lt;/li&gt;
  278. &lt;li&gt;bbb&lt;/li&gt;
  279. &lt;li&gt;ccc&lt;/li&gt;
  280. &lt;/ul&gt;
  281. </pre>
  282. <p>Erubis has command 'erubis'. Command-line option '-x' shows the compiled source code of eRuby script.
  283. </p>
  284. <a name="example1_x.result"></a>
  285. <div class="terminal_caption">
  286. example of command-line option '-x'</div>
  287. <pre class="terminal">$ erubis <strong>-x</strong> example1.eruby
  288. _buf = ''; _buf &lt;&lt; '&lt;ul&gt;
  289. '; for item in list
  290. _buf &lt;&lt; ' &lt;li&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/li&gt;
  291. '; end
  292. _buf &lt;&lt; '&lt;/ul&gt;
  293. ';
  294. _buf.to_s
  295. </pre>
  296. <br>
  297. <a name="tut-trim"></a>
  298. <h3 class="section2">Trimming Spaces</h3>
  299. <p>Erubis deletes spaces around '&lt;% %&gt;' automatically, while it leaves spaces around '&lt;%= %&gt;'.
  300. </p>
  301. <a name="example2.eruby.comment_filter"></a>
  302. <div class="program_caption">
  303. example2.eruby</div>
  304. <pre class="program">&lt;ul&gt;
  305. &lt;% for item in list %&gt; # trimmed
  306. &lt;li&gt;
  307. &lt;%= item %&gt; # not trimmed
  308. &lt;/li&gt;
  309. &lt;% end %&gt; # trimmed
  310. &lt;/ul&gt;
  311. </pre>
  312. <a name="example2_x.result"></a>
  313. <div class="terminal_caption">
  314. compiled source code</div>
  315. <pre class="terminal">$ erubis -x example2.eruby
  316. _buf = ''; _buf &lt;&lt; '&lt;ul&gt;
  317. '; for item in list
  318. _buf &lt;&lt; ' &lt;li&gt;
  319. '; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '
  320. '; _buf &lt;&lt; ' &lt;/li&gt;
  321. '; end
  322. _buf &lt;&lt; '&lt;/ul&gt;
  323. ';
  324. _buf.to_s
  325. </pre>
  326. <p>If you want leave spaces around '&lt;% %&gt;', add command-line property '--trim=false'.
  327. </p>
  328. <a name="example2_trim.result"></a>
  329. <div class="terminal_caption">
  330. compiled source code with command-line property '--trim=false'</div>
  331. <pre class="terminal">$ erubis -x <strong>--trim=false</strong> example2.eruby
  332. _buf = ''; _buf &lt;&lt; '&lt;ul&gt;
  333. '; _buf &lt;&lt; ' '; for item in list ; _buf &lt;&lt; '
  334. '; _buf &lt;&lt; ' &lt;li&gt;
  335. '; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '
  336. '; _buf &lt;&lt; ' &lt;/li&gt;
  337. '; _buf &lt;&lt; ' '; end ; _buf &lt;&lt; '
  338. '; _buf &lt;&lt; '&lt;/ul&gt;
  339. ';
  340. _buf.to_s
  341. </pre>
  342. <p>Or add option <code>:trim=&gt;false</code> to Erubis::Eruby.new().
  343. </p>
  344. <a name="example2.rb"></a>
  345. <div class="program_caption">
  346. example2.rb</div>
  347. <pre class="program">require 'erubis'
  348. input = File.read('example2.eruby')
  349. eruby = Erubis::Eruby.new(input<strong>, :trim=&gt;false</strong>)
  350. puts "----- script source ---"
  351. puts eruby.src # print script source
  352. puts "----- result ----------"
  353. list = ['aaa', 'bbb', 'ccc']
  354. puts eruby.result(binding()) # get result
  355. </pre>
  356. <a name="example2.result"></a>
  357. <div class="terminal_caption">
  358. output</div>
  359. <pre class="terminal">$ ruby example2.rb
  360. ----- script source ---
  361. _buf = ''; _buf &lt;&lt; '&lt;ul&gt;
  362. '; <strong>_buf &lt;&lt; ' ';</strong> for item in list ; _buf &lt;&lt; '
  363. '; _buf &lt;&lt; ' &lt;li&gt;
  364. '; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '
  365. '; _buf &lt;&lt; ' &lt;/li&gt;
  366. '; <strong>_buf &lt;&lt; ' ';</strong> end ; _buf &lt;&lt; '
  367. '; _buf &lt;&lt; '&lt;/ul&gt;
  368. ';
  369. _buf.to_s
  370. ----- result ----------
  371. &lt;ul&gt;
  372. &lt;li&gt;
  373. aaa
  374. &lt;/li&gt;
  375. &lt;li&gt;
  376. bbb
  377. &lt;/li&gt;
  378. &lt;li&gt;
  379. ccc
  380. &lt;/li&gt;
  381. &lt;/ul&gt;
  382. </pre>
  383. <br>
  384. <a name="tut-escape"></a>
  385. <h3 class="section2">Escape</h3>
  386. <p>Erubis has ability to escape (sanitize) expression.
  387. Erubis::Eruby class act as the following:
  388. </p>
  389. <ul type="disc">
  390. <li><code>&lt;%= <em>expr</em> %&gt;</code> - not escaped.
  391. </li>
  392. <li><code>&lt;%== <em>expr</em> %&gt;</code> - escaped.
  393. </li>
  394. <li><code>&lt;%=== <em>expr</em> %&gt;</code> - out to $stderr.
  395. </li>
  396. <li><code>&lt;%==== <em>expr</em> %&gt;</code> - ignored.
  397. </li>
  398. </ul>
  399. <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.
  400. It means that using Erubis::EscapedEruby you can escape expression by default.
  401. Also Erubis::XmlEruby class (which is equivalent to Erubis::EscapedEruby) is provided for compatibility with Erubis 1.1.
  402. </p>
  403. <a name="example3.eruby"></a>
  404. <div class="program_caption">
  405. example3.eruby</div>
  406. <pre class="program">&lt;% for item in list %&gt;
  407. &lt;p&gt;<strong>&lt;%=</strong> item <strong>%&gt;</strong>&lt;/p&gt;
  408. &lt;p&gt;<strong>&lt;%==</strong> item <strong>%&gt;</strong>&lt;/p&gt;
  409. &lt;p&gt;<strong>&lt;%===</strong> item <strong>%&gt;</strong>&lt;/p&gt;
  410. &lt;% end %&gt;
  411. </pre>
  412. <a name="example3.rb"></a>
  413. <div class="program_caption">
  414. example3.rb</div>
  415. <pre class="program">require 'erubis'
  416. input = File.read('example3.eruby')
  417. eruby = Erubis::<strong>EscapedEruby</strong>.new(input) # or Erubis::XmlEruby
  418. puts "----- script source ---"
  419. puts eruby.src # print script source
  420. puts "----- result ----------"
  421. <strong>list = ['&lt;aaa&gt;', 'b&amp;b', '"ccc"']</strong>
  422. puts eruby.result(binding()) # get result
  423. </pre>
  424. <a name="example3.result.split_filter"></a>
  425. <div class="terminal_caption">
  426. output</div>
  427. <pre class="terminal">$ ruby example3.rb 2&gt; stderr.log
  428. ----- script source ---
  429. _buf = ''; for item in list
  430. _buf &lt;&lt; ' &lt;p&gt;'; <strong>_buf &lt;&lt; Erubis::XmlHelper.escape_xml( item )</strong>; _buf &lt;&lt; '&lt;/p&gt;
  431. &lt;p&gt;'; <strong>_buf &lt;&lt; ( item ).to_s</strong>; _buf &lt;&lt; '&lt;/p&gt;
  432. &lt;p&gt;'; <strong>$stderr.puts("*** debug: item=#{(item).inspect}")</strong>; _buf &lt;&lt; '&lt;/p&gt;
  433. '; end
  434. _buf.to_s
  435. ----- result ----------
  436. &lt;p&gt;<strong>&amp;lt;aaa&amp;gt;</strong>&lt;/p&gt;
  437. &lt;p&gt;&lt;aaa&gt;&lt;/p&gt;
  438. &lt;p&gt;&lt;/p&gt;
  439. &lt;p&gt;<strong>b&amp;amp;b</strong>&lt;/p&gt;
  440. &lt;p&gt;b&amp;b&lt;/p&gt;
  441. &lt;p&gt;&lt;/p&gt;
  442. &lt;p&gt;<strong>&amp;quot;ccc&amp;quot;</strong>&lt;/p&gt;
  443. &lt;p&gt;"ccc"&lt;/p&gt;
  444. &lt;p&gt;&lt;/p&gt;
  445. $ cat stderr.log
  446. *** debug: item="&lt;aaa&gt;"
  447. *** debug: item="b&amp;b"
  448. *** debug: item="\"ccc\""
  449. </pre>
  450. <p>The command-line option '-e' will do the same action as Erubis::EscapedEruby.
  451. This option is available for any language.
  452. </p>
  453. <a name="example3_e.result"></a>
  454. <pre class="terminal">$ erubis -l ruby <strong>-e</strong> example3.eruby
  455. _buf = ''; for item in list
  456. _buf &lt;&lt; ' &lt;p&gt;'; _buf &lt;&lt; Erubis::XmlHelper.escape_xml( item ); _buf &lt;&lt; '&lt;/p&gt;
  457. &lt;p&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/p&gt;
  458. &lt;p&gt;'; $stderr.puts("*** debug: item=#{(item).inspect}"); _buf &lt;&lt; '&lt;/p&gt;
  459. '; end
  460. _buf.to_s
  461. </pre>
  462. <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.
  463. </p>
  464. <div class="program_caption">
  465. example to override Erubis::Eruby#escaped_expr()</div>
  466. <pre class="program">class CGIEruby &lt; Erubis::Eruby
  467. def <strong>escaped_expr(code)</strong>
  468. return "CGI.escapeHTML((#{code.strip}).to_s)"
  469. #return "h(#{code.strip})"
  470. end
  471. end
  472. class LatexEruby &lt; Erubi::Eruby
  473. def <strong>escaped_expr(code)</strong>
  474. return "(#{code}).gsub(/[%\\]/,'\\\\\&amp;')"
  475. end
  476. end
  477. </pre>
  478. <div class="footnote">
  479. <dl compact>
  480. <dt>(<a name="fnref:1" href="#fnlink:1">*1</a>)</dt>
  481. <dd>Erubis::EscapedEruby class includes Erubis::EscapeEnhancer which swtches the action of '&lt;%= %&gt;' and '&lt;%== %&gt;'.</dd>
  482. </dl>
  483. </div>
  484. <br>
  485. <a name="tut-pattern"></a>
  486. <h3 class="section2">Embedded Pattern</h3>
  487. <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().
  488. </p>
  489. <a name="example4.eruby"></a>
  490. <div class="program_caption">
  491. example4.eruby</div>
  492. <pre class="program"><strong>&lt;!--%</strong> for item in list <strong>%--&gt;</strong>
  493. &lt;p&gt;<strong>&lt;!--%=</strong> item <strong>%--&gt;</strong>&lt;/p&gt;
  494. <strong>&lt;!--%</strong> end <strong>%--&gt;</strong>
  495. </pre>
  496. <a name="example4_x.result"></a>
  497. <div class="terminal_caption">
  498. compiled source code with command-line option '-p'</div>
  499. <pre class="terminal">$ erubis -x <strong>-p '&lt;!--% %--&gt;'</strong> example4.eruby
  500. _buf = ''; for item in list
  501. _buf &lt;&lt; ' &lt;p&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/p&gt;
  502. '; end
  503. _buf.to_s
  504. </pre>
  505. <a name="example4.rb"></a>
  506. <div class="program_caption">
  507. example4.rb</div>
  508. <pre class="program">require 'erubis'
  509. input = File.read('example4.eruby')
  510. eruby = Erubis::Eruby.new(input<strong>, :pattern=&gt;'&lt;!--% %--&gt;'</strong>)
  511. # or '&lt;(?:!--)?% %(?:--)?&gt;'
  512. puts "---------- script source ---"
  513. puts eruby.src # print script source
  514. puts "---------- result ----------"
  515. list = ['aaa', 'bbb', 'ccc']
  516. puts eruby.result(binding()) # get result
  517. </pre>
  518. <a name="example4.result"></a>
  519. <div class="terminal_caption">
  520. output</div>
  521. <pre class="terminal">$ ruby example4.rb
  522. ---------- script source ---
  523. _buf = ''; for item in list
  524. _buf &lt;&lt; ' &lt;p&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/p&gt;
  525. '; end
  526. _buf.to_s
  527. ---------- result ----------
  528. &lt;p&gt;aaa&lt;/p&gt;
  529. &lt;p&gt;bbb&lt;/p&gt;
  530. &lt;p&gt;ccc&lt;/p&gt;
  531. </pre>
  532. <p>It is able to specify regular expression with :pattern option.
  533. Notice that you must use '<code>(?: )</code>' instead of '<code>( )</code>' for grouping.
  534. For example, '<code>&lt;(!--)?% %(--)?&gt;</code>' will not work while '<code>&lt;(?:!--)?% %(?:--)?&gt;</code>' will work.
  535. </p>
  536. <br>
  537. <a name="tut-context"></a>
  538. <h3 class="section2">Context Object</h3>
  539. <p>Context object is a set of data which are used in eRuby script.
  540. Using context object makes clear which data to be used.
  541. In Erubis, Hash object and Erubis::Context object are available as context object.
  542. </p>
  543. <p>Context data can be accessible via instance variables in eRuby script.
  544. </p>
  545. <a name="example5.eruby"></a>
  546. <div class="program_caption">
  547. example5.eruby</div>
  548. <pre class="program">&lt;span&gt;&lt;%= <strong>@val</strong> %&gt;&lt;/span&gt;
  549. &lt;ul&gt;
  550. &lt;% for item in <strong>@list</strong> %&gt;
  551. &lt;li&gt;&lt;%= item %&gt;&lt;/li&gt;
  552. &lt;% end %&gt;
  553. &lt;/ul&gt;
  554. </pre>
  555. <a name="example5.rb"></a>
  556. <div class="program_caption">
  557. example5.rb</div>
  558. <pre class="program">require 'erubis'
  559. input = File.read('example5.eruby')
  560. eruby = Erubis::Eruby.new(input) # create Eruby object
  561. ## create context object
  562. ## (key means var name, which may be string or symbol.)
  563. <strong>context = {
  564. :val =&gt; 'Erubis Example',
  565. 'list' =&gt; ['aaa', 'bbb', 'ccc'],
  566. }</strong>
  567. ## or
  568. # context = Erubis::Context.new()
  569. # context['val'] = 'Erubis Example'
  570. # context[:list] = ['aaa', 'bbb', 'ccc'],
  571. puts <strong>eruby.evaluate(context)</strong> # get result
  572. </pre>
  573. <a name="example5.result"></a>
  574. <div class="terminal_caption">
  575. output</div>
  576. <pre class="terminal">$ ruby example5.rb
  577. &lt;span&gt;Erubis Example&lt;/span&gt;
  578. &lt;ul&gt;
  579. &lt;li&gt;aaa&lt;/li&gt;
  580. &lt;li&gt;bbb&lt;/li&gt;
  581. &lt;li&gt;ccc&lt;/li&gt;
  582. &lt;/ul&gt;
  583. </pre>
  584. <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'.
  585. 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.
  586. </p>
  587. <p>Here is the definition of Erubis#result() and Erubis#evaluate().
  588. </p>
  589. <div class="program_caption">
  590. definition of result(binding) and evaluate(context)</div>
  591. <pre class="program">def result(_binding=TOPLEVEL_BINDING)
  592. if _binding.is_a?(Hash)
  593. # load hash data as local variable
  594. _h = _binding
  595. _binding = binding()
  596. eval _h.collect{|k,v| "#{k} = _h[#{k.inspect}];"}.join, _binding
  597. end
  598. return <strong>eval(@src, _binding)</strong>
  599. end
  600. def evaluate(_context=Erubis::Context.new)
  601. if _context.is_a?(Hash)
  602. # convert hash object to Context object
  603. _hash = _context
  604. _context = Erubis::Context.new
  605. _hash.each {|k, v| _context[k] = v }
  606. end
  607. return <strong>_context.instance_eval(@src)</strong>
  608. end
  609. </pre>
  610. <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.
  611. </p>
  612. <a name="example6.rb"></a>
  613. <div class="program_caption">
  614. example6.rb</div>
  615. <pre class="program">class MyData
  616. attr_accessor :val, :list
  617. end
  618. ## any object can be a context object
  619. <strong>mydata = MyData.new</strong>
  620. <strong>mydata.val = 'Erubis Example'</strong>
  621. <strong>mydata.list = ['aaa', 'bbb', 'ccc']</strong>
  622. require 'erubis'
  623. eruby = Erubis::Eruby.new(File.read('example5.eruby'))
  624. puts eruby.evaluate(<strong>mydata</strong>)
  625. </pre>
  626. <a name="example6.result"></a>
  627. <div class="terminal_caption">
  628. output</div>
  629. <pre class="terminal">$ ruby example6.rb
  630. &lt;span&gt;Erubis Example&lt;/span&gt;
  631. &lt;ul&gt;
  632. &lt;li&gt;aaa&lt;/li&gt;
  633. &lt;li&gt;bbb&lt;/li&gt;
  634. &lt;li&gt;ccc&lt;/li&gt;
  635. &lt;/ul&gt;
  636. </pre>
  637. <p>It is recommended to use 'Erubis::Eruby#evaluate(context)' rather than 'Erubis::Eruby#result(binding())' because the latter has some problems.
  638. See <a href="#topics-context-vs-binding">evaluate(context) v.s. result(binding)</a> section for details.
  639. </p>
  640. <br>
  641. <a name="tut-datafile"></a>
  642. <h3 class="section2">Context Data File</h3>
  643. <p>Command-line option '-f' specifies context data file.
  644. Erubis load context data file and use it as context data.
  645. Context data file can be YAML file ('*.yaml' or '*.yml') or Ruby script ('*.rb').
  646. </p>
  647. <a name="example7.eruby"></a>
  648. <div class="program_caption">
  649. example7.eruby</div>
  650. <pre class="program">&lt;h1&gt;&lt;%= <strong>@title</strong> %&gt;&lt;/h1&gt;
  651. &lt;ul&gt;
  652. &lt;% for user in <strong>@users</strong> %&gt;
  653. &lt;li&gt;
  654. &lt;a href="mailto:&lt;%= user['mail']%&gt;"&gt;&lt;%= user['name'] %&gt;&lt;/a&gt;
  655. &lt;/li&gt;
  656. &lt;% end %&gt;
  657. &lt;/ul&gt;
  658. </pre>
  659. <a name="context.yaml"></a>
  660. <div class="program_caption">
  661. context.yaml</div>
  662. <pre class="program"><strong>title:</strong> Users List
  663. <strong>users:</strong>
  664. - name: foo
  665. mail: foo@mail.com
  666. - name: bar
  667. mail: bar@mail.net
  668. - name: baz
  669. mail: baz@mail.org
  670. </pre>
  671. <a name="context.rb"></a>
  672. <div class="program_caption">
  673. context.rb</div>
  674. <pre class="program">@title = 'Users List'
  675. @users = [
  676. { 'name'=&gt;'foo', 'mail'=&gt;'foo@mail.com' },
  677. { 'name'=&gt;'bar', 'mail'=&gt;'bar@mail.net' },
  678. { 'name'=&gt;'baz', 'mail'=&gt;'baz@mail.org' },
  679. ]
  680. </pre>
  681. <a name="example7.result.split_filter"></a>
  682. <div class="terminal_caption">
  683. example of command-line option '-f'</div>
  684. <pre class="terminal">$ erubis <strong>-f context.yaml</strong> example7.eruby
  685. &lt;h1&gt;Users List&lt;/h1&gt;
  686. &lt;ul&gt;
  687. &lt;li&gt;
  688. &lt;a href="mailto:foo@mail.com"&gt;foo&lt;/a&gt;
  689. &lt;/li&gt;
  690. &lt;li&gt;
  691. &lt;a href="mailto:bar@mail.net"&gt;bar&lt;/a&gt;
  692. &lt;/li&gt;
  693. &lt;li&gt;
  694. &lt;a href="mailto:baz@mail.org"&gt;baz&lt;/a&gt;
  695. &lt;/li&gt;
  696. &lt;/ul&gt;
  697. $ erubis <strong>-f context.rb</strong> example7.eruby
  698. &lt;h1&gt;Users List&lt;/h1&gt;
  699. &lt;ul&gt;
  700. &lt;li&gt;
  701. &lt;a href="mailto:foo@mail.com"&gt;foo&lt;/a&gt;
  702. &lt;/li&gt;
  703. &lt;li&gt;
  704. &lt;a href="mailto:bar@mail.net"&gt;bar&lt;/a&gt;
  705. &lt;/li&gt;
  706. &lt;li&gt;
  707. &lt;a href="mailto:baz@mail.org"&gt;baz&lt;/a&gt;
  708. &lt;/li&gt;
  709. &lt;/ul&gt;
  710. </pre>
  711. <p>Command-line option '-S' converts keys of mapping in YAML data file from string into symbol.
  712. Command-line option '-B' invokes 'Erubis::Eruby#result(binding())' instead of 'Erubis::Eruby#evaluate(context)'.
  713. </p>
  714. <br>
  715. <a name="tut-datastr"></a>
  716. <h3 class="section2">Context Data String</h3>
  717. <p>Command-line option '-c <em>str</em>' enables you to specify context data in command-line.
  718. <em>str</em> can be YAML flow-style or Ruby code.
  719. </p>
  720. <a name="example8.eruby"></a>
  721. <div class="program_caption">
  722. example8.eruby</div>
  723. <pre class="program">&lt;h1&gt;&lt;%= @title %&gt;&lt;/h1&gt;
  724. &lt;ul&gt;
  725. &lt;% for item in @list %&gt;
  726. &lt;li&gt;&lt;%= item %&gt;&lt;/li&gt;
  727. &lt;% end %&gt;
  728. &lt;/ul&gt;
  729. </pre>
  730. <a name="example8_yaml.result"></a>
  731. <div class="terminal_caption">
  732. example of YAML flow style</div>
  733. <pre class="terminal">$ erubis <strong>-c '{title: Example, list: [AAA, BBB, CCC]}'</strong> example8.eruby
  734. &lt;h1&gt;Example&lt;/h1&gt;
  735. &lt;ul&gt;
  736. &lt;li&gt;AAA&lt;/li&gt;
  737. &lt;li&gt;BBB&lt;/li&gt;
  738. &lt;li&gt;CCC&lt;/li&gt;
  739. &lt;/ul&gt;
  740. </pre>
  741. <a name="example8_ruby.result"></a>
  742. <div class="terminal_caption">
  743. example of Ruby code</div>
  744. <pre class="terminal">$ erubis <strong>-c '@title="Example"; @list=%w[AAA BBB CCC]'</strong> example8.eruby
  745. &lt;h1&gt;Example&lt;/h1&gt;
  746. &lt;ul&gt;
  747. &lt;li&gt;AAA&lt;/li&gt;
  748. &lt;li&gt;BBB&lt;/li&gt;
  749. &lt;li&gt;CCC&lt;/li&gt;
  750. &lt;/ul&gt;
  751. </pre>
  752. <br>
  753. <a name="tut-preamble"></a>
  754. <h3 class="section2">Preamble and Postamble</h3>
  755. <p>The first line ('_buf = '';') in the compiled source code is called preamble
  756. and the last line ('_buf.to_s') is called postamble.
  757. </p>
  758. <p>Command-line option '-b' skips the output of preamble and postamble.
  759. </p>
  760. <a name="example9.eruby"></a>
  761. <div class="program_caption">
  762. example9.eruby</div>
  763. <pre class="program">&lt;% for item in @list %&gt;
  764. &lt;b&gt;&lt;%= item %&gt;&lt;/b&gt;
  765. &lt;% end %&gt;
  766. </pre>
  767. <a name="example9.result.split_filter"></a>
  768. <div class="terminal_caption">
  769. compiled source code with and without command-line option '-b'</div>
  770. <pre class="terminal">$ erubis -x example9.eruby
  771. <strong>_buf = '';</strong> for item in @list
  772. _buf &lt;&lt; ' &lt;b&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/b&gt;
  773. '; end
  774. <strong>_buf.to_s</strong>
  775. $ erubis -x <strong>-b</strong> example9.eruby
  776. for item in @list
  777. _buf &lt;&lt; ' &lt;b&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/b&gt;
  778. '; end
  779. </pre>
  780. <p>Erubis::Eruby.new option '<code>:preamble=&gt;false</code>' and '<code>:postamble=&gt;false</code>' also suppress output of preamble or postamle.
  781. </p>
  782. <a name="example9.rb"></a>
  783. <div class="program_caption">
  784. example9.rb</div>
  785. <pre class="program">require 'erubis'
  786. input = File.read('example9.eruby')
  787. eruby1 = Erubis::Eruby.new(input)
  788. eruby2 = Erubis::Eruby.new(input, <strong>:preamble=&gt;false, :postamble=&gt;false</strong>)
  789. puts eruby1.src # print preamble and postamble
  790. puts "--------------"
  791. puts eruby2.src # don't print preamble and postamble
  792. </pre>
  793. <a name="example9.result"></a>
  794. <div class="terminal_caption">
  795. output</div>
  796. <pre class="terminal">$ ruby example9.rb
  797. _buf = ''; for item in @list
  798. _buf &lt;&lt; ' &lt;b&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/b&gt;
  799. '; end
  800. _buf.to_s
  801. --------------
  802. for item in @list
  803. _buf &lt;&lt; ' &lt;b&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/b&gt;
  804. '; end
  805. </pre>
  806. <br>
  807. <a name="tut-pi"></a>
  808. <h3 class="section2">Processing Instruction (PI) Converter</h3>
  809. <p>Erubis can parse Processing Instructions (PI) as embedded pattern.
  810. </p>
  811. <ul type="disc">
  812. <li>'<code>&lt;?rb <em>...</em> ?&gt;</code>' represents Ruby statement.
  813. </li>
  814. <li>'<code>@{<em>...</em>}@</code>' represents escaped expression value.
  815. </li>
  816. <li>'<code>@!{<em>...</em>}@</code>' represents normal expression value.
  817. </li>
  818. <li>'<code>@!!{<em>...</em>}@</code>' prints expression value to standard output.
  819. </li>
  820. <li>(experimental) '<code>&lt;%= <em>...</em> %&gt;</code>' is also available to print expression value.
  821. </li>
  822. </ul>
  823. <p>This is more useful than basic embedded pattern ('<code>&lt;% ... &gt;</code>') because PI doesn't break XML or HTML at all.
  824. For example the following XHTML file is well-formed and HTML validator got no errors on this example.
  825. </p>
  826. <a name="example10.xhtml"></a>
  827. <div class="program_caption">
  828. example10.xhtml</div>
  829. <pre class="program">&lt;?xml version="1.0" ?&gt;
  830. <strong>&lt;?rb
  831. lang = 'en'
  832. list = ['&lt;aaa&gt;', 'b&amp;b', '"ccc"']
  833. ?&gt;</strong>
  834. &lt;html lang="<strong>@!{lang}@</strong>"&gt;
  835. &lt;body&gt;
  836. &lt;ul&gt;
  837. <strong>&lt;?rb for item in list ?&gt;</strong>
  838. &lt;li&gt;<strong>@{item}@</strong>&lt;/li&gt;
  839. <strong>&lt;?rb end ?&gt;</strong>
  840. &lt;/ul&gt;
  841. &lt;/body&gt;
  842. &lt;/html&gt;
  843. </pre>
  844. <p>If the command-line property '--pi=<em>name</em>' is specified, erubis command parses input with PI converter.
  845. If <em>name</em> is omitted then the following name is used according to '-l <em>lang</em>'.
  846. </p>
  847. <div align="center">
  848. <table class="table1" border="1" cellspacing="0">
  849. <tr class="tr1">
  850. <th class="th1">'-l' option</th>
  851. <th class="th1">PI name</th>
  852. </tr>
  853. <tr class="tr1">
  854. <td class="td1">-l ruby</td>
  855. <td class="td1">&lt;?rb ... ?&gt;</td>
  856. </tr>
  857. <tr class="tr1">
  858. <td class="td1">-l php</td>
  859. <td class="td1">&lt;?php ... ?&gt;</td>
  860. </tr>
  861. <tr class="tr1">
  862. <td class="td1">-l perl</td>
  863. <td class="td1">&lt;?perl ... ?&gt;</td>
  864. </tr>
  865. <tr class="tr1">
  866. <td class="td1">-l java</td>
  867. <td class="td1">&lt;?java ... ?&gt;</td>
  868. </tr>
  869. <tr class="tr1">
  870. <td class="td1">-l javascript</td>
  871. <td class="td1">&lt;?js ... ?&gt;</td>
  872. </tr>
  873. <tr class="tr1">
  874. <td class="td1">-l scheme</td>
  875. <td class="td1">&lt;?scheme ... ?&gt;</td>
  876. </tr>
  877. </table>
  878. </div>
  879. <a name="example10_x.result"></a>
  880. <div class="terminal_caption">
  881. output</div>
  882. <pre class="terminal">$ erubis -x <strong>--pi</strong> example10.xhtml
  883. _buf = ''; _buf &lt;&lt; '&lt;?xml version="1.0" ?&gt;
  884. ';
  885. lang = 'en'
  886. list = ['&lt;aaa&gt;', 'b&amp;b', '"ccc"']
  887. _buf &lt;&lt; '&lt;html lang="'; _buf &lt;&lt; (lang).to_s; _buf &lt;&lt; '"&gt;
  888. &lt;body&gt;
  889. &lt;ul&gt;
  890. '; for item in list
  891. _buf &lt;&lt; ' &lt;li&gt;'; _buf &lt;&lt; Erubis::XmlHelper.escape_xml(item); _buf &lt;&lt; '&lt;/li&gt;
  892. '; end
  893. _buf &lt;&lt; ' &lt;/ul&gt;
  894. &lt;/body&gt;
  895. &lt;/html&gt;
  896. ';
  897. _buf.to_s
  898. </pre>
  899. <p>Expression character can be changeable by command-line property '--embchar=<em>char</em>. Default is '<code>@</code>'.
  900. </p>
  901. <p>Use Erubis::PI::Eruby instead of Erubis::Eruby if you want to use PI as embedded pattern.
  902. </p>
  903. <a name="example10.rb"></a>
  904. <div class="program_caption">
  905. example10.rb</div>
  906. <pre class="program">require 'erubis'
  907. input = File.read('example10.xhtml')
  908. eruby = Erubis::PI::Eruby.new(input)
  909. print eruby.src
  910. </pre>
  911. <a name="example10.result"></a>
  912. <div class="terminal_caption">
  913. output</div>
  914. <pre class="terminal">$ ruby example10.rb
  915. _buf = ''; _buf &lt;&lt; '&lt;?xml version="1.0" ?&gt;
  916. ';
  917. lang = 'en'
  918. list = ['&lt;aaa&gt;', 'b&amp;b', '"ccc"']
  919. _buf &lt;&lt; '&lt;html lang="'; _buf &lt;&lt; (lang).to_s; _buf &lt;&lt; '"&gt;
  920. &lt;body&gt;
  921. &lt;ul&gt;
  922. '; for item in list
  923. _buf &lt;&lt; ' &lt;li&gt;'; _buf &lt;&lt; Erubis::XmlHelper.escape_xml(item); _buf &lt;&lt; '&lt;/li&gt;
  924. '; end
  925. _buf &lt;&lt; ' &lt;/ul&gt;
  926. &lt;/body&gt;
  927. &lt;/html&gt;
  928. ';
  929. _buf.to_s
  930. </pre>
  931. <p><strong>(experimental)</strong> Erubis supports '&lt;%= ... %&gt;' pattern with PI pattern.
  932. </p>
  933. <div class="program_caption">
  934. example of Rails view template</div>
  935. <pre class="program">&lt;table&gt;
  936. &lt;tr&gt;
  937. &lt;?rb for item in @list ?&gt;
  938. &lt;td&gt;@{item.id}@&lt;/td&gt;
  939. &lt;td&gt;@{item.name}@&lt;/td&gt;
  940. &lt;td&gt;
  941. <strong>&lt;%=</strong> link_to 'Destroy', {:action=&gt;'destroy', :id=&gt;item.id},
  942. :confirm=&gt;'Are you OK?' <strong>%&gt;</strong>
  943. &lt;/td&gt;
  944. &lt;?rb end ?&gt;
  945. &lt;/tr&gt;
  946. &lt;/table&gt;
  947. </pre>
  948. <br>
  949. <a name="tut-notext"></a>
  950. <h3 class="section2">Retrieve Ruby Code</h3>
  951. <p>Similar to '-x', ommand-line option '-X' shows converted Ruby source code.
  952. The difference between '-x' and 'X' is that the former converts text part but the latter ignores it.
  953. It means that you can retrieve Ruby code from eRuby script by '-X' option.
  954. </p>
  955. <p>For example, see the following eRuby script.
  956. This is some complex, so it is difficult to grasp the program code.
  957. </p>
  958. <a name="example11.rhtml"></a>
  959. <div class="program_caption">
  960. example11.rhtml</div>
  961. <pre class="program">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  962. &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  963. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  964. &lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt;
  965. &lt;body&gt;
  966. &lt;h3&gt;List&lt;/h3&gt;
  967. &lt;% if @list.nil? || @list.empty? %&gt;
  968. &lt;p&gt;not found.&lt;/p&gt;
  969. &lt;% else %&gt;
  970. &lt;table&gt;
  971. &lt;tbody&gt;
  972. &lt;% @list.each_with_index do |item, i| %&gt;
  973. &lt;tr bgcolor="&lt;%= i % 2 == 0 ? '#FCC' : '#CCF' %&gt;"&gt;
  974. &lt;td&gt;&lt;%= item %&gt;&lt;/td&gt;
  975. &lt;/tr&gt;
  976. &lt;% end %&gt;
  977. &lt;/tbody&gt;
  978. &lt;/table&gt;
  979. &lt;% end %&gt;
  980. &lt;/body&gt;
  981. &lt;/html&gt;
  982. </pre>
  983. <p>Command-line option '-X' extracts only the ruby code from eRuby script.
  984. </p>
  985. <a name="example11.result"></a>
  986. <div class="terminal_caption">
  987. result</div>
  988. <pre class="terminal">$ erubis <strong>-X</strong> example11.rhtml
  989. _buf = '';
  990. if @list.nil? || @list.empty?
  991. else
  992. @list.each_with_index do |item, i|
  993. _buf &lt;&lt; ( i % 2 == 0 ? '#FCC' : '#CCF' ).to_s;
  994. _buf &lt;&lt; ( item ).to_s;
  995. end
  996. end
  997. _buf.to_s
  998. </pre>
  999. <p>Command-line option '-C' (<strong>c</strong>mpact) deletes empty lines.
  1000. </p>
  1001. <a name="example11_C.result"></a>
  1002. <div class="terminal_caption">
  1003. result</div>
  1004. <pre class="terminal">$ erubis <strong>-XC</strong> example11.rhtml
  1005. _buf = '';
  1006. if @list.nil? || @list.empty?
  1007. else
  1008. @list.each_with_index do |item, i|
  1009. _buf &lt;&lt; ( i % 2 == 0 ? '#FCC' : '#CCF' ).to_s;
  1010. _buf &lt;&lt; ( item ).to_s;
  1011. end
  1012. end
  1013. _buf.to_s
  1014. </pre>
  1015. <p>Option '-U' (<strong>u</strong>nique) converts empty lines into a line.
  1016. </p>
  1017. <a name="example11_U.result"></a>
  1018. <div class="terminal_caption">
  1019. result</div>
  1020. <pre class="terminal">$ erubis <strong>-XU</strong> example11.rhtml
  1021. _buf = '';
  1022. if @list.nil? || @list.empty?
  1023. else
  1024. @list.each_with_index do |item, i|
  1025. _buf &lt;&lt; ( i % 2 == 0 ? '#FCC' : '#CCF' ).to_s;
  1026. _buf &lt;&lt; ( item ).to_s;
  1027. end
  1028. end
  1029. _buf.to_s
  1030. </pre>
  1031. <p>Option '-N' (<strong>n</strong>umber) adds line number.
  1032. It is available with '-C' or '-U'.
  1033. </p>
  1034. <a name="example11_N.result"></a>
  1035. <div class="terminal_caption">
  1036. result</div>
  1037. <pre class="terminal">$ erubis <strong>-XNU</strong> example11.rhtml
  1038. 1: _buf = '';
  1039. 7: if @list.nil? || @list.empty?
  1040. 9: else
  1041. 12: @list.each_with_index do |item, i|
  1042. 13: _buf &lt;&lt; ( i % 2 == 0 ? '#FCC' : '#CCF' ).to_s;
  1043. 14: _buf &lt;&lt; ( item ).to_s;
  1044. 16: end
  1045. 19: end
  1046. 22: _buf.to_s
  1047. </pre>
  1048. <p>Command-line option '-X' is available with PHP script.
  1049. </p>
  1050. <a name="example11.php"></a>
  1051. <div class="program_caption">
  1052. example11.php</div>
  1053. <pre class="program">&lt;?xml version="1.0"?&gt;
  1054. &lt;html&gt;
  1055. &lt;body&gt;
  1056. &lt;h3&gt;List&lt;/h3&gt;
  1057. &lt;?php if (!$list) { ?&gt;
  1058. &lt;p&gt;not found.&lt;/p&gt;
  1059. &lt;?php } else { ?&gt;
  1060. &lt;table&gt;
  1061. &lt;tbody&gt;
  1062. &lt;?php $i = 0; ?&gt;
  1063. &lt;?php foreach ($list as $item) { ?&gt;
  1064. &lt;tr bgcolor="&lt;?php echo ++$i % 2 == 1 ? '#FCC' : '#CCF'; ?&gt;"&gt;
  1065. &lt;td&gt;&lt;?php echo $item; ?&gt;&lt;/td&gt;
  1066. &lt;/tr&gt;
  1067. &lt;?php } ?&gt;
  1068. &lt;/tbody&gt;
  1069. &lt;/table&gt;
  1070. &lt;?php } ?&gt;
  1071. &lt;/body&gt;
  1072. &lt;/html&gt;
  1073. </pre>
  1074. <a name="example11_php.result"></a>
  1075. <div class="terminal_caption">
  1076. result</div>
  1077. <pre class="terminal">$ erubis -XNU <strong>-l php</strong> <strong>--pi=php</strong> --trim=false example11.php
  1078. 5: &lt;?php if (!$list) { ?&gt;
  1079. 7: &lt;?php } else { ?&gt;
  1080. 10: &lt;?php $i = 0; ?&gt;
  1081. 11: &lt;?php foreach ($list as $item) { ?&gt;
  1082. 12: &lt;?php echo ++$i % 2 == 1 ? '#FCC' : '#CCF'; ?&gt;
  1083. 13: &lt;?php echo $item; ?&gt;
  1084. 15: &lt;?php } ?&gt;
  1085. 18: &lt;?php } ?&gt;
  1086. </pre>
  1087. <br>
  1088. <br>
  1089. <a name="enhancer"></a>
  1090. <h2 class="section1">Enhancer</h2>
  1091. <p>Enhancer is a module to add a certain feature into Erubis::Eruby class.
  1092. Enhancer may be language-independent or only for Erubis::Eruby class.
  1093. </p>
  1094. <p>To use enhancers, define subclass and include them.
  1095. 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>.
  1096. </p>
  1097. <pre class="program">class MyEruby &lt; Erubis::Eruby
  1098. include EscapeEnhancer
  1099. include PercentLineEnhancer
  1100. include BiPatternEnhancer
  1101. end
  1102. </pre>
  1103. <p>You can specify enhancers in command-line with option '-E'.
  1104. The following is an example to use some enhancers in command-line.
  1105. </p>
  1106. <pre class="terminal">$ erubis -xE Escape,PercentLine,BiPattern example.eruby
  1107. </pre>
  1108. <p>The following is the list of enhancers.
  1109. </p>
  1110. <dl class="dl1">
  1111. <dt class="dt1">
  1112. <a href="#escape-enhancer">EscapeEnhander</a> (language-independent)</dt>
  1113. <dd class="dd1">
  1114. Switch '&lt;%= %&gt;' to escaped and '&lt;%== %&gt;' to unescaped.
  1115. </dd>
  1116. <dt class="dt1">
  1117. <a href="#stdout-enhancer">StdoutEnhancer</a> (only for Eruby)</dt>
  1118. <dd class="dd1">
  1119. Use $stdout instead of array buffer.
  1120. </dd>
  1121. <dt class="dt1">
  1122. <a href="#printout-enhancer">PrintOutEnhancer</a> (only for Eruby)</dt>
  1123. <dd class="dd1">
  1124. Use "print(...)" statement insead of "_buf &lt;&lt; ...".
  1125. </dd>
  1126. <dt class="dt1">
  1127. <a href="#printenabled-enhancer">PrintEnabledEnhancer</a> (only for Eruby)</dt>
  1128. <dd class="dd1">
  1129. Enable to use print() in '&lt;% ... %&gt;'.
  1130. </dd>
  1131. <dt class="dt1">
  1132. <a href="#array-enhancer">ArrayEnhancer</a> (only for Eruby)</dt>
  1133. <dd class="dd1">
  1134. Return array of string instead of returning string.
  1135. </dd>
  1136. <dt class="dt1">
  1137. <a href="#arraybuffer-enhancer">ArrayBufferEnhancer</a> (only for Eruby)</dt>
  1138. <dd class="dd1">
  1139. Use array buffer. It is a little slower than StringBufferEnhancer.
  1140. </dd>
  1141. <dt class="dt1">
  1142. <a href="#stringbuffer-enhancer">StringBufferEnhancer</a> (only for Eruby)</dt>
  1143. <dd class="dd1">
  1144. Use string buffer. This is included in Erubis::Eruby by default.
  1145. </dd>
  1146. <dt class="dt1">
  1147. <a href="#erbout-enhancer">ErboutEnhancer</a> (only for Eruby)</dt>
  1148. <dd class="dd1">
  1149. Set '_erbout = _buf = "";' to be compatible with ERB.
  1150. </dd>
  1151. <dt class="dt1">
  1152. <a href="#notext-enhancer">NoTextEnhancer</a> (language-independent)</dt>
  1153. <dd class="dd1">
  1154. Print embedded code only and ignore normal text.
  1155. </dd>
  1156. <dt class="dt1">
  1157. <a href="#nocode-enhancer">NoCodeEnhancer</a> (language-independent)</dt>
  1158. <dd class="dd1">
  1159. Print normal text only and ignore code.
  1160. </dd>
  1161. <dt class="dt1">
  1162. <a href="#simplify-enhancer">SimplifyEnhancer</a> (language-independent)</dt>
  1163. <dd class="dd1">
  1164. Make compile faster but don't trim spaces around '&lt;% %&gt;'.
  1165. </dd>
  1166. <dt class="dt1">
  1167. <a href="#bipattern-enhancer">BiPatternEnhancer</a> (language-independent)</dt>
  1168. <dd class="dd1">
  1169. [experimental] Enable to use another embedded pattern with '&lt;% %&gt;'.
  1170. </dd>
  1171. <dt class="dt1">
  1172. <a href="#percentline-enhancer">PercentLineEnhancer</a> (language-independent)</dt>
  1173. <dd class="dd1">
  1174. Regard lines starting with '%' as Ruby code. This is for compatibility with eruby and ERB.
  1175. </dd>
  1176. <dt class="dt1">
  1177. <a href="#headerfooter-enhancer">HeaderFooterEnhancer</a> (language-independent)</dt>
  1178. <dd class="dd1">
  1179. [experimental] Enable you to add header and footer in eRuby script.
  1180. </dd>
  1181. <dt class="dt1">
  1182. <a href="#interpolation-enhancer">InterpolationEnhancer</a> (only for Eruby)</dt>
  1183. <dd class="dd1">
  1184. [experimental] convert '&lt;p&gt;&lt;%= text %&gt;&lt;/p&gt;' into '_buf &lt;&lt; %Q`&lt;p&gt;#{text}&lt;/p&gt;`'.
  1185. </dd>
  1186. <dt class="dt1">
  1187. <a href="#deleteindent-enhancer">DeleteIndentEnhancer</a> (language-independent)</dt>
  1188. <dd class="dd1">
  1189. [experimental] delete indentation of HTML file and eliminate page size.
  1190. </dd>
  1191. </dl>
  1192. <p>If you required 'erubis/engine/enhanced', Eruby subclasses which include each enhancers are defined.
  1193. For example, class BiPatternEruby includes BiPatternEnhancer.
  1194. </p>
  1195. <a name="escape-enhancer"></a>
  1196. <h3 class="section2">EscapeEnhancer</h3>
  1197. <p>EscapeEnhancer switches '&lt;%= ... %&gt;' to escaped and '&lt;%== ... %&gt;' to unescaped.
  1198. </p>
  1199. <a name="example.eruby"></a>
  1200. <div class="program_caption">
  1201. example.eruby</div>
  1202. <pre class="program">&lt;div&gt;
  1203. &lt;% for item in list %&gt;
  1204. &lt;p&gt;&lt;%= item %&gt;&lt;/p&gt;
  1205. &lt;p&gt;&lt;%== item %&gt;&lt;/p&gt;
  1206. &lt;% end %&gt;
  1207. &lt;/div&gt;
  1208. </pre>
  1209. <a name="escape_example.result"></a>
  1210. <div class="terminal_caption">
  1211. compiled source code</div>
  1212. <pre class="terminal">$ erubis -xE Escape example.eruby
  1213. _buf = ''; _buf &lt;&lt; '&lt;div&gt;
  1214. '; for item in list
  1215. _buf &lt;&lt; ' &lt;p&gt;'; <strong>_buf &lt;&lt; Erubis::XmlHelper.escape_xml( item );</strong> _buf &lt;&lt; '&lt;/p&gt;
  1216. &lt;p&gt;'; <strong>_buf &lt;&lt; ( item ).to_s;</strong> _buf &lt;&lt; '&lt;/p&gt;
  1217. '; end
  1218. _buf &lt;&lt; '&lt;/div&gt;
  1219. ';
  1220. _buf.to_s
  1221. </pre>
  1222. <p>EscapeEnhancer is language-independent.
  1223. </p>
  1224. <br>
  1225. <a name="stdout-enhancer"></a>
  1226. <h3 class="section2">StdoutEnhancer</h3>
  1227. <p>StdoutEnhancer use $sdtdout instead of array buffer.
  1228. Therefore, you can use 'print' statement in embedded ruby code.
  1229. </p>
  1230. <a name="stdout_exmple.result"></a>
  1231. <div class="terminal_caption">
  1232. compiled source code</div>
  1233. <pre class="terminal">$ erubis -xE Stdout example.eruby
  1234. <strong>_buf = $stdout;</strong> _buf &lt;&lt; '&lt;div&gt;
  1235. '; for item in list
  1236. _buf &lt;&lt; ' &lt;p&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/p&gt;
  1237. &lt;p&gt;'; _buf &lt;&lt; Erubis::XmlHelper.escape_xml( item ); _buf &lt;&lt; '&lt;/p&gt;
  1238. '; end
  1239. _buf &lt;&lt; '&lt;/div&gt;
  1240. ';
  1241. <strong>''</strong>
  1242. </pre>
  1243. <p>StdoutEnhancer is only for Eruby.
  1244. </p>
  1245. <br>
  1246. <a name="printout-enhancer"></a>
  1247. <h3 class="section2">PrintOutEnhancer</h3>
  1248. <p>PrintOutEnhancer makes compiled source code to use 'print(...)' instead of '_buf &lt;&lt; ...'.
  1249. </p>
  1250. <a name="printstatement_example.result"></a>
  1251. <div class="terminal_caption">
  1252. compiled source code</div>
  1253. <pre class="terminal">$ erubis -xE PrintOut example.eruby
  1254. <strong>print</strong> '&lt;div&gt;
  1255. '; for item in list
  1256. <strong>print</strong> ' &lt;p&gt;'; <strong>print</strong>(( item ).to_s); <strong>print</strong> '&lt;/p&gt;
  1257. &lt;p&gt;'; <strong>print</strong> Erubis::XmlHelper.escape_xml( item ); <strong>print</strong> '&lt;/p&gt;
  1258. '; end
  1259. <strong>print</strong> '&lt;/div&gt;
  1260. ';
  1261. </pre>
  1262. <p>PrintOutEnhancer is only for Eruby.
  1263. </p>
  1264. <br>
  1265. <a name="printenabled-enhancer"></a>
  1266. <h3 class="section2">PrintEnabledEnhancer</h3>
  1267. <p>PrintEnabledEnhancer enables you to use print() method in '&lt;% ... %&gt;'.
  1268. </p>
  1269. <a name="printenabled-example.eruby"></a>
  1270. <div class="program_caption">
  1271. printenabled-example.eruby</div>
  1272. <pre class="program">&lt;% for item in @list %&gt;
  1273. &lt;b&gt;<strong>&lt;% print item %&gt;</strong>&lt;/b&gt;
  1274. &lt;% end %&gt;
  1275. </pre>
  1276. <a name="printenabled-example.rb"></a>
  1277. <div class="program_caption">
  1278. printenabled-example.rb</div>
  1279. <pre class="program">require 'erubis'
  1280. class PrintEnabledEruby &lt; Erubis::Eruby
  1281. include Erubis::PrintEnabledEnhancer
  1282. end
  1283. input = File.read('printenabled-example.eruby')
  1284. eruby = PrintEnabledEruby.new(input)
  1285. list = ['aaa', 'bbb', 'ccc']
  1286. print eruby.evaluate(:list=&gt;list)
  1287. </pre>
  1288. <a name="printenable_example.result"></a>
  1289. <div class="terminal_caption">
  1290. output result</div>
  1291. <pre class="terminal">$ ruby printenabled-example.rb
  1292. &lt;b&gt;aaa&lt;/b&gt;
  1293. &lt;b&gt;bbb&lt;/b&gt;
  1294. &lt;b&gt;ccc&lt;/b&gt;
  1295. </pre>
  1296. <p>Notice to use Eruby#evaluate() and not to use Eruby#result(),
  1297. because print() method in '&lt;% ... %&gt;' invokes not Kernel#print() but PrintEnabledEnhancer#print().
  1298. </p>
  1299. <p>PrintEnabledEnhancer is only for Eruby.
  1300. </p>
  1301. <br>
  1302. <a name="array-enhancer"></a>
  1303. <h3 class="section2">ArrayEnhancer</h3>
  1304. <p>ArrayEnhancer makes Eruby to return an array of strings.
  1305. </p>
  1306. <a name="array_example.result"></a>
  1307. <div class="terminal_caption">
  1308. compiled source code</div>
  1309. <pre class="terminal">$ erubis -xE Array example.eruby
  1310. <strong>_buf = [];</strong> _buf &lt;&lt; '&lt;div&gt;
  1311. '; for item in list
  1312. _buf &lt;&lt; ' &lt;p&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/p&gt;
  1313. &lt;p&gt;'; _buf &lt;&lt; Erubis::XmlHelper.escape_xml( item ); _buf &lt;&lt; '&lt;/p&gt;
  1314. '; end
  1315. _buf &lt;&lt; '&lt;/div&gt;
  1316. ';
  1317. <strong>_buf</strong>
  1318. </pre>
  1319. <p>ArrayEnhancer is only for Eruby.
  1320. </p>
  1321. <br>
  1322. <a name="arraybuffer-enhancer"></a>
  1323. <h3 class="section2">ArrayBufferEnhancer</h3>
  1324. <p>ArrayBufferEnhancer makes Eruby to use array buffer.
  1325. Array buffer is a litte slower than String buffer.
  1326. </p>
  1327. <p>ArrayBufferEnhancer is only for Eruby.
  1328. </p>
  1329. <a name="arraybuffer_example.result"></a>
  1330. <div class="terminal_caption">
  1331. compiled source code</div>
  1332. <pre class="terminal">$ erubis -xE ArrayBuffer example.eruby
  1333. <strong>_buf = [];</strong> _buf &lt;&lt; '&lt;div&gt;
  1334. '; for item in list
  1335. _buf &lt;&lt; ' &lt;p&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/p&gt;
  1336. &lt;p&gt;'; _buf &lt;&lt; Erubis::XmlHelper.escape_xml( item ); _buf &lt;&lt; '&lt;/p&gt;
  1337. '; end
  1338. _buf &lt;&lt; '&lt;/div&gt;
  1339. ';
  1340. <strong>_buf.join</strong>
  1341. </pre>
  1342. <br>
  1343. <a name="stringbuffer-enhancer"></a>
  1344. <h3 class="section2">StringBufferEnhancer</h3>
  1345. <p>StringBufferEnhancer makes Eruby to use string buffer.
  1346. String buffer is a little faster than array buffer.
  1347. Erubis::Eruby includes this enhancer by default.
  1348. </p>
  1349. <a name="stringbuffer_example.result"></a>
  1350. <div class="terminal_caption">
  1351. compiled source code</div>
  1352. <pre class="terminal">$ erubis -xE StringBuffer example.eruby
  1353. <strong>_buf = '';</strong> _buf &lt;&lt; '&lt;div&gt;
  1354. '; for item in list
  1355. _buf &lt;&lt; ' &lt;p&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/p&gt;
  1356. &lt;p&gt;'; _buf &lt;&lt; Erubis::XmlHelper.escape_xml( item ); _buf &lt;&lt; '&lt;/p&gt;
  1357. '; end
  1358. _buf &lt;&lt; '&lt;/div&gt;
  1359. ';
  1360. <strong>_buf.to_s</strong>
  1361. </pre>
  1362. <p>StringBufferEnhancer is only for Eruby.
  1363. </p>
  1364. <br>
  1365. <a name="erbout-enhancer"></a>
  1366. <h3 class="section2">ErboutEnhancer</h3>
  1367. <p>ErboutEnhancer makes Eruby to be compatible with ERB.
  1368. This is useful especially for Ruby on Rails.
  1369. </p>
  1370. <div class="terminal_caption">
  1371. compiled source code</div>
  1372. <pre class="terminal">$ erubis -xE Erbout example.eruby
  1373. <strong>_erbout = _buf = '';</strong> _buf &lt;&lt; '&lt;div&gt;
  1374. '; for item in list
  1375. _buf &lt;&lt; ' &lt;p&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/p&gt;
  1376. &lt;p&gt;'; _buf &lt;&lt; Erubis::XmlHelper.escape_xml( item ); _buf &lt;&lt; '&lt;/p&gt;
  1377. '; end
  1378. _buf &lt;&lt; '&lt;/div&gt;
  1379. ';
  1380. _buf.to_s
  1381. </pre>
  1382. <p>ErboutEnhancer is only for Eruby.
  1383. </p>
  1384. <br>
  1385. <a name="notext-enhancer"></a>
  1386. <h3 class="section2">NoTextEnhancer</h3>
  1387. <p>NoTextEnhancer suppress output of text and prints only embedded code.
  1388. This is useful especially when debugging a complex eRuby script.
  1389. </p>
  1390. <a name="notext-example.eruby"></a>
  1391. <div class="program_caption">
  1392. notext-example.eruby</div>
  1393. <pre class="program">&lt;h3&gt;List&lt;/h3&gt;
  1394. &lt;% if !@list || @list.empty? %&gt;
  1395. &lt;p&gt;not found.&lt;/p&gt;
  1396. &lt;% else %&gt;
  1397. &lt;table&gt;
  1398. &lt;tbody&gt;
  1399. &lt;% @list.each_with_index do |item, i| %&gt;
  1400. &lt;tr bgcolor="&lt;%= i%2 == 0 ? '#FFCCCC' : '#CCCCFF' %&gt;"&gt;
  1401. &lt;td&gt;&lt;%= item %&gt;&lt;/td&gt;
  1402. &lt;/tr&gt;
  1403. &lt;% end %&gt;
  1404. &lt;/tbody&gt;
  1405. &lt;/table&gt;
  1406. &lt;% end %&gt;
  1407. </pre>
  1408. <a name="notext_example.result"></a>
  1409. <div class="terminal_caption">
  1410. output example of NoTextEnhancer</div>
  1411. <pre class="terminal">$ erubis -xE NoText notext-example.eruby
  1412. _buf = '';
  1413. if !@list || @list.empty?
  1414. else
  1415. @list.each_with_index do |item, i|
  1416. _buf &lt;&lt; ( i%2 == 0 ? '#FFCCCC' : '#CCCCFF' ).to_s;
  1417. _buf &lt;&lt; ( item ).to_s;
  1418. end
  1419. end
  1420. _buf.to_s
  1421. </pre>
  1422. <p>NoTextEnhancer is language-independent. It is useful even if you are PHP user, see <a href="#topics-php">this section</a>.
  1423. </p>
  1424. <br>
  1425. <a name="nocode-enhancer"></a>
  1426. <h3 class="section2">NoCodeEnhancer</h3>
  1427. <p>NoCodeEnhancer suppress output of embedded code and prints only normal text.
  1428. This is useful especially when validating HTML tags.
  1429. </p>
  1430. <a name="nocode-example.eruby"></a>
  1431. <div class="program_caption">
  1432. nocode-example.eruby</div>
  1433. <pre class="program">&lt;h3&gt;List&lt;/h3&gt;
  1434. &lt;% if !@list || @list.empty? %&gt;
  1435. &lt;p&gt;not found.&lt;/p&gt;
  1436. &lt;% else %&gt;
  1437. &lt;table&gt;
  1438. &lt;tbody&gt;
  1439. &lt;% @list.each_with_index do |item, i| %&gt;
  1440. &lt;tr bgcolor="&lt;%= i%2 == 0 ? '#FFCCCC' : '#CCCCFF' %&gt;"&gt;
  1441. &lt;td&gt;&lt;%= item %&gt;&lt;/td&gt;
  1442. &lt;/tr&gt;
  1443. &lt;% end %&gt;
  1444. &lt;/tbody&gt;
  1445. &lt;/table&gt;
  1446. &lt;% end %&gt;
  1447. </pre>
  1448. <a name="nocode_example.result"></a>
  1449. <div class="terminal_caption">
  1450. output example of NoCodeEnhancer</div>
  1451. <pre class="terminal">$ erubis -xE NoCode notext-example.eruby
  1452. &lt;h3&gt;List&lt;/h3&gt;
  1453. &lt;p&gt;not found.&lt;/p&gt;
  1454. &lt;table&gt;
  1455. &lt;tbody&gt;
  1456. &lt;tr bgcolor=""&gt;
  1457. &lt;td&gt;&lt;/td&gt;
  1458. &lt;/tr&gt;
  1459. &lt;/tbody&gt;
  1460. &lt;/table&gt;
  1461. </pre>
  1462. <p>NoCodeEnhancer is language-independent. It is useful even if you are PHP user, see <a href="#topics-php">this section</a>.
  1463. </p>
  1464. <br>
  1465. <a name="simplify-enhancer"></a>
  1466. <h3 class="section2">SimplifyEnhancer</h3>
  1467. <p>SimplifyEnhancer makes compiling a little faster but don't trim spaces around '&lt;% %&gt;'.
  1468. </p>
  1469. <a name="simplify_example.result"></a>
  1470. <div class="terminal_caption">
  1471. compiled source code</div>
  1472. <pre class="terminal">$ erubis -xE Simplify example.eruby
  1473. _buf = ''; _buf &lt;&lt; '&lt;div&gt;
  1474. '; for item in list ; _buf &lt;&lt; '
  1475. &lt;p&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/p&gt;
  1476. &lt;p&gt;'; _buf &lt;&lt; Erubis::XmlHelper.escape_xml( item ); _buf &lt;&lt; '&lt;/p&gt;
  1477. '; end ; _buf &lt;&lt; '
  1478. &lt;/div&gt;
  1479. ';
  1480. _buf.to_s
  1481. </pre>
  1482. <p>SimplifyEnhancer is language-independent.
  1483. </p>
  1484. <br>
  1485. <a name="bipattern-enhancer"></a>
  1486. <h3 class="section2">BiPatternEnhancer</h3>
  1487. <p>BiPatternEnhancer enables to use another embedded pattern with '&lt;% %&gt;'.
  1488. By Default, '[= ... =]' is available for expression.
  1489. You can specify pattern by :bipattern property.
  1490. </p>
  1491. <a name="bipattern-example.rhtml"></a>
  1492. <div class="program_caption">
  1493. bipattern-example.rhtml</div>
  1494. <pre class="program">&lt;% for item in list %&gt;
  1495. &lt;b&gt;<strong>[= item =]</strong>&lt;/b&gt;
  1496. &lt;b&gt;<strong>[== item =]</strong>&lt;/b&gt;
  1497. &lt;% end %&gt;
  1498. </pre>
  1499. <a name="bipattern_example.result"></a>
  1500. <div class="terminal_caption">
  1501. compiled source code</div>
  1502. <pre class="terminal">$ erubis -xE BiPattern bipattern-example.rhtml
  1503. _buf = ''; for item in list
  1504. _buf &lt;&lt; ' &lt;b&gt;'; <strong>_buf &lt;&lt; ( item ).to_s;</strong> _buf &lt;&lt; '&lt;/b&gt;
  1505. &lt;b&gt;'; <strong>_buf &lt;&lt; Erubis::XmlHelper.escape_xml( item );</strong> _buf &lt;&lt; '&lt;/b&gt;
  1506. '; end
  1507. _buf.to_s
  1508. </pre>
  1509. <p>BiPatternEnhancer is language-independent.
  1510. </p>
  1511. <br>
  1512. <a name="percentline-enhancer"></a>
  1513. <h3 class="section2">PercentLineEnhancer</h3>
  1514. <p>PercentLineEnhancer regards lines starting with '%' as Ruby code.
  1515. This is for compatibility with eruby and ERB.
  1516. </p>
  1517. <a name="percentline-example.rhtml"></a>
  1518. <div class="program_caption">
  1519. percentline-example.rhtml</div>
  1520. <pre class="program">&lt;ul&gt;
  1521. <strong>% for item in list</strong>
  1522. &lt;li&gt;&lt;%= item %&gt;&lt;/li&gt;
  1523. <strong>% end</strong>
  1524. &lt;/ul&gt;
  1525. <strong>%% lines with '%%'</strong>
  1526. </pre>
  1527. <a name="percentline_example.result"></a>
  1528. <div class="terminal_caption">
  1529. compiled source code</div>
  1530. <pre class="terminal">$ erubis -xE PercentLine percentline-example.rhtml
  1531. _buf = ''; _buf &lt;&lt; '&lt;ul&gt;
  1532. '; <strong>for item in list</strong>
  1533. _buf &lt;&lt; ' &lt;li&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/li&gt;
  1534. '; <strong>end</strong>
  1535. _buf &lt;&lt; '&lt;/ul&gt;
  1536. <strong>% lines with \'%%\'</strong>
  1537. ';
  1538. _buf.to_s
  1539. </pre>
  1540. <p>PercentLineEnhancer is language-independent.
  1541. </p>
  1542. <br>
  1543. <a name="prefixedline-enhancer"></a>
  1544. <h3 class="section2">PrefixedLineEnhancer</h3>
  1545. <p>PrefixedlineEnhancer regards lines starting with '%' as Ruby code.
  1546. It is similar to <a href="#percentline-enhancer">PercentLineEnhancer</a>, but there are some differences.
  1547. </p>
  1548. <ul type="disc">
  1549. <li>PrefixedlineEnhancer allows to indent lines starting with '%', but PercentLineEnhancer doesn't.
  1550. </li>
  1551. <li>PrefixedlineEnhancer allows to change prefixed character (default '%'), but PercentLineEnhancer doesn't.
  1552. </li>
  1553. </ul>
  1554. <a name="prefixedline-example.rhtml"></a>
  1555. <div class="program_caption">
  1556. prefixedline-example.rhtml</div>
  1557. <pre class="program">&lt;ul&gt;
  1558. <strong>! for item in list</strong>
  1559. &lt;li&gt;&lt;%= item %&gt;&lt;/li&gt;
  1560. <strong>! end</strong>
  1561. &lt;/ul&gt;
  1562. <strong>!! lines with '!!'</strong>
  1563. </pre>
  1564. <a name="prefixedline-example.rb"></a>
  1565. <div class="program_caption">
  1566. prefixedline-example.rb</div>
  1567. <pre class="program">require 'erubis'
  1568. class PrefixedLineEruby &lt; Erubis::Eruby
  1569. include Erubis::PrefixedLineEnhancer
  1570. end
  1571. input = File.read('prefixedline-example.rhtml')
  1572. eruby = PrefixedLineEruby.new(input, :prefixchar=&gt;'!') # default '%'
  1573. print eruby.src
  1574. </pre>
  1575. <a name="prefixedline_example.result"></a>
  1576. <div class="terminal_caption">
  1577. compiled source code</div>
  1578. <pre class="terminal">$ ruby prefixedline-example.rb
  1579. _buf = ''; _buf &lt;&lt; '&lt;ul&gt;
  1580. '; <strong>for item in list</strong>
  1581. _buf &lt;&lt; ' &lt;li&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/li&gt;
  1582. '; <strong>end</strong>
  1583. _buf &lt;&lt; '&lt;/ul&gt;
  1584. <strong>! lines with \'!!\'</strong>
  1585. ';
  1586. _buf.to_s
  1587. </pre>
  1588. <p>PrefixedLineEnhancer is language-independent.
  1589. </p>
  1590. <br>
  1591. <a name="headerfooter-enhancer"></a>
  1592. <h3 class="section2">HeaderFooterEnhancer</h3>
  1593. <p>[experimental]
  1594. </p>
  1595. <p>HeaderFooterEnhancer enables you to add header and footer in eRuby script.
  1596. </p>
  1597. <a name="headerfooter-example.eruby"></a>
  1598. <div class="program_caption">
  1599. headerfooter-example.eruby</div>
  1600. <pre class="program"><strong>&lt;!--#header:</strong>
  1601. <strong>def list_items(items)</strong>
  1602. <strong>#--&gt;</strong>
  1603. &lt;% for item in items %&gt;
  1604. &lt;b&gt;&lt;%= item %&gt;&lt;/b&gt;
  1605. &lt;% end %&gt;
  1606. <strong>&lt;!--#footer:</strong>
  1607. <strong>end</strong>
  1608. <strong>#--&gt;</strong>
  1609. </pre>
  1610. <a name="headerfooter_example.result"></a>
  1611. <div class="terminal_caption">
  1612. compiled source code</div>
  1613. <pre class="terminal">$ erubis -xE HeaderFooter headerfooter-example.eruby
  1614. <strong>def list_items(items)</strong>
  1615. _buf = ''; for item in items
  1616. _buf &lt;&lt; ' &lt;b&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/b&gt;
  1617. '; end
  1618. _buf.to_s
  1619. <strong>end</strong>
  1620. </pre>
  1621. <p>Compare to the following:
  1622. </p>
  1623. <a name="normal-eruby-test.eruby"></a>
  1624. <div class="program_caption">
  1625. normal-eruby-test.eruby</div>
  1626. <pre class="program"><strong>&lt;%</strong>
  1627. <strong>def list_items(items)</strong>
  1628. <strong>%&gt;</strong>
  1629. &lt;% for item in items %&gt;
  1630. &lt;li&gt;&lt;%= item %&gt;&lt;/li&gt;
  1631. &lt;% end %&gt;
  1632. <strong>&lt;%</strong>
  1633. <strong>end</strong>
  1634. <strong>%&gt;</strong>
  1635. </pre>
  1636. <a name="normal_eruby_test.result"></a>
  1637. <div class="terminal_caption">
  1638. compiled source code</div>
  1639. <pre class="terminal">$ erubis -x normal-eruby-test.eruby
  1640. _buf = '';
  1641. <strong>def list_items(items)</strong>
  1642. for item in items
  1643. _buf &lt;&lt; '&lt;li&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/li&gt;
  1644. '; end
  1645. <strong>end</strong>
  1646. _buf.to_s
  1647. </pre>
  1648. <p>Header and footer can be in any position in eRuby script,
  1649. that is, header is no need to be in the head of eRuby script.
  1650. </p>
  1651. <a name="headerfooter-example2.rhtml"></a>
  1652. <div class="program_caption">
  1653. headerfooter-example2.rhtml</div>
  1654. <pre class="program">&lt;?xml version="1.0"?&gt;
  1655. &lt;html&gt;
  1656. <strong>&lt;!--#header:</strong>
  1657. <strong>def page(list)</strong>
  1658. <strong>#--&gt;</strong>
  1659. :
  1660. <strong>&lt;!--#footer:</strong>
  1661. <strong>end</strong>
  1662. <strong>#--&gt;</strong>
  1663. &lt;/html&gt;
  1664. </pre>
  1665. <a name="headerfooter_example2.result"></a>
  1666. <div class="terminal_caption">
  1667. compiled source code</div>
  1668. <pre class="terminal">$ erubis -xE HeaderFooter headerfooter-example2.rhtml
  1669. <strong>def page(list)</strong>
  1670. _buf = ''; _buf &lt;&lt; '&lt;?xml version="1.0"?&gt;
  1671. &lt;html&gt;
  1672. '; _buf &lt;&lt; ' :
  1673. '; _buf &lt;&lt; '&lt;/html&gt;
  1674. ';
  1675. _buf.to_s
  1676. <strong>end</strong>
  1677. </pre>
  1678. <p>HeaderFooterEnhancer is experimental and is language-independent.
  1679. </p>
  1680. <br>
  1681. <a name="interpolation-enhancer"></a>
  1682. <h3 class="section2">InterpolationEnhancer</h3>
  1683. <p>[experimental]
  1684. </p>
  1685. <p>InterpolationEnhancer converts "&lt;h1&gt;&lt;%= title %&gt;&lt;/h1&gt;" into
  1686. "_buf &lt;&lt; %Q`&lt;h1&gt;#{ title }&lt;/h1&gt;`".
  1687. This makes Eruby a litter faster because method call of String#&lt;&lt; are eliminated
  1688. by expression interpolations.
  1689. </p>
  1690. <div class="program_caption">
  1691. InterpolationEnhancer elmininates method call of String#&lt;&lt;.</div>
  1692. <pre class="program">## Assume that input is '&lt;a href="&lt;%=url%&gt;"&gt;&lt;%=name%&gt;&lt;/a&gt;'.
  1693. ## Eruby convert input into the following code. String#&lt;&lt; is called 5 times.
  1694. _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;';
  1695. ## If InterpolationEnhancer is used, String#&lt;&lt; is called only once.
  1696. _buf &lt;&lt; %Q`&lt;a href="#{url}"&gt;#{name}&lt;/a&gt;`;
  1697. </pre>
  1698. <a name="interpolation_example.result"></a>
  1699. <div class="terminal_caption">
  1700. compiled source code</div>
  1701. <pre class="terminal">$ erubis -xE Interpolation example.eruby
  1702. _buf = ''; _buf &lt;&lt; <strong>%Q`</strong>&lt;div&gt;\n<strong>`</strong>
  1703. for item in list
  1704. _buf &lt;&lt; <strong>%Q`</strong> &lt;p&gt;<strong>#{ item }</strong>&lt;/p&gt;
  1705. &lt;p&gt;<strong>#{Erubis::XmlHelper.escape_xml( item )}</strong>&lt;/p&gt;\n<strong>`</strong>
  1706. end
  1707. _buf &lt;&lt; <strong>%Q`</strong>&lt;/div&gt;\n<strong>`</strong>
  1708. _buf.to_s
  1709. </pre>
  1710. <p>Erubis provides Erubis::FastEruby class which includes InterpolationEnhancer.
  1711. You can use Erubis::FastEruby class instead of Erubis::Eruby class.
  1712. </p>
  1713. <p>InterpolationEnhancer is only for Eruby.
  1714. </p>
  1715. <br>
  1716. <a name="deleteindent-enhancer"></a>
  1717. <h3 class="section2">DeleteIndentEnhancer</h3>
  1718. <p>[experimental]
  1719. DeleteIndentEnhancer deletes indentation of HTML file.
  1720. </p>
  1721. <a name="interpolation_example.result"></a>
  1722. <div class="terminal_caption">
  1723. compiled source code</div>
  1724. <pre class="terminal">$ erubis -xE DeleteIndent example.eruby
  1725. _buf = ''; _buf &lt;&lt; '&lt;div&gt;
  1726. '; for item in list
  1727. _buf &lt;&lt; '&lt;p&gt;'; _buf &lt;&lt; ( item ).to_s; _buf &lt;&lt; '&lt;/p&gt;
  1728. &lt;p&gt;'; _buf &lt;&lt; Erubis::XmlHelper.escape_xml( item ); _buf &lt;&lt; '&lt;/p&gt;
  1729. '; end
  1730. _buf &lt;&lt; '&lt;/div&gt;
  1731. ';
  1732. _buf.to_s
  1733. </pre>
  1734. <p>Notice that DeleteIndentEnhancer isn't intelligent.
  1735. It deletes indentations even if they are in &lt;PRE&gt;&lt;/PRE&gt;.
  1736. </p>
  1737. <p>DeleteIndentEnhancer is language-independent.
  1738. </p>
  1739. <br>
  1740. <br>
  1741. <a name="lang"></a>
  1742. <h2 class="section1">Multi-Language Support</h2>
  1743. <p>Erubis supports the following languages<sup>(<a href="#fnref:2" name="fnlink:2">*2</a>)</sup>:
  1744. </p>
  1745. <ul type="disc">
  1746. <li>Ruby
  1747. </li>
  1748. <li><a href="#lang-php">PHP</a>
  1749. </li>
  1750. <li><a href="#lang-c">C</a>
  1751. </li>
  1752. <li><a href="#lang-java">Java</a>
  1753. </li>
  1754. <li><a href="#lang-scheme">Scheme</a>
  1755. </li>
  1756. <li><a href="#lang-perl">Perl</a>
  1757. </li>
  1758. <li><a href="#lang-javascript">JavaScript</a>
  1759. </li>
  1760. </ul>
  1761. <div class="footnote">
  1762. <dl compact>
  1763. <dt>(<a name="fnref:2" href="#fnlink:2">*2</a>)</dt>
  1764. <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>
  1765. </dl>
  1766. </div>
  1767. <a name="lang-php"></a>
  1768. <h3 class="section2">PHP</h3>
  1769. <a name="example.ephp"></a>
  1770. <div class="program_caption">
  1771. example.ephp</div>
  1772. <pre class="program">&lt;?xml version="1.0"?&gt;
  1773. &lt;html&gt;
  1774. &lt;body&gt;
  1775. &lt;p&gt;Hello <strong>&lt;%= $user %&gt;</strong>!&lt;/p&gt;
  1776. &lt;table&gt;
  1777. &lt;tbody&gt;
  1778. <strong>&lt;% $i = 0; %&gt;</strong>
  1779. <strong>&lt;% foreach ($list as $item) { %&gt;</strong>
  1780. <strong>&lt;% $i++; %&gt;</strong>
  1781. &lt;tr bgcolor=<strong>"&lt;%= $i % 2 == 0 ? '#FFCCCC' : '#CCCCFF' %&gt;"</strong>&gt;
  1782. &lt;td&gt;<strong>&lt;%= $i %&gt;</strong>&lt;/td&gt;
  1783. &lt;td&gt;<strong>&lt;%== $item %&gt;</strong>&lt;/td&gt;
  1784. &lt;/tr&gt;
  1785. <strong>&lt;% } %&gt;</strong>
  1786. &lt;/tbody&gt;
  1787. &lt;/table&gt;
  1788. &lt;/body&gt;
  1789. &lt;/html&gt;
  1790. </pre>
  1791. <a name="example_php.result"></a>
  1792. <div class="terminal_caption">
  1793. compiled source code</div>
  1794. <pre class="terminal">$ erubis -l php example.ephp
  1795. &lt;&lt;?php ?&gt;?xml version="1.0"?&gt;
  1796. &lt;html&gt;
  1797. &lt;body&gt;
  1798. &lt;p&gt;Hello &lt;?php echo $user; ?&gt;!&lt;/p&gt;
  1799. &lt;table&gt;
  1800. &lt;tbody&gt;
  1801. &lt;?php $i = 0; ?&gt;
  1802. &lt;?php foreach ($list as $item) { ?&gt;
  1803. &lt;?php $i++; ?&gt;
  1804. &lt;tr bgcolor="&lt;?php echo $i % 2 == 0 ? '#FFCCCC' : '#CCCCFF'; ?&gt;"&gt;
  1805. &lt;td&gt;&lt;?php echo $i; ?&gt;&lt;/td&gt;
  1806. &lt;td&gt;&lt;?php echo htmlspecialchars($item); ?&gt;&lt;/td&gt;
  1807. &lt;/tr&gt;
  1808. &lt;?php } ?&gt;
  1809. &lt;/tbody&gt;
  1810. &lt;/table&gt;
  1811. &lt;/body&gt;
  1812. &lt;/html&gt;
  1813. </pre>
  1814. <br>
  1815. <a name="lang-c"></a>
  1816. <h3 class="section2">C</h3>
  1817. <a name="example.ec"></a>
  1818. <div class="program_caption">
  1819. example.ec</div>
  1820. <pre class="program"><strong>&lt;%
  1821. #include &lt;stdio.h&gt;
  1822. int main(int argc, char *argv[])
  1823. {
  1824. int i;
  1825. %&gt;</strong>
  1826. &lt;html&gt;
  1827. &lt;body&gt;
  1828. &lt;p&gt;Hello <strong>&lt;%= "%s", argv[0] %&gt;</strong>!&lt;/p&gt;
  1829. &lt;table&gt;
  1830. &lt;tbody&gt;
  1831. <strong>&lt;% for (i = 1; i &lt; argc; i++) { %&gt;</strong>
  1832. &lt;tr bgcolor="<strong>&lt;%= i % 2 == 0 ? "#FFCCCC" : "#CCCCFF" %&gt;</strong>"&gt;
  1833. &lt;td&gt;<strong>&lt;%= "%d", i %&gt;</strong>&lt;/td&gt;
  1834. &lt;td&gt;<strong>&lt;%= "%s", argv[i] %&gt;</strong>&lt;/td&gt;
  1835. &lt;/tr&gt;
  1836. <strong>&lt;% } %&gt;</strong>
  1837. &lt;/tbody&gt;
  1838. &lt;/table&gt;
  1839. &lt;/body&gt;
  1840. &lt;/html&gt;
  1841. <strong>&lt;%
  1842. return 0;
  1843. }
  1844. %&gt;</strong>
  1845. </pre>
  1846. <a name="example_c.result"></a>
  1847. <div class="terminal_caption">
  1848. compiled source code</div>
  1849. <pre class="terminal">$ erubis -l c example.ec
  1850. #line 1 "example.ec"
  1851. #include &lt;stdio.h&gt;
  1852. int main(int argc, char *argv[])
  1853. {
  1854. int i;
  1855. fputs("&lt;html&gt;\n"
  1856. " &lt;body&gt;\n"
  1857. " &lt;p&gt;Hello ", stdout); fprintf(stdout, "%s", argv[0]); fputs("!&lt;/p&gt;\n"
  1858. " &lt;table&gt;\n"
  1859. " &lt;tbody&gt;\n", stdout);
  1860. for (i = 1; i &lt; argc; i++) {
  1861. fputs(" &lt;tr bgcolor=\"", stdout); fprintf(stdout, i % 2 == 0 ? "#FFCCCC" : "#CCCCFF"); fputs("\"&gt;\n"
  1862. " &lt;td&gt;", stdout); fprintf(stdout, "%d", i); fputs("&lt;/td&gt;\n"
  1863. " &lt;td&gt;", stdout); fprintf(stdout, "%s", argv[i]); fputs("&lt;/td&gt;\n"
  1864. " &lt;/tr&gt;\n", stdout);
  1865. }
  1866. fputs(" &lt;/tbody&gt;\n"
  1867. " &lt;/table&gt;\n"
  1868. " &lt;/body&gt;\n"
  1869. "&lt;/html&gt;\n", stdout);
  1870. return 0;
  1871. }
  1872. </pre>
  1873. <br>
  1874. <a name="lang-cpp"></a>
  1875. <h3 class="section2">C++</h3>
  1876. <a name="example.ecpp"></a>
  1877. <div class="program_caption">
  1878. example.ecpp</div>
  1879. <pre class="program"><strong>&lt;%
  1880. #include &lt;string&gt;
  1881. #include &lt;iostream&gt;
  1882. #include &lt;sstream&gt;
  1883. int main(int argc, char *argv[])
  1884. {
  1885. std::stringstream _buf;
  1886. %&gt;</strong>
  1887. &lt;html&gt;
  1888. &lt;body&gt;
  1889. &lt;p&gt;Hello <strong>&lt;%= argv[0] %&gt;</strong>!&lt;/p&gt;
  1890. &lt;table&gt;
  1891. &lt;tbody&gt;
  1892. <strong>&lt;% for (int i = 1; i &lt; argc; i++) { %&gt;</strong>
  1893. &lt;tr bgcolor="<strong>&lt;%= i % 2 == 0 ? "#FFCCCC" : "#CCCCFF" %&gt;</strong>"&gt;
  1894. &lt;td&gt;<strong>&lt;%= i %&gt;</strong>&lt;/td&gt;
  1895. &lt;td&gt;<strong>&lt;%= argv[i] %&gt;</strong>&lt;/td&gt;
  1896. &lt;/tr&gt;
  1897. <strong>&lt;% } %&gt;</strong>
  1898. &lt;/tbody&gt;
  1899. &lt;/table&gt;
  1900. &lt;/body&gt;
  1901. &lt;/html&gt;
  1902. <strong>&lt;%
  1903. std::string output = _buf.str();
  1904. std::cout &lt;&lt; output;
  1905. return 0;
  1906. }
  1907. %&gt;</strong>
  1908. </pre>
  1909. <a name="example_c.result"></a>
  1910. <div class="terminal_caption">
  1911. compiled source code</div>
  1912. <pre class="terminal">$ erubis -l cpp example.ecpp
  1913. #line 1 "example.ecpp"
  1914. #include &lt;string&gt;
  1915. #include &lt;iostream&gt;
  1916. #include &lt;sstream&gt;
  1917. int main(int argc, char *argv[])
  1918. {
  1919. std::stringstream _buf;
  1920. _buf &lt;&lt; "&lt;html&gt;\n"
  1921. " &lt;body&gt;\n"
  1922. " &lt;p&gt;Hello "; _buf &lt;&lt; (argv[0]); _buf &lt;&lt; "!&lt;/p&gt;\n"
  1923. " &lt;table&gt;\n"
  1924. " &lt;tbody&gt;\n";
  1925. for (int i = 1; i &lt; argc; i++) {
  1926. _buf &lt;&lt; " &lt;tr bgcolor=\""; _buf &lt;&lt; (i % 2 == 0 ? "#FFCCCC" : "#CCCCFF"); _buf &lt;&lt; "\"&gt;\n"
  1927. " &lt;td&gt;"; _buf &lt;&lt; (i); _buf &lt;&lt; "&lt;/td&gt;\n"
  1928. " &lt;td&gt;"; _buf &lt;&lt; (argv[i]); _buf &lt;&lt; "&lt;/td&gt;\n"
  1929. " &lt;/tr&gt;\n";
  1930. }
  1931. _buf &lt;&lt; " &lt;/tbody&gt;\n"
  1932. " &lt;/table&gt;\n"
  1933. " &lt;/body&gt;\n"
  1934. "&lt;/html&gt;\n";
  1935. std::string output = _buf.str();
  1936. std::cout &lt;&lt; output;
  1937. return 0;
  1938. }
  1939. </pre>
  1940. <br>
  1941. <a name="lang-java"></a>
  1942. <h3 class="section2">Java</h3>
  1943. <a name="Example.ejava"></a>
  1944. <div class="program_caption">
  1945. Example.ejava</div>
  1946. <pre class="program"><strong>&lt;%
  1947. import java.util.*;
  1948. public class Example {
  1949. private String user;
  1950. private String[] list;
  1951. public example(String user, String[] list) {
  1952. this.user = user;
  1953. this.list = list;
  1954. }
  1955. public String view() {
  1956. StringBuffer _buf = new StringBuffer();
  1957. %&gt;</strong>
  1958. &lt;html&gt;
  1959. &lt;body&gt;
  1960. &lt;p&gt;Hello <strong>&lt;%= user %&gt;</strong>!&lt;/p&gt;
  1961. &lt;table&gt;
  1962. &lt;tbody&gt;
  1963. <strong>&lt;% for (int i = 0; i &lt; list.length; i++) { %&gt;</strong>
  1964. &lt;tr bgcolor=<strong>"&lt;%= i % 2 == 0 ? "#FFCCCC" : "#CCCCFF" %&gt;"</strong>&gt;
  1965. &lt;td&gt;<strong>&lt;%= i + 1 %&gt;</strong>&lt;/td&gt;
  1966. &lt;td&gt;<strong>&lt;%== list[i] %&gt;</strong>&lt;/td&gt;
  1967. &lt;/tr&gt;
  1968. <strong>&lt;% } %&gt;</strong>
  1969. &lt;/tbody&gt;
  1970. &lt;/table&gt;
  1971. &lt;body&gt;
  1972. &lt;/html&gt;
  1973. <strong>&lt;%
  1974. return _buf.toString();
  1975. }
  1976. public static void main(String[] args) {
  1977. String[] list = { "&lt;aaa&gt;", "b&amp;b", "\"ccc\"" };
  1978. Example ex = Example.new("Erubis", list);
  1979. System.out.print(ex.view());
  1980. }
  1981. public static String escape(String s) {
  1982. StringBuffer sb = new StringBuffer();
  1983. for (int i = 0; i &lt; s.length(); i++) {
  1984. char ch = s.charAt(i);
  1985. switch (ch) {
  1986. case '&lt;': sb.append("&amp;lt;"); break;
  1987. case '&gt;': sb.append("&amp;gt;"); break;
  1988. case '&amp;': sb.append("&amp;amp;"); break;
  1989. case '"': sb.append("&amp;quot;"); break;
  1990. default: sb.append(ch);
  1991. }
  1992. }
  1993. return sb.toString();
  1994. }
  1995. }
  1996. %&gt;</strong>
  1997. </pre>
  1998. <a name="example_java.result"></a>
  1999. <div class="terminal_caption">
  2000. compiled source code</div>
  2001. <pre class="terminal">$ erubis -b -l java example.ejava
  2002. import java.util.*;
  2003. public class Example {
  2004. private String user;
  2005. private String[] list;
  2006. public example(String user, String[] list) {
  2007. this.user = user;
  2008. this.list = list;
  2009. }
  2010. public String view() {
  2011. StringBuffer _buf = new StringBuffer();
  2012. _buf.append("&lt;html&gt;\n"
  2013. + " &lt;body&gt;\n"
  2014. + " &lt;p&gt;Hello "); _buf.append(user); _buf.append("!&lt;/p&gt;\n"
  2015. + " &lt;table&gt;\n"
  2016. + " &lt;tbody&gt;\n");
  2017. for (int i = 0; i &lt; list.length; i++) {
  2018. _buf.append(" &lt;tr bgcolor=\""); _buf.append(i % 2 == 0 ? "#FFCCCC" : "#CCCCFF"); _buf.append("\"&gt;\n"
  2019. + " &lt;td&gt;"); _buf.append(i + 1); _buf.append("&lt;/td&gt;\n"
  2020. + " &lt;td&gt;"); _buf.append(escape(list[i])); _buf.append("&lt;/td&gt;\n"
  2021. + " &lt;/tr&gt;\n");
  2022. }
  2023. _buf.append(" &lt;/tbody&gt;\n"
  2024. + " &lt;/table&gt;\n"
  2025. + " &lt;body&gt;\n"
  2026. + "&lt;/html&gt;\n");
  2027. return _buf.toString();
  2028. }
  2029. public static void main(String[] args) {
  2030. String[] list = { "&lt;aaa&gt;", "b&amp;b", "\"ccc\"" };
  2031. Example ex = Example.new("Erubis", list);
  2032. System.out.print(ex.view());
  2033. }
  2034. public static String escape(String s) {
  2035. StringBuffer sb = new StringBuffer();
  2036. for (int i = 0; i &lt; s.length(); i++) {
  2037. char ch = s.charAt(i);
  2038. switch (ch) {
  2039. case '&lt;': sb.append("&amp;lt;"); break;
  2040. case '&gt;': sb.append("&amp;gt;"); break;
  2041. case '&amp;': sb.append("&amp;amp;"); break;
  2042. case '"': sb.append("&amp;quot;"); break;
  2043. default: sb.append(ch);
  2044. }
  2045. }
  2046. return sb.toString();
  2047. }
  2048. }
  2049. </pre>
  2050. <br>
  2051. <a name="lang-scheme"></a>
  2052. <h3 class="section2">Scheme</h3>
  2053. <a name="example.escheme"></a>
  2054. <div class="program_caption">
  2055. example.escheme</div>
  2056. <pre class="program">&lt;html&gt;
  2057. &lt;body&gt;
  2058. <strong>&lt;%
  2059. (let ((user "Erubis")
  2060. (items '("&lt;aaa&gt;" "b&amp;b" "\"ccc\""))
  2061. (i 0))
  2062. %&gt;</strong>
  2063. &lt;p&gt;Hello <strong>&lt;%= user %&gt;</strong>!&lt;/p&gt;
  2064. &lt;table&gt;
  2065. <strong>&lt;%
  2066. (for-each
  2067. (lambda (item)
  2068. (set! i (+ i 1))
  2069. %&gt;</strong>
  2070. &lt;tr bgcolor="<strong>&lt;%= (if (= (modulo i 2) 0) "#FFCCCC" "#CCCCFF") %&gt;</strong>"&gt;
  2071. &lt;td&gt;<strong>&lt;%= i %&gt;</strong>&lt;/td&gt;
  2072. &lt;td&gt;<strong>&lt;%= item %&gt;</strong>&lt;/td&gt;
  2073. &lt;/tr&gt;
  2074. <strong>&lt;%
  2075. ) ; lambda end
  2076. items) ; for-each end
  2077. %&gt;</strong>
  2078. &lt;/table&gt;
  2079. <strong>&lt;%
  2080. ) ; let end
  2081. %&gt;</strong>
  2082. &lt;/body&gt;
  2083. &lt;/html&gt;
  2084. </pre>
  2085. <a name="example_scheme.result"></a>
  2086. <div class="terminal_caption">
  2087. compiled source code</div>
  2088. <pre class="terminal">$ erubis -l scheme example.escheme
  2089. (let ((_buf '())) (define (_add x) (set! _buf (cons x _buf))) (_add "&lt;html&gt;
  2090. &lt;body&gt;\n")
  2091. (let ((user "Erubis")
  2092. (items '("&lt;aaa&gt;" "b&amp;b" "\"ccc\""))
  2093. (i 0))
  2094. (_add " &lt;p&gt;Hello ")(_add user)(_add "!&lt;/p&gt;
  2095. &lt;table&gt;\n")
  2096. (for-each
  2097. (lambda (item)
  2098. (set! i (+ i 1))
  2099. (_add " &lt;tr bgcolor=\"")(_add (if (= (modulo i 2) 0) "#FFCCCC" "#CCCCFF"))(_add "\"&gt;
  2100. &lt;td&gt;")(_add i)(_add "&lt;/td&gt;
  2101. &lt;td&gt;")(_add item)(_add "&lt;/td&gt;
  2102. &lt;/tr&gt;\n")
  2103. ) ; lambda end
  2104. items) ; for-each end
  2105. (_add " &lt;/table&gt;\n")
  2106. ) ; let end
  2107. (_add " &lt;/body&gt;
  2108. &lt;/html&gt;\n")
  2109. (reverse _buf))
  2110. </pre>
  2111. <a name="example_scheme_display.result"></a>
  2112. <div class="terminal_caption">
  2113. compiled source code (with <code>--func=display</code> property)</div>
  2114. <pre class="terminal">$ erubis -l scheme --func=display example.escheme
  2115. (display "&lt;html&gt;
  2116. &lt;body&gt;\n")
  2117. (let ((user "Erubis")
  2118. (items '("&lt;aaa&gt;" "b&amp;b" "\"ccc\""))
  2119. (i 0))
  2120. (display " &lt;p&gt;Hello ")(display user)(display "!&lt;/p&gt;
  2121. &lt;table&gt;\n")
  2122. (for-each
  2123. (lambda (item)
  2124. (set! i (+ i 1))
  2125. (display " &lt;tr bgcolor=\"")(display (if (= (modulo i 2) 0) "#FFCCCC" "#CCCCFF"))(display "\"&gt;
  2126. &lt;td&gt;")(display i)(display "&lt;/td&gt;
  2127. &lt;td&gt;")(display item)(display "&lt;/td&gt;
  2128. &lt;/tr&gt;\n")
  2129. ) ; lambda end
  2130. items) ; for-each end
  2131. (display " &lt;/table&gt;\n")
  2132. ) ; let end
  2133. (display " &lt;/body&gt;
  2134. &lt;/html&gt;\n")
  2135. </pre>
  2136. <br>
  2137. <a name="lang-perl"></a>
  2138. <h3 class="section2">Perl</h3>
  2139. <a name="example.eperl"></a>
  2140. <div class="program_caption">
  2141. example.eperl</div>
  2142. <pre class="program"><strong>&lt;%
  2143. my $user = 'Erubis';
  2144. my @list = ('&lt;aaa&gt;', 'b&amp;b', '"ccc"');
  2145. %&gt;</strong>
  2146. &lt;html&gt;
  2147. &lt;body&gt;
  2148. &lt;p&gt;Hello <strong>&lt;%= $user %&gt;</strong>!&lt;/p&gt;
  2149. &lt;table&gt;
  2150. <strong>&lt;% $i = 0; %&gt;</strong>
  2151. <strong>&lt;% for $item (@list) { %&gt;</strong>
  2152. &lt;tr bgcolor=<strong>&lt;%= ++$i % 2 == 0 ? '#FFCCCC' : '#CCCCFF' %&gt;</strong>"&gt;
  2153. &lt;td&gt;<strong>&lt;%= $i %&gt;</strong>&lt;/td&gt;
  2154. &lt;td&gt;<strong>&lt;%= $item %&gt;</strong>&lt;/td&gt;
  2155. &lt;/tr&gt;
  2156. <strong>&lt;% } %&gt;</strong>
  2157. &lt;/table&gt;
  2158. &lt;/body&gt;
  2159. &lt;/html&gt;
  2160. </pre>
  2161. <a name="example_perl.result"></a>
  2162. <div class="terminal_caption">
  2163. compiled source code</div>
  2164. <pre class="terminal">$ erubis -l perl example.eperl
  2165. use HTML::Entities;
  2166. my $user = 'Erubis';
  2167. my @list = ('&lt;aaa&gt;', 'b&amp;b', '"ccc"');
  2168. print('&lt;html&gt;
  2169. &lt;body&gt;
  2170. &lt;p&gt;Hello '); print($user); print('!&lt;/p&gt;
  2171. &lt;table&gt;
  2172. '); $i = 0;
  2173. for $item (@list) {
  2174. print(' &lt;tr bgcolor='); print(++$i % 2 == 0 ? '#FFCCCC' : '#CCCCFF'); print('"&gt;
  2175. &lt;td&gt;'); print($i); print('&lt;/td&gt;
  2176. &lt;td&gt;'); print($item); print('&lt;/td&gt;
  2177. &lt;/tr&gt;
  2178. '); }
  2179. print(' &lt;/table&gt;
  2180. &lt;/body&gt;
  2181. &lt;/html&gt;
  2182. ');
  2183. </pre>
  2184. <br>
  2185. <a name="lang-javascript"></a>
  2186. <h3 class="section2">JavaScript</h3>
  2187. <a name="example.ejs"></a>
  2188. <div class="program_caption">
  2189. example.ejs</div>
  2190. <pre class="program"><strong>&lt;%
  2191. var user = 'Erubis';
  2192. var list = ['&lt;aaa&gt;', 'b&amp;b', '"ccc"'];
  2193. %&gt;</strong>
  2194. &lt;html&gt;
  2195. &lt;body&gt;
  2196. &lt;p&gt;Hello <strong>&lt;%= user %&gt;</strong>!&lt;/p&gt;
  2197. &lt;table&gt;
  2198. &lt;tbody&gt;
  2199. <strong>&lt;% var i; %&gt;</strong>
  2200. <strong>&lt;% for (i = 0; i &lt; list.length; i++) { %&gt;</strong>
  2201. &lt;tr bgcolor="<strong>&lt;%= i % 2 == 0 ? '#FFCCCC' : '#CCCCFF' %&gt;</strong>"&gt;
  2202. &lt;td&gt;<strong>&lt;%= i + 1 %&gt;</strong>&lt;/td&gt;
  2203. &lt;td&gt;<strong>&lt;%= list[i] %&gt;</strong>&lt;/td&gt;
  2204. &lt;/tr&gt;
  2205. <strong>&lt;% } %&gt;</strong>
  2206. &lt;/tbody&gt;
  2207. &lt;/table&gt;
  2208. &lt;/body&gt;
  2209. &lt;/html&gt;
  2210. </pre>
  2211. <a name="example_js.result"></a>
  2212. <div class="terminal_caption">
  2213. compiled source code</div>
  2214. <pre class="terminal">$ erubis -l js example.ejs
  2215. var _buf = [];
  2216. var user = 'Erubis';
  2217. var list = ['&lt;aaa&gt;', 'b&amp;b', '"ccc"'];
  2218. _buf.push("&lt;html&gt;\n\
  2219. &lt;body&gt;\n\
  2220. &lt;p&gt;Hello "); _buf.push(user); _buf.push("!&lt;/p&gt;\n\
  2221. &lt;table&gt;\n\
  2222. &lt;tbody&gt;\n");
  2223. var i;
  2224. for (i = 0; i &lt; list.length; i++) {
  2225. _buf.push(" &lt;tr bgcolor=\""); _buf.push(i % 2 == 0 ? '#FFCCCC' : '#CCCCFF'); _buf.push("\"&gt;\n\
  2226. &lt;td&gt;"); _buf.push(i + 1); _buf.push("&lt;/td&gt;\n\
  2227. &lt;td&gt;"); _buf.push(list[i]); _buf.push("&lt;/td&gt;\n\
  2228. &lt;/tr&gt;\n");
  2229. }
  2230. _buf.push(" &lt;/tbody&gt;\n\
  2231. &lt;/table&gt;\n\
  2232. &lt;/body&gt;\n\
  2233. &lt;/html&gt;\n");
  2234. document.write(_buf.join(""));
  2235. </pre>
  2236. <p>If command-line option '<code>--docwrite=false</code>' is specified,
  2237. '<code>_buf.join("");</code>' is used instead of '<code>document.write(_buf.join(""));</code>'.
  2238. This is useful when passing converted source code to eval() function in JavaScript.
  2239. </p>
  2240. <p>You can pass <code>:docwrite=&gt;false</code> to Erubis::Ejavascript.new() in your Ruby script.
  2241. </p>
  2242. <pre class="program">s = File.read('example.jshtml')
  2243. engine = Erubis::Ejavascript.new(s, <code>:docwrite=&gt;false</code>)
  2244. </pre>
  2245. <p>If you want to specify any JavaScript code, use '--postamble=...'.
  2246. </p>
  2247. <p>Notice that default value of 'docwrite' property will be false in the future release.
  2248. </p>
  2249. <br>
  2250. <br>
  2251. <a name="rails"></a>
  2252. <h2 class="section1">Ruby on Rails Support</h2>
  2253. <p><span style="color:#FF0000">NOTICE: Rails 3 adopts Erubis as default default engine. You don't need to do anything at all when using Rails 3. This section is for Rails 2.</span>
  2254. </p>
  2255. <p>Erubis supports Ruby on Rails.
  2256. This section describes how to use Erubis with Ruby on Rails.
  2257. </p>
  2258. <a name="rails-settings"></a>
  2259. <h3 class="section2">Settings</h3>
  2260. <p>Add the following code to your 'config/environment.rb' and restart web server.
  2261. This replaces ERB in Rails by Erubis entirely.
  2262. </p>
  2263. <div class="program_caption">
  2264. config/environment.rb</div>
  2265. <pre class="program">require 'erubis/helpers/rails_helper'
  2266. #Erubis::Helpers::RailsHelper.engine_class = Erubis::Eruby # or Erubis::FastEruby
  2267. #Erubis::Helpers::RailsHelper.init_properties = {}
  2268. #Erubis::Helpers::RailsHelper.show_src = nil
  2269. #Erubis::Helpers::RailsHelper.preprocessing = false
  2270. </pre>
  2271. <p>Options:
  2272. </p>
  2273. <dl class="dl2">
  2274. <dt class="dt2">
  2275. Erubis::Helpers::RailsHelper.engine_class (=Erubis::Eruby)</dt>
  2276. <dd class="dd2">
  2277. <p> Erubis engine class (default Erubis::Eruby).
  2278. </p>
  2279. </dd>
  2280. <dt class="dt2">
  2281. Erubis::Helpers::RailsHelper.init_properties (={})</dt>
  2282. <dd class="dd2">
  2283. <p> Optional arguments for Erubis::Eruby#initialize() method (default {}).
  2284. </p>
  2285. </dd>
  2286. <dt class="dt2">
  2287. Erubis::Helpers::RailsHelper.show_src (=nil)</dt>
  2288. <dd class="dd2">
  2289. <p> Whether to print converted Ruby code into log file.
  2290. If true, Erubis prints coverted code into log file.
  2291. If false, Erubis doesn't.
  2292. If nil, Erubis prints when ENV['RAILS_ENV'] == 'development'.
  2293. Default is nil.
  2294. </p>
  2295. </dd>
  2296. <dt class="dt2">
  2297. Erubis::Helpers::RailsHelper.preprocessing (=false)</dt>
  2298. <dd class="dd2">
  2299. <p> Enable preprocessing if true (default false).
  2300. </p>
  2301. </dd>
  2302. </dl>
  2303. <br>
  2304. <a name="rails-preprocessing"></a>
  2305. <h3 class="section2">Preprosessing</h3>
  2306. <p>Erubis supports preprocessing of template files.
  2307. Preprocessing make your Ruby on Rails application about 20-40 percent faster.
  2308. To enable preprocessing, set Erubis::Helpers::RailsHelper.preprocessing to true in your 'environment.rb' file.
  2309. </p>
  2310. <p>For example, assume the following template.
  2311. This is slow because link_to() method is called every time when template is rendered.
  2312. </p>
  2313. <pre class="program">&lt;%= link_to 'Create', :action=&gt;'create' %&gt;
  2314. </pre>
  2315. <p>The following is faster than the above, but not flexible because url is fixed.
  2316. </p>
  2317. <pre class="program">&lt;a href="/users/create"&gt;Create&lt;/a&gt;
  2318. </pre>
  2319. <p>Preprocessing solves this problem.
  2320. If you use '[%= %]' instead of '&lt;%= %&gt;', preprocessor evaluate it only once when template is loaded.
  2321. </p>
  2322. <pre class="program"><strong>[%= link_to 'Create', :action=&gt;'create'%]</strong>
  2323. </pre>
  2324. <p>The above is evaluated by preprocessor and replaced to the following code automatically.
  2325. </p>
  2326. <pre class="program">&lt;a href="/users/create"&gt;Create&lt;/a&gt;
  2327. </pre>
  2328. <p>Notice that this is done only once when template file is loaded.
  2329. It means that link_to() method is not called when template is rendered.
  2330. </p>
  2331. <p>If link_to() method have variable arguments, use <code>_?()</code> helper method.
  2332. </p>
  2333. <pre class="program">&lt;% for user in @users %&gt;
  2334. [%= link_to <strong>_?('user.name')</strong>, :action=&gt;'show', :id=&gt;<strong>_?('user.id')</strong> %]
  2335. &lt;% end %&gt;
  2336. </pre>
  2337. <p>The above is evaluated by preprocessor when template is loaded and expanded into the following code.
  2338. This will be much faster because link_to() method is not called when rendering.
  2339. </p>
  2340. <pre class="program">&lt;% for user in @users %&gt;
  2341. &lt;a href="/users/show/<strong>&lt;%=user.id%&gt;</strong>"&gt;<strong>&lt;%=user.name%&gt;</strong>&lt;/a&gt;
  2342. &lt;% end %&gt;
  2343. </pre>
  2344. <p>Preprocessing statement (<code>[% %]</code>) is also available as well as preprocessing expression (<code>[%= %]</code>).
  2345. </p>
  2346. <pre class="program">&lt;select name="state"&gt;
  2347. &lt;option value=""&gt;-&lt;/option&gt;
  2348. <strong>[% for code in states.keys.sort %]</strong>
  2349. &lt;option value="<strong>[%= code %]</strong>"&gt;<strong>[%= states[code] %]</strong>&lt;/option&gt;
  2350. <strong>[% end %]</strong>
  2351. &lt;/select&gt;
  2352. </pre>
  2353. <p>The above will be evaluated by preprocessor and expanded into the following when template is loaded.
  2354. In the result, rendering speed will be much faster because for-loop is not executed when rendering.
  2355. </p>
  2356. <pre class="program">&lt;select name="state"&gt;
  2357. &lt;option value=""&gt;-&lt;/option&gt;
  2358. &lt;option value="AK"&gt;Alaska&lt;/option&gt;
  2359. &lt;option value="AL"&gt;Alabama&lt;/option&gt;
  2360. &lt;option value="AR"&gt;Arkansas&lt;/option&gt;
  2361. &lt;option value="AS"&gt;American Samoa&lt;/option&gt;
  2362. &lt;option value="AZ"&gt;Arizona&lt;/option&gt;
  2363. &lt;option value="CA"&gt;California&lt;/option&gt;
  2364. &lt;option value="CO"&gt;Colorado&lt;/option&gt;
  2365. ....
  2366. &lt;/select&gt;
  2367. </pre>
  2368. <p>Notice that it is not recommended to use preprocessing with tag helpers,
  2369. because tag helpers generate different html code when form parameter has errors or not.
  2370. </p>
  2371. <p>Helper methods of Ruby on Rails are divided into two groups.
  2372. </p>
  2373. <ul type="disc">
  2374. <li>link_to() or _() (method of gettext package) are not need to call for every time
  2375. as template is rendered because it returns same value when same arguments are passed.
  2376. These methods can be got faster by preprocessing.
  2377. </li>
  2378. <li>Tag helper methods should be called for every time as template is rendered
  2379. because it may return differrent value even if the same arguments are passed.
  2380. Preprocessing is not available with these methods.
  2381. </li>
  2382. </ul>
  2383. <p>In Ruby on Rails 2.0, <code>_?('user_id')</code> is OK but <code>_?('user.id')</code> is NG
  2384. because the latter contains period ('.') character.
  2385. </p>
  2386. <pre class="program">&lt;!-- NG in Rails 2.0, because _?('') contains period --&gt;
  2387. [%= link_to 'Edit', edit_user_path(<strong>_?('@user.id')</strong>) %]
  2388. [%= link_to 'Show', <strong>@user</strong> %]
  2389. [%= link_to 'Delete', <strong>@user</strong>, :confirm=&gt;'OK?', :method=&gt;:delete %]
  2390. &lt;!-- OK in Rails 2.0 --&gt;
  2391. <strong>&lt;%= user_id = @user.id %&gt;</strong>
  2392. [%= link_to 'Edit', edit_user_path(<strong>_?('user_id')</strong>) %]
  2393. [%= link_to 'Show', <strong>:action=&gt;'show', :id=&gt;_?('user_id')</strong> %]
  2394. [%= link_to 'Delete', <strong>{:action=&gt;'destroy', :id=&gt;_?('user_id')}</strong>,
  2395. {:confirm=&gt;'OK?', :method=&gt;:delete} %]
  2396. </pre>
  2397. <br>
  2398. <a name="rails-formhelpers"></a>
  2399. <h3 class="section2">Form Helpers for Preprocessing</h3>
  2400. <p><strong>(Experimental)</strong>
  2401. </p>
  2402. <p>Erubis provides form helper methods for preprocessing.
  2403. These are defined in 'erubis/helpers/rails_form_helper.rb'.
  2404. If you want to use it, require it and include Erubis::Helpers::RailsFormHelper in 'app/helpers/applition_helper.rb'
  2405. </p>
  2406. <div class="program_caption">
  2407. app/helpers/xxx_helper.rb</div>
  2408. <pre class="program">require 'erubis/helpers/rails_form_helper'
  2409. module ApplicationHelper
  2410. include Erubis::Helpers::RailsFormHelper
  2411. end
  2412. </pre>
  2413. <p>Form helper methods defined in Erubis::Helpers::RailsFormHelper are named as 'pp_xxxx'
  2414. ('pp' represents preprocessing).
  2415. </p>
  2416. <p>Assume the following view template:
  2417. </p>
  2418. <div class="program_caption">
  2419. _form.rhtml</div>
  2420. <pre class="program"> &lt;p&gt;
  2421. Name: &lt;%= text_field :user, :name %&gt;
  2422. &lt;/p&gt;
  2423. &lt;p&gt;
  2424. Name: <strong>[%= pp_text_field :user, :name %]</strong>
  2425. &lt;/p&gt;
  2426. </pre>
  2427. <p>Erubis preprocessor converts it to the following eRuby string:
  2428. </p>
  2429. <div class="program_caption">
  2430. preprocessed</div>
  2431. <pre class="program"> &lt;p&gt;
  2432. Name: &lt;%= text_field :user, :name %&gt;
  2433. &lt;/p&gt;
  2434. &lt;p&gt;
  2435. Name: <strong>&lt;input id="stock_name" name="stock[name]" size="30" type="text" value="&lt;%=h @stock.name%&gt;" /&gt;</strong>
  2436. &lt;/p&gt;
  2437. </pre>
  2438. <p>Erubis converts it to the following Ruby code:
  2439. </p>
  2440. <div class="program_caption">
  2441. Ruby code</div>
  2442. <pre class="program"> _buf &lt;&lt; ' &lt;p&gt;
  2443. Name: '; _buf &lt;&lt; ( text_field :stock, :name ).to_s; _buf &lt;&lt; '
  2444. '; _buf &lt;&lt; ' &lt;/p&gt;
  2445. &lt;p&gt;
  2446. 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;
  2447. &lt;/p&gt;
  2448. ';
  2449. </pre>
  2450. <p>The above Ruby code shows that text_field() is called everytime when rendering,
  2451. but pp_text_field() is called only once when template is loaded.
  2452. This means that pp_text_field() with preprocessing makes view layer very fast.
  2453. </p>
  2454. <p>Module Erubis::Helpers::RailsFormHelper defines the following form helper methods.
  2455. </p>
  2456. <ul type="disc">
  2457. <li>pp_render_partial(basename)
  2458. </li>
  2459. <li>pp_form_tag(url_for_options={}, options={}, *parameters_for_url, &amp;block)
  2460. </li>
  2461. <li>pp_text_field(object_name, method, options={})
  2462. </li>
  2463. <li>pp_password_field(object_name, method, options={})
  2464. </li>
  2465. <li>pp_hidden_field(object_name, method, options={})
  2466. </li>
  2467. <li>pp_file_field(object_name, method, options={})
  2468. </li>
  2469. <li>pp_text_area(object_name, method, options={})
  2470. </li>
  2471. <li>pp_check_box(object_name, method, options={}, checked_value="1", unchecked_value="0")
  2472. </li>
  2473. <li>pp_radio_button(object_name, method, tag_value, options={})
  2474. </li>
  2475. <li>pp_select(object, method, collection, options={}, html_options={})
  2476. </li>
  2477. <li>pp_collection_select(object, method, collection, value_method, text_method, options={}, html_options={})
  2478. </li>
  2479. <li>pp_country_select(object, method, priority_countries=nil, options={}, html_options={})
  2480. </li>
  2481. <li>pp_time_zone_select(object, method, priority_zones=nil, options={}, html_options={})
  2482. </li>
  2483. <li>pp_submit_tag(value="Save changes", options={})
  2484. </li>
  2485. <li>pp_image_submit_tag(source, options={})
  2486. </li>
  2487. </ul>
  2488. <p>Notice that pp_form_for() is not provided.
  2489. </p>
  2490. <p><span style="color:#FF0000">CAUTION:</span> These are experimental and may not work in Ruby on Rails 2.0.
  2491. </p>
  2492. <br>
  2493. <a name="rails-others"></a>
  2494. <h3 class="section2">Others</h3>
  2495. <ul type="disc">
  2496. <li>ActionView::Helpers::CaptureHelper#capture() and ActionView::Helpers::Texthelper#concat() are available.
  2497. </li>
  2498. </ul>
  2499. <ul type="disc">
  2500. <li>Form helper methods are not tested in Ruby on Rails 2.0.
  2501. </li>
  2502. </ul>
  2503. <ul type="disc">
  2504. <li>ERB::Util.h() is redefined if you require 'erubis/helpers/rails_helper.rb'.
  2505. Original definition of ERB::Util.h() is the following and it is slow
  2506. because it scans string four times.
  2507. <pre class="program"> def html_escape(s)
  2508. s.to_s.gsub(/&amp;/, "&amp;amp;").gsub(/\"/, "&amp;quot;").gsub(/&gt;/, "&amp;gt;").gsub(/&lt;/, "&amp;lt;")
  2509. end
  2510. alias h html_escape
  2511. </pre>
  2512. <p> New definition in 'erubis/helpers/rails_helper.rb' is faster than the above
  2513. because it scans string only once.
  2514. </p>
  2515. <pre class="program"> ESCAPE_TABLE = { '&amp;'=&gt;'&amp;amp;', '&lt;'=&gt;'&amp;lt;', '&gt;'=&gt;'&amp;gt;', '"'=&gt;'&amp;quot;', "'"=&gt;'&amp;#039;', }
  2516. def h(value)
  2517. value.to_s.gsub(/[&amp;&lt;&gt;"]/) { |s| ESCAPE_TABLE[s] }
  2518. end
  2519. </pre>
  2520. <p> Notice that the new definition may be slow if string contains
  2521. many '&lt; &gt; &amp; "' characters because block is call many time.
  2522. You should use ERB::Util.html_hscape() if string contains a lot of '&lt; &gt; &amp; "'
  2523. characters.
  2524. </p>
  2525. </li>
  2526. </ul>
  2527. <br>
  2528. <br>
  2529. <a name="topics"></a>
  2530. <h2 class="section1">Other Topics</h2>
  2531. <a name="topics-fasteruby"></a>
  2532. <h3 class="section2"><code>Erubis::FastEruby</code> Class</h3>
  2533. <p><code>Erubis::FastEruby</code> class generates more effective code than <code>Erubis::Eruby</code>.
  2534. </p>
  2535. <a name="fasteruby-example.rb"></a>
  2536. <div class="program_caption">
  2537. fasteruby-example.rb</div>
  2538. <pre class="program">require 'erubis'
  2539. input = File.read('example.eruby')
  2540. puts "----- Erubis::Eruby -----"
  2541. print Erubis::Eruby.new(input).src
  2542. puts "----- Erubis::FastEruby -----"
  2543. print <strong>Erubis::FastEruby</strong>.new(input).src
  2544. </pre>
  2545. <a name="fasteruby-example.result"></a>
  2546. <div class="terminal_caption">
  2547. result</div>
  2548. <pre class="terminal">$ ruby fasteruby-example.rb
  2549. ----- Erubis::Eruby -----
  2550. _buf = ''; _buf &lt;&lt; '&lt;div&gt;
  2551. '; for item in list
  2552. _buf &lt;&lt; ' &lt;p&gt;'; <strong>_buf &lt;&lt; ( item ).to_s;</strong> _buf &lt;&lt; '&lt;/p&gt;
  2553. &lt;p&gt;'; <strong>_buf &lt;&lt; Erubis::XmlHelper.escape_xml( item );</strong> _buf &lt;&lt; '&lt;/p&gt;
  2554. '; end
  2555. _buf &lt;&lt; '&lt;/div&gt;
  2556. ';
  2557. _buf.to_s
  2558. ----- Erubis::FastEruby -----
  2559. _buf = ''; _buf &lt;&lt; %Q`&lt;div&gt;\n`
  2560. for item in list
  2561. _buf &lt;&lt; %Q` &lt;p&gt;<strong>#{ item }</strong>&lt;/p&gt;
  2562. &lt;p&gt;<strong>#{Erubis::XmlHelper.escape_xml( item )}</strong>&lt;/p&gt;\n`
  2563. end
  2564. _buf &lt;&lt; %Q`&lt;/div&gt;\n`
  2565. _buf.to_s
  2566. </pre>
  2567. <p>Technically, <code>Erubis::FastEruby</code> is just a subclass of <code>Erubis::Eruby</code> and includes <code><a href="#interpolation-enhancer">InterpolationEnhancer</a></code>. <code>Erubis::FastEruby</code> is faster than <code>Erubis::Eruby</code> but is not extensible compared to <code>Erubis::Eruby</code>. This is the reason why <code>Erubis::FastEruby</code> is not the default class of Erubis.
  2568. </p>
  2569. <br>
  2570. <a name="topics-bufvar"></a>
  2571. <h3 class="section2"><code>:bufvar</code> Option</h3>
  2572. <p>Since 2.7.0, Erubis supports <code>:bufvar</code> option which allows you to change buffer variable name (default '<code>_buf</code>').
  2573. </p>
  2574. <a name="bufvar-example.rb"></a>
  2575. <div class="program_caption">
  2576. bufvar-example.rb</div>
  2577. <pre class="program">require 'erubis'
  2578. input = File.read('example.eruby')
  2579. puts "----- default -----"
  2580. eruby = Erubis::FastEruby.new(input)
  2581. puts eruby.src
  2582. puts "----- with :bufvar option -----"
  2583. eruby = Erubis::FastEruby.new(input, <strong>:bufvar=&gt;'@_out_buf'</strong>)
  2584. print eruby.src
  2585. </pre>
  2586. <a name="bufvar-example.result"></a>
  2587. <div class="terminal_caption">
  2588. result</div>
  2589. <pre class="terminal">$ ruby bufvar-example.rb
  2590. ----- default -----
  2591. _buf = ''; _buf &lt;&lt; %Q`&lt;div&gt;\n`
  2592. for item in list
  2593. _buf &lt;&lt; %Q` &lt;p&gt;#{ item }&lt;/p&gt;
  2594. &lt;p&gt;#{Erubis::XmlHelper.escape_xml( item )}&lt;/p&gt;\n`
  2595. end
  2596. _buf &lt;&lt; %Q`&lt;/div&gt;\n`
  2597. _buf.to_s
  2598. ----- with :bufvar option -----
  2599. <strong>@_out_buf</strong> = ''; <strong>@_out_buf</strong> &lt;&lt; %Q`&lt;div&gt;\n`
  2600. for item in list
  2601. <strong>@_out_buf</strong> &lt;&lt; %Q` &lt;p&gt;#{ item }&lt;/p&gt;
  2602. &lt;p&gt;#{Erubis::XmlHelper.escape_xml( item )}&lt;/p&gt;\n`
  2603. end
  2604. <strong>@_out_buf</strong> &lt;&lt; %Q`&lt;/div&gt;\n`
  2605. <strong>@_out_buf</strong>.to_s
  2606. </pre>
  2607. <br>
  2608. <a name="topics-trimspaces"></a>
  2609. <h3 class="section2">'&lt;%= =%&gt;' and '&lt;%= -%&gt;'</h3>
  2610. <p>Since 2.6.0, '&lt;%= -%&gt;' remove tail spaces and newline.
  2611. This is for compatibiliy with ERB when trim mode is '-'.
  2612. '&lt;%= =%&gt;' also removes tail spaces and newlines, and this is
  2613. Erubis-original enhancement (cooler than '&lt;%= -%&gt;', isn't it?).
  2614. </p>
  2615. <a name="tailnewline.rhtml.comment_filter"></a>
  2616. <div class="program_caption">
  2617. tailnewline.rhtml</div>
  2618. <pre class="program">&lt;div&gt;
  2619. &lt;%= @var -%&gt; # or &lt;%= @var =%&gt;
  2620. &lt;/div&gt;
  2621. </pre>
  2622. <div class="terminal_caption">
  2623. result (version 2.5.0):</div>
  2624. <pre class="terminal">$ erubis -c '{var: "AAA\n"}' tailnewline.rhtml
  2625. &lt;div&gt;
  2626. AAA
  2627. &lt;/div&gt;
  2628. </pre>
  2629. <a name="tail_260.result"></a>
  2630. <div class="terminal_caption">
  2631. result (version 2.6.0):</div>
  2632. <pre class="terminal">$ erubis -c '{var: "AAA\n"}' tailnewline.rhtml
  2633. &lt;div&gt;
  2634. AAA
  2635. &lt;/div&gt;
  2636. </pre>
  2637. <br>
  2638. <a name="topics-doublepercent"></a>
  2639. <h3 class="section2">'&lt;%% %&gt;' and '&lt;%%= %&gt;'</h3>
  2640. <p>Since 2.6.0, '&lt;%% %&gt;' and '&lt;%%= %&gt;' are converted into '&lt;% %&gt;' and '&lt;%= %&gt;' respectively.
  2641. This is for compatibility with ERB.
  2642. </p>
  2643. <div class="program_caption">
  2644. doublepercent.rhtml:</div>
  2645. <pre class="program">&lt;ul&gt;
  2646. &lt;%% for item in @list %&gt;
  2647. &lt;li&gt;&lt;%%= item %&gt;&lt;/li&gt;
  2648. &lt;%% end %&gt;
  2649. &lt;/ul&gt;
  2650. </pre>
  2651. <div class="terminal_caption">
  2652. result:</div>
  2653. <pre class="terminal">$ erubis doublepercent.rhtml
  2654. &lt;ul&gt;
  2655. &lt;% for item in @list %&gt;
  2656. &lt;li&gt;&lt;%= item %&gt;&lt;/li&gt;
  2657. &lt;% end %&gt;
  2658. &lt;/ul&gt;
  2659. </pre>
  2660. <br>
  2661. <a name="topics-context-vs-binding"></a>
  2662. <h3 class="section2">evaluate(context) v.s. result(binding)</h3>
  2663. <p>It is recommended to use 'Erubis::Eruby#evaluate(context)' instead of 'Erubis::Eruby#result(binding)' because Ruby's Binding object has some problems.
  2664. </p>
  2665. <ul type="disc">
  2666. <li>It is not able to specify variables to use.
  2667. Using binding() method, all of local variables are passed to templates.
  2668. </li>
  2669. <li>Changing local variables in templates may affect to varialbes in main program.
  2670. If you assign '10' to local variable 'x' in templates, it may change variable 'x' in main program unintendedly.
  2671. </li>
  2672. </ul>
  2673. <p>The following example shows that assignment of some values into variable 'x' in templates affect to local variable 'x' in main program unintendedly.
  2674. </p>
  2675. <a name="template1.rhtml"></a>
  2676. <div class="program_caption">
  2677. template1.rhtml (intended to be passed 'items' from main program)</div>
  2678. <pre class="program">&lt;% for <strong>x</strong> in <strong>items</strong> %&gt;
  2679. item = &lt;%= x %&gt;
  2680. &lt;% end %&gt;
  2681. ** debug: local variables=&lt;%= local_variables().inspect() %&gt;
  2682. </pre>
  2683. <a name="main_program1.rb"></a>
  2684. <div class="program_caption">
  2685. main_program1.rb (intended to pass 'items' to template)</div>
  2686. <pre class="program">require 'erubis'
  2687. eruby = Erubis::Eruby.new(File.read('template1.rhtml'))
  2688. items = ['foo', 'bar', 'baz']
  2689. x = 1
  2690. ## local variable 'x' and 'eruby' are passed to template as well as 'items'!
  2691. print <strong>eruby.result(binding())</strong>
  2692. ## local variable 'x' is changed unintendedly because it is changed in template!
  2693. puts "** debug: x=#{x.inspect}" #=&gt; "baz"
  2694. </pre>
  2695. <a name="main_program1.result"></a>
  2696. <div class="terminal_caption">
  2697. Result:</div>
  2698. <pre class="terminal">$ ruby main_program1.rb
  2699. item = foo
  2700. item = bar
  2701. item = baz
  2702. ** debug: local variables=["eruby", "items", "x", "_buf"]
  2703. ** debug: x="baz"
  2704. </pre>
  2705. <p>This problem is caused because Ruby's Binding class is poor to use in template engine.
  2706. Binding class should support the following features.
  2707. </p>
  2708. <pre class="program">b = Binding.new # create empty Binding object
  2709. b['x'] = 1 # set local variables using binding object
  2710. </pre>
  2711. <p>But the above features are not implemented in Ruby.
  2712. </p>
  2713. <p>A pragmatic solution is to use 'Erubis::Eruby#evaluate(context)' instead of 'Erubis::Eruby#result(binding)'.
  2714. 'evaluate(context)' uses Erubis::Context object and instance variables instead of Binding object and local variables.
  2715. </p>
  2716. <a name="template2.rhtml"></a>
  2717. <div class="program_caption">
  2718. template2.rhtml (intended to be passed '@items' from main program)</div>
  2719. <pre class="program">&lt;% for <strong>x</strong> in <strong>@items</strong> %&gt;
  2720. item = &lt;%= x %&gt;
  2721. &lt;% end %&gt;
  2722. ** debug: local variables=&lt;%= local_variables().inspect() %&gt;
  2723. </pre>
  2724. <a name="main_program2.rb"></a>
  2725. <div class="program_caption">
  2726. main_program2.rb (intended to pass '@items' to template)</div>
  2727. <pre class="program">require 'erubis'
  2728. eruby = Erubis::Eruby.new(File.read('template2.rhtml'))
  2729. items = ['foo', 'bar', 'baz']
  2730. x = 1
  2731. ## only 'items' are passed to template
  2732. print <strong>eruby.evaluate(:items=&gt;items)</strong>
  2733. ## local variable 'x' is not changed!
  2734. puts "** debug: x=#{x.inspect}" #=&gt; 1
  2735. </pre>
  2736. <a name="main_program2.result"></a>
  2737. <div class="terminal_caption">
  2738. Result:</div>
  2739. <pre class="terminal">$ ruby main_program2.rb
  2740. item = foo
  2741. item = bar
  2742. item = baz
  2743. ** debug: local variables=["_context", "x", "_buf"]
  2744. ** debug: x=1
  2745. </pre>
  2746. <br>
  2747. <a name="topics-fasteruby"></a>
  2748. <h3 class="section2">Class Erubis::FastEruby</h3>
  2749. <p>[experimental]
  2750. </p>
  2751. <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>.
  2752. If you desire more speed, try Erubis::FastEruby class.
  2753. </p>
  2754. <a name="fasteruby.rhtml"></a>
  2755. <div class="program_caption">
  2756. File 'fasteruby.rhtml':</div>
  2757. <pre class="program">&lt;html&gt;
  2758. &lt;body&gt;
  2759. &lt;h1&gt;&lt;%== @title %&gt;&lt;/h1&gt;
  2760. &lt;table&gt;
  2761. &lt;% i = 0 %&gt;
  2762. &lt;% for item in @list %&gt;
  2763. &lt;% i += 1 %&gt;
  2764. &lt;tr&gt;
  2765. &lt;td&gt;&lt;%= i %&gt;&lt;/td&gt;
  2766. &lt;td&gt;&lt;%== item %&gt;&lt;/td&gt;
  2767. &lt;/tr&gt;
  2768. &lt;% end %&gt;
  2769. &lt;/table&gt;
  2770. &lt;/body&gt;
  2771. &lt;/html&gt;
  2772. </pre>
  2773. <a name="fasteruby.rb"></a>
  2774. <div class="program_caption">
  2775. File 'fasteruby.rb':</div>
  2776. <pre class="program">require 'erubis'
  2777. input = File.read('fasteruby.rhtml')
  2778. eruby = <strong>Erubis::FastEruby.new(input)</strong> # create Eruby object
  2779. puts "---------- script source ---"
  2780. puts eruby.src
  2781. puts "---------- result ----------"
  2782. context = { :title=&gt;'Example', :list=&gt;['aaa', 'bbb', 'ccc'] }
  2783. output = eruby.evaluate(context)
  2784. print output
  2785. </pre>
  2786. <a name="fasteruby.result"></a>
  2787. <div class="terminal_caption">
  2788. output</div>
  2789. <pre class="terminal">$ ruby fasteruby.rb
  2790. ---------- script source ---
  2791. _buf = ''; _buf &lt;&lt; <strong>%Q`</strong>&lt;html&gt;
  2792. &lt;body&gt;
  2793. &lt;h1&gt;<strong>#{Erubis::XmlHelper.escape_xml( @title )}</strong>&lt;/h1&gt;
  2794. &lt;table&gt;\n<strong>`</strong>
  2795. i = 0
  2796. for item in @list
  2797. i += 1
  2798. _buf &lt;&lt; <strong>%Q`</strong> &lt;tr&gt;
  2799. &lt;td&gt;<strong>#{ i }</strong>&lt;/td&gt;
  2800. &lt;td&gt;<strong>#{Erubis::XmlHelper.escape_xml( item )}</strong>&lt;/td&gt;
  2801. &lt;/tr&gt;\n<strong>`</strong>
  2802. end
  2803. _buf &lt;&lt; <strong>%Q`</strong> &lt;/table&gt;
  2804. &lt;/body&gt;
  2805. &lt;/html&gt;\n<strong>`</strong>
  2806. _buf.to_s
  2807. ---------- result ----------
  2808. &lt;html&gt;
  2809. &lt;body&gt;
  2810. &lt;h1&gt;Example&lt;/h1&gt;
  2811. &lt;table&gt;
  2812. &lt;tr&gt;
  2813. &lt;td&gt;1&lt;/td&gt;
  2814. &lt;td&gt;aaa&lt;/td&gt;
  2815. &lt;/tr&gt;
  2816. &lt;tr&gt;
  2817. &lt;td&gt;2&lt;/td&gt;
  2818. &lt;td&gt;bbb&lt;/td&gt;
  2819. &lt;/tr&gt;
  2820. &lt;tr&gt;
  2821. &lt;td&gt;3&lt;/td&gt;
  2822. &lt;td&gt;ccc&lt;/td&gt;
  2823. &lt;/tr&gt;
  2824. &lt;/table&gt;
  2825. &lt;/body&gt;
  2826. &lt;/html&gt;
  2827. </pre>
  2828. <br>
  2829. <a name="topics-syntax"></a>
  2830. <h3 class="section2">Syntax Checking</h3>
  2831. <p>Command-line option '-z' checks syntax. It is similar to 'erubis -x file.rhtml | ruby -wc', but it can take several file names.
  2832. </p>
  2833. <div class="terminal_caption">
  2834. example of command-line option '-z'</div>
  2835. <pre class="terminal">$ erubis <strong>-z</strong> app/views/*/*.rhtml
  2836. Syntax OK
  2837. </pre>
  2838. <br>
  2839. <a name="topics-caching"></a>
  2840. <h3 class="section2">File Caching</h3>
  2841. <p>Erubis::Eruby.load_file(filename) convert file into Ruby script and return Eruby object.
  2842. In addition, it caches converted Ruby script into cache file (filename + '.cache') if cache file is old or not exist.
  2843. If cache file exists and is newer than eruby file, Erubis::Eruby.load_file() loads cache file.
  2844. </p>
  2845. <div class="program_caption">
  2846. example of Erubis::Eruby.load_file()</div>
  2847. <pre class="program">require 'erubis'
  2848. filename = 'example.rhtml'
  2849. eruby = <strong>Erubis::Eruby.load_file(filename)</strong>
  2850. cachename = filename + '.cache'
  2851. if test(?f, cachename)
  2852. puts "*** cache file '#{cachename}' created."
  2853. end
  2854. </pre>
  2855. <p>Since 2.6.0, it is able to specify cache filename.
  2856. </p>
  2857. <div class="program_caption">
  2858. specify cache filename.</div>
  2859. <pre class="program">filename = 'example.rhtml'
  2860. eruby = Erubis::Eruby.load_file(filename, :cachename=&gt;filename+'.cache')
  2861. </pre>
  2862. <p>Caching makes Erubis about 40-50 percent faster than no-caching.
  2863. See <a href="#topics-benchmark">benchmark</a> for details.
  2864. </p>
  2865. <br>
  2866. <a name="topics-tinyeruby"></a>
  2867. <h3 class="section2">Erubis::TinyEruby class</h3>
  2868. <p>Erubis::TinyEruby class in 'erubis/tiny.rb' is the smallest implementation of eRuby.
  2869. If you don't need any enhancements of Erubis and only require simple eRuby implementation,
  2870. try Erubis::TinyEruby class.
  2871. </p>
  2872. <br>
  2873. <a name="topics-php"></a>
  2874. <h3 class="section2">NoTextEnhancer and NoCodeEnhancer in PHP</h3>
  2875. <p>NoTextEnhancer and NoCodEnahncer are quite useful not only for eRuby but also for PHP.
  2876. The former "drops" HTML text and show up embedded Ruby/PHP code
  2877. and the latter drops embedded Ruby/PHP code and leave HTML text.
  2878. </p>
  2879. <p>For example, see the following PHP script.
  2880. </p>
  2881. <a name="notext-example.php"></a>
  2882. <div class="program_caption">
  2883. notext-example.php</div>
  2884. <pre class="program">&lt;html&gt;
  2885. &lt;body&gt;
  2886. &lt;h3&gt;List&lt;/h3&gt;
  2887. &lt;?php if (!$list || count($list) == 0) { ?&gt;
  2888. &lt;p&gt;not found.&lt;/p&gt;
  2889. &lt;?php } else { ?&gt;
  2890. &lt;table&gt;
  2891. &lt;tbody&gt;
  2892. &lt;?php $i = 0; ?&gt;
  2893. &lt;?php foreach ($list as $item) { ?&gt;
  2894. &lt;tr bgcolor="&lt;?php echo ++$i % 2 == 1 ? '#FFCCCC' : '#CCCCFF'; ?&gt;"&gt;
  2895. &lt;td&gt;&lt;?php echo $item; ?&gt;&lt;/td&gt;
  2896. &lt;/tr&gt;
  2897. &lt;?php } ?&gt;
  2898. &lt;/tbody&gt;
  2899. &lt;/table&gt;
  2900. &lt;?php } ?&gt;
  2901. &lt;/body&gt;
  2902. &lt;/html&gt;
  2903. </pre>
  2904. <p>This is complex because PHP code and HTML document are mixed.
  2905. NoTextEnhancer can separate PHP code from HTML document.
  2906. </p>
  2907. <a name="notext-php.result"></a>
  2908. <div class="terminal_caption">
  2909. example of using NoTextEnhancer with PHP file</div>
  2910. <pre class="terminal">$ erubis -l php --pi=php -N -E NoText --trim=false notext-example.php
  2911. 1:
  2912. 2:
  2913. 3:
  2914. 4: &lt;?php if (!$list || count($list) == 0) { ?&gt;
  2915. 5:
  2916. 6: &lt;?php } else { ?&gt;
  2917. 7:
  2918. 8:
  2919. 9: &lt;?php $i = 0; ?&gt;
  2920. 10: &lt;?php foreach ($list as $item) { ?&gt;
  2921. 11: &lt;?php echo ++$i % 2 == 1 ? '#FFCCCC' : '#CCCCFF'; ?&gt;
  2922. 12: &lt;?php echo $item; ?&gt;
  2923. 13:
  2924. 14: &lt;?php } ?&gt;
  2925. 15:
  2926. 16:
  2927. 17: &lt;?php } ?&gt;
  2928. 18:
  2929. 19:
  2930. </pre>
  2931. <p>In the same way, NoCodeEnhancer can extract HTML tags.
  2932. </p>
  2933. <a name="nocode-php.result"></a>
  2934. <div class="terminal_caption">
  2935. example of using NoCodeEnhancer with PHP file</div>
  2936. <pre class="terminal">$ erubis -l php --pi=php -N -E NoCode --trim=false notext-example.php
  2937. 1: &lt;html&gt;
  2938. 2: &lt;body&gt;
  2939. 3: &lt;h3&gt;List&lt;/h3&gt;
  2940. 4:
  2941. 5: &lt;p&gt;not found.&lt;/p&gt;
  2942. 6:
  2943. 7: &lt;table&gt;
  2944. 8: &lt;tbody&gt;
  2945. 9:
  2946. 10:
  2947. 11: &lt;tr bgcolor=""&gt;
  2948. 12: &lt;td&gt;&lt;/td&gt;
  2949. 13: &lt;/tr&gt;
  2950. 14:
  2951. 15: &lt;/tbody&gt;
  2952. 16: &lt;/table&gt;
  2953. 17:
  2954. 18: &lt;/body&gt;
  2955. 19: &lt;/html&gt;
  2956. </pre>
  2957. <br>
  2958. <a name="topcs-modruby"></a>
  2959. <h3 class="section2">Helper Class for mod_ruby</h3>
  2960. <p>Thanks Andrew R Jackson, he developed 'erubis-run.rb' which enables you to use Erubis with mod_ruby.
  2961. </p>
  2962. <ol type="1">
  2963. <li>Copy 'erubis-2.7.0/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.
  2964. <pre class="terminal">$ cd erubis-2.7.0/
  2965. $ sudo copy contrib/erubis-run.rb /usr/local/lib/ruby/1.8/apache/
  2966. </pre>
  2967. </li>
  2968. <li>Add the following example to your 'httpd.conf' (for example '/usr/local/apache2/conf/httpd.conf')
  2969. <pre class="program">LoadModule ruby_module modules/mod_ruby.so
  2970. &lt;IfModule mod_ruby.c&gt;
  2971. RubyRequire apache/ruby-run
  2972. RubyRequire apache/eruby-run
  2973. RubyRequire apache/erubis-run
  2974. &lt;Location /erubis&gt;
  2975. SetHandler ruby-object
  2976. RubyHandler Apache::ErubisRun.instance
  2977. &lt;/Location&gt;
  2978. &lt;Files *.rhtml&gt;
  2979. SetHandler ruby-object
  2980. RubyHandler Apache::ErubisRun.instance
  2981. &lt;/Files&gt;
  2982. &lt;/IfModule&gt;
  2983. </pre>
  2984. </li>
  2985. <li>Restart Apache web server.
  2986. <pre class="terminal">$ sudo /usr/local/apache2/bin/apachectl stop
  2987. $ sudo /usr/local/apache2/bin/apachectl start
  2988. </pre>
  2989. </li>
  2990. <li>Create *.rhtml file, for example:
  2991. <pre class="program">&lt;html&gt;
  2992. &lt;body&gt;
  2993. Now is &lt;%= Time.now %&gt;
  2994. Erubis version is &lt;%= Erubis::VERSION %&gt;
  2995. &lt;/body&gt;
  2996. &lt;/html&gt;
  2997. </pre>
  2998. </li>
  2999. <li>Change mode of your directory to be writable by web server process.
  3000. <pre class="terminal">$ cd /usr/local/apache2/htdocs/erubis
  3001. $ sudo chgrp daemon .
  3002. $ sudo chmod 775 .
  3003. </pre>
  3004. </li>
  3005. <li>Access the *.rhtml file and you'll get the web page.
  3006. </li>
  3007. </ol>
  3008. <p>You must set your directories to be writable by web server process, because
  3009. Apache::ErubisRun calls Erubis::Eruby.load_file() internally which creates cache files
  3010. in the same directory in which '*.rhtml' file exists.
  3011. </p>
  3012. <br>
  3013. <a name="topics-index-cgi"></a>
  3014. <h3 class="section2">Helper CGI Script for Apache</h3>
  3015. <p>Erubis provides helper CGI script for Apache.
  3016. Using this script, it is very easy to publish *.rhtml files as *.html.
  3017. </p>
  3018. <pre class="terminal">### install Erubis
  3019. $ tar xzf erubis-X.X.X.tar.gz
  3020. $ cd erubis-X.X.X/
  3021. $ ruby setup.py install
  3022. ### copy files to ~/public_html
  3023. $ mkdir -p ~/public_html
  3024. $ cp public_html/_htaccess ~/public_html/.htaccess
  3025. $ cp public_html/index.cgi ~/public_html/
  3026. $ cp public_html/index.rhtml ~/public_html/
  3027. ### add executable permission to index.cgi
  3028. $ chmod a+x ~/public_html/index.cgi
  3029. ### edit .htaccess
  3030. $ vi ~/public_html/.htaccess
  3031. ### (optional) edit index.cgi to configure
  3032. $ vi ~/public_html/index.cgi
  3033. </pre>
  3034. <p>Edit ~/public_html/.htaccess and modify user name.
  3035. </p>
  3036. <div class="program_caption">
  3037. ~/public_html/.htaccess</div>
  3038. <pre class="program">## enable mod_rewrie
  3039. RewriteEngine on
  3040. ## deny access to *.rhtml and *.cache
  3041. #RewriteRule \.(rhtml|cache)$ - [R=404,L]
  3042. RewriteRule \.(rhtml|cache)$ - [F,L]
  3043. ## rewrite only if requested file is not found
  3044. RewriteCond %{SCRIPT_FILENAME} !-f
  3045. ## handle request to *.html and directories by index.cgi
  3046. RewriteRule (\.html|/|^)$ /~<strong>username</strong>/index.cgi
  3047. #RewriteRule (\.html|/|^)$ index.cgi
  3048. </pre>
  3049. <p>After these steps, *.rhtml will be published as *.html.
  3050. For example, if you access to <code>http://<em>host</em>.<em>domain</em>/~<em>username</em>/index.html</code> (or <code>http://<em>host</em>.<em>domain</em>/~<em>username</em>/</code>), file <code>~/public_html/index.rhtml</code> will be displayed.
  3051. </p>
  3052. <br>
  3053. <a name="topics-defmethod"></a>
  3054. <h3 class="section2">Define method</h3>
  3055. <p>Erubis::Eruby#def_method() defines instance method or singleton method.
  3056. </p>
  3057. <a name="def_method.rb"></a>
  3058. <pre class="program">require 'erubis'
  3059. s = "hello &lt;%= name %&gt;"
  3060. eruby = Erubis::Eruby.new(s)
  3061. filename = 'hello.rhtml'
  3062. ## define instance method to Dummy class (or module)
  3063. class Dummy; end
  3064. <strong>eruby.def_method(Dummy, 'render(name)', filename)</strong> # filename is optional
  3065. p Dummy.new.render('world') #=&gt; "hello world"
  3066. ## define singleton method to dummy object
  3067. obj = Object.new
  3068. <strong>eruby.def_method(obj, 'render(name)', filename)</strong> # filename is optional
  3069. p obj.render('world') #=&gt; "hello world"
  3070. </pre>
  3071. <br>
  3072. <a name="topics-benchmark"></a>
  3073. <h3 class="section2">Benchmark</h3>
  3074. <p>A benchmark script is included in Erubis package at 'erubis-2.7.0/benchark/' directory.
  3075. Here is an example result of benchmark.
  3076. </p>
  3077. <div class="terminal_caption">
  3078. MacOS X 10.4 Tiger, Intel CoreDuo 1.83GHz, Ruby1.8.6, eruby1.0.5, gcc4.0.1</div>
  3079. <pre class="terminal">$ cd erubis-2.7.0/benchmark/
  3080. $ ruby bench.rb -n 10000 -m execute
  3081. *** ntimes=10000, testmode=execute
  3082. user system total real
  3083. eruby 12.720000 0.240000 <strong>12.960000</strong> ( 12.971888)
  3084. ERB 36.760000 0.350000 <strong>37.110000</strong> ( 37.112019)
  3085. ERB(cached) 11.990000 0.440000 <strong>12.430000</strong> ( 12.430375)
  3086. Erubis::Eruby 10.840000 0.300000 <strong>11.140000</strong> ( 11.144426)
  3087. Erubis::Eruby(cached) 7.540000 0.410000 <strong>7.950000</strong> ( 7.969305)
  3088. Erubis::FastEruby 10.440000 0.300000 <strong>10.740000</strong> ( 10.737808)
  3089. Erubis::FastEruby(cached) 6.940000 0.410000 <strong>7.350000</strong> ( 7.353666)
  3090. Erubis::TinyEruby 9.550000 0.290000 9.840000 ( 9.851729)
  3091. Erubis::ArrayBufferEruby 11.010000 0.300000 11.310000 ( 11.314339)
  3092. Erubis::PrintOutEruby 11.640000 0.290000 11.930000 ( 11.942141)
  3093. Erubis::StdoutEruby 11.590000 0.300000 11.890000 ( 11.886512)
  3094. </pre>
  3095. <p>This shows that...
  3096. </p>
  3097. <ul type="disc">
  3098. <li>Erubis::Eruby runs more than 10 percent faster than eruby.
  3099. </li>
  3100. <li>Erubis::Eruby runs about 3 times faster than ERB.
  3101. </li>
  3102. <li>Caching (by Erubis::Eruby.load_file()) makes Erubis about 40-50 percent faster.
  3103. </li>
  3104. <li>Erubis::FastEruby is a litte faster than Erubis::Eruby.
  3105. </li>
  3106. <li>Array buffer (ArrayBufferEnhancer) is a little slower than string buffer (StringBufferEnhancer which Erubis::Eruby includes)
  3107. </li>
  3108. <li>$stdout and print() make Erubis a little slower.
  3109. </li>
  3110. <li>Erubis::TinyEruby (at 'erubis/tiny.rb') is the fastest in all eRuby implementations when no caching.
  3111. </li>
  3112. </ul>
  3113. <p>Escaping HTML characters (such as '&lt; &gt; &amp; "') makes Erubis more faster than eruby and ERB,
  3114. because Erubis::XmlHelper#escape_xml() works faster than CGI.escapeHTML() and ERB::Util#h().
  3115. 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.
  3116. </p>
  3117. <div class="terminal_caption">
  3118. When escaping HTML characters with option '-e'</div>
  3119. <pre class="terminal">$ ruby bench.rb -n 10000 -m execute -ep
  3120. *** ntimes=10000, testmode=execute
  3121. user system total real
  3122. eruby 21.700000 0.290000 <strong>21.990000</strong> ( 22.050687)
  3123. ERB 45.140000 0.390000 <strong>45.530000</strong> ( 45.536976)
  3124. ERB(cached) 20.340000 0.470000 <strong>20.810000</strong> ( 20.822653)
  3125. Erubis::Eruby 14.830000 0.310000 <strong>15.140000</strong> ( 15.147930)
  3126. Erubis::Eruby(cached) 11.090000 0.420000 <strong>11.510000</strong> ( 11.514954)
  3127. Erubis::FastEruby 14.850000 0.310000 <strong>15.160000</strong> ( 15.172499)
  3128. Erubis::FastEruby(cached) 10.970000 0.430000 <strong>11.400000</strong> ( 11.399605)
  3129. Erubis::ArrayBufferEruby 14.970000 0.300000 15.270000 ( 15.281061)
  3130. Erubis::PrintOutEruby 15.780000 0.300000 16.080000 ( 16.088289)
  3131. Erubis::StdoutEruby 15.840000 0.310000 16.150000 ( 16.235338)
  3132. </pre>
  3133. <br>
  3134. <br>
  3135. <a name="command"></a>
  3136. <h2 class="section1">Command Reference</h2>
  3137. <a name="command-usage"></a>
  3138. <h3 class="section2">Usage</h3>
  3139. <p>erubis [..options..] [<em>file</em> ...]
  3140. </p>
  3141. <br>
  3142. <a name="command-options"></a>
  3143. <h3 class="section2">Options</h3>
  3144. <dl class="dl3" compact>
  3145. <dt class="dt3"><b>
  3146. -h, --help </b></dt>
  3147. <dd class="dd3">
  3148. Help.
  3149. </dd>
  3150. <dt class="dt3"><b>
  3151. -v </b></dt>
  3152. <dd class="dd3">
  3153. Release version.
  3154. </dd>
  3155. <dt class="dt3"><b>
  3156. -x </b></dt>
  3157. <dd class="dd3">
  3158. Show compiled source.
  3159. </dd>
  3160. <dt class="dt3"><b>
  3161. -X </b></dt>
  3162. <dd class="dd3">
  3163. Show compiled source but only Ruby code.
  3164. This is equivarent to '-E NoText'.
  3165. </dd>
  3166. <dt class="dt3"><b>
  3167. -N </b></dt>
  3168. <dd class="dd3">
  3169. Numbering: add line numbers. (for '-x/-X')
  3170. </dd>
  3171. <dt class="dt3"><b>
  3172. -U </b></dt>
  3173. <dd class="dd3">
  3174. Unique mode: zip empty lines into a line. (for '-x/-X')
  3175. </dd>
  3176. <dt class="dt3"><b>
  3177. -C </b></dt>
  3178. <dd class="dd3">
  3179. Compact: remove empty lines. (for '-x/-X')
  3180. </dd>
  3181. <dt class="dt3"><b>
  3182. -b </b></dt>
  3183. <dd class="dd3">
  3184. Body only: no preamble nor postamble. (for '-x/-X')
  3185. This is equivarent to '--preamble=false --postamble=false'.
  3186. </dd>
  3187. <dt class="dt3"><b>
  3188. -z </b></dt>
  3189. <dd class="dd3">
  3190. Syntax checking.
  3191. </dd>
  3192. <dt class="dt3"><b>
  3193. -e </b></dt>
  3194. <dd class="dd3">
  3195. Escape. This is equivarent to '-E Escape'.
  3196. </dd>
  3197. <dt class="dt3"><b>
  3198. -p pattern </b></dt>
  3199. <dd class="dd3">
  3200. Embedded pattern (default '&lt;% %&gt;').
  3201. This is equivarent to '--pattern=<em>pattern</em>'.
  3202. </dd>
  3203. <dt class="dt3"><b>
  3204. -l lang </b></dt>
  3205. <dd class="dd3">
  3206. Language name.
  3207. This option makes erubis command to compile script but no execute.
  3208. </dd>
  3209. <dt class="dt3"><b>
  3210. -E enhacers </b></dt>
  3211. <dd class="dd3">
  3212. Enhancer name (Escape, PercentLine, ...).
  3213. It is able to specify several enhancer name separating with ','
  3214. (ex. -f Escape,PercentLine,HeaderFooter).
  3215. </dd>
  3216. <dt class="dt3"><b>
  3217. -I path </b></dt>
  3218. <dd class="dd3">
  3219. Require library path ($:).
  3220. It is able to specify several paths separating with ','
  3221. (ex. -f path1,path2,path3).
  3222. </dd>
  3223. <dt class="dt3"><b>
  3224. -K kanji </b></dt>
  3225. <dd class="dd3">
  3226. Kanji code (euc, sjis, utf8, or none) (default none).
  3227. </dd>
  3228. <dt class="dt3"><b>
  3229. -f datafile </b></dt>
  3230. <dd class="dd3">
  3231. Context data file in YAML format ('*.yaml', '*.yml') or
  3232. Ruby script ('*.rb').
  3233. It is able to specify several filenames separating with ','
  3234. (ex. -f file1,file2,file3).
  3235. </dd>
  3236. <dt class="dt3"><b>
  3237. -c context </b></dt>
  3238. <dd class="dd3">
  3239. Context data string in YAML inline style or Ruby code.
  3240. </dd>
  3241. <dt class="dt3"><b>
  3242. -T </b></dt>
  3243. <dd class="dd3">
  3244. Don't expand tab characters in YAML file.
  3245. </dd>
  3246. <dt class="dt3"><b>
  3247. -S </b></dt>
  3248. <dd class="dd3">
  3249. Convert mapping key from string to symbol in YAML file.
  3250. </dd>
  3251. <dt class="dt3"><b>
  3252. -B </b></dt>
  3253. <dd class="dd3">
  3254. invoke Eruby#result() instead of Eruby#evaluate()
  3255. </dd>
  3256. <dt class="dt3"><b>
  3257. --pi[=name] </b></dt>
  3258. <dd class="dd3">
  3259. parse '&lt;?name ... ?&gt;' instead of '&lt;% ... %&gt;'
  3260. </dd>
  3261. <dt class="dt3"><b>
  3262. --trim=false </b></dt>
  3263. <dd class="dd3">
  3264. No trimming spaces around '&lt;% %&gt;'.
  3265. </dd>
  3266. </dl>
  3267. <br>
  3268. <a name="command-props"></a>
  3269. <h3 class="section2">Properties</h3>
  3270. <p>Some Eruby classes can take optional properties to change it's compile option.
  3271. For example, property '--indent=" "' may change indentation of compiled source code.
  3272. Try 'erubis -h' for details.
  3273. </p>
  3274. <br>
  3275. <br>
  3276. </div>
  3277. </blockquote>
  3278. </body>
  3279. </html>