erubis 2.0.1 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/ChangeLog +150 -2
- data/LGPL +504 -0
- data/README.txt +14 -8
- data/benchmark/erubybench.rb +1 -1
- data/benchmark/erubybench.rhtml +3 -3
- data/bin/erubis +1 -1
- data/bin/notext +154 -0
- data/contrib/erubis +1036 -374
- data/contrib/inline-require +5 -3
- data/doc-api/classes/Erubis.html +43 -1
- data/doc-api/classes/Erubis/ArrayBufferEnhancer.html +14 -14
- data/doc-api/classes/Erubis/ArrayEnhancer.html +14 -14
- data/doc-api/classes/Erubis/Basic.html +112 -0
- data/doc-api/classes/Erubis/Basic/Converter.html +318 -0
- data/doc-api/classes/Erubis/Basic/Engine.html +130 -0
- data/doc-api/classes/Erubis/BiPatternEnhancer.html +14 -14
- data/doc-api/classes/Erubis/CGenerator.html +386 -0
- data/doc-api/classes/Erubis/Context.html +31 -32
- data/doc-api/classes/Erubis/Converter.html +231 -0
- data/doc-api/classes/Erubis/Ec.html +9 -283
- data/doc-api/classes/Erubis/Ejava.html +9 -274
- data/doc-api/classes/Erubis/Ejavascript.html +9 -298
- data/doc-api/classes/Erubis/Engine.html +68 -508
- data/doc-api/classes/Erubis/Eperl.html +9 -259
- data/doc-api/classes/Erubis/Ephp.html +9 -239
- data/doc-api/classes/Erubis/ErubisError.html +2 -2
- data/doc-api/classes/Erubis/Eruby.html +4 -187
- data/doc-api/classes/Erubis/EscapeEnhancer.html +9 -11
- data/doc-api/classes/Erubis/Escheme.html +9 -272
- data/doc-api/classes/Erubis/Evaluator.html +212 -0
- data/doc-api/classes/Erubis/Generator.html +410 -0
- data/doc-api/classes/Erubis/HeaderFooterEnhancer.html +16 -16
- data/doc-api/classes/Erubis/Helper.html +112 -0
- data/doc-api/classes/Erubis/Helper/CachedRailsTemplate.html +198 -0
- data/doc-api/classes/Erubis/Helper/RailsTemplate.html +366 -0
- data/doc-api/classes/Erubis/JavaGenerator.html +359 -0
- data/doc-api/classes/Erubis/JavascriptGenerator.html +381 -0
- data/doc-api/classes/Erubis/Main.html +27 -25
- data/doc-api/classes/Erubis/NoCodeEnhancer.html +249 -0
- data/doc-api/classes/Erubis/NoCodeEruby.html +120 -0
- data/doc-api/classes/Erubis/NoTextEnhancer.html +8 -8
- data/doc-api/classes/Erubis/NotSupportedError.html +119 -0
- data/doc-api/classes/Erubis/OptimizedEruby.html +22 -304
- data/doc-api/classes/Erubis/OptimizedGenerator.html +439 -0
- data/doc-api/classes/Erubis/OptimizedXmlEruby.html +7 -7
- data/doc-api/classes/Erubis/PI.html +124 -0
- data/doc-api/classes/Erubis/PI/Converter.html +318 -0
- data/doc-api/classes/Erubis/PI/Ec.html +166 -0
- data/doc-api/classes/Erubis/PI/Ejava.html +166 -0
- data/doc-api/classes/Erubis/PI/Ejavascript.html +166 -0
- data/doc-api/classes/Erubis/PI/Engine.html +122 -0
- data/doc-api/classes/Erubis/PI/Eperl.html +166 -0
- data/doc-api/classes/Erubis/PI/Ephp.html +166 -0
- data/doc-api/classes/Erubis/PI/Eruby.html +155 -0
- data/doc-api/classes/Erubis/PI/Escheme.html +166 -0
- data/doc-api/classes/Erubis/PI/TinyEruby.html +305 -0
- data/doc-api/classes/Erubis/PercentLineEnhancer.html +7 -7
- data/doc-api/classes/Erubis/PerlGenerator.html +344 -0
- data/doc-api/classes/Erubis/PhpGenerator.html +350 -0
- data/doc-api/classes/Erubis/PrintEnabledEnhancer.html +24 -24
- data/doc-api/classes/Erubis/PrintOutEnhancer.html +35 -35
- data/doc-api/classes/Erubis/RubyEvaluator.html +190 -0
- data/doc-api/classes/Erubis/RubyGenerator.html +327 -0
- data/doc-api/classes/Erubis/SchemeGenerator.html +382 -0
- data/doc-api/classes/Erubis/SimplifyEnhancer.html +17 -13
- data/doc-api/classes/Erubis/StdoutEnhancer.html +14 -14
- data/doc-api/classes/Erubis/StringBufferEnhancer.html +14 -14
- data/doc-api/classes/Erubis/TinyEruby.html +45 -40
- data/doc-api/classes/Erubis/XmlHelper.html +14 -14
- data/doc-api/created.rid +1 -1
- data/doc-api/files/__/README_txt.html +27 -9
- data/doc-api/files/erubis/context_rb.html +107 -0
- data/doc-api/files/erubis/converter_rb.html +114 -0
- data/doc-api/files/erubis/engine/ec_rb.html +2 -2
- data/doc-api/files/erubis/engine/ejava_rb.html +2 -2
- data/doc-api/files/erubis/engine/ejavascript_rb.html +2 -2
- data/doc-api/files/erubis/engine/enhanced_rb.html +2 -2
- data/doc-api/files/erubis/engine/eperl_rb.html +2 -2
- data/doc-api/files/erubis/engine/ephp_rb.html +2 -2
- data/doc-api/files/erubis/engine/eruby_rb.html +3 -2
- data/doc-api/files/erubis/engine/escheme_rb.html +2 -2
- data/doc-api/files/erubis/engine/optimized_rb.html +2 -2
- data/doc-api/files/erubis/engine_rb.html +6 -3
- data/doc-api/files/erubis/enhancer_rb.html +2 -9
- data/doc-api/files/erubis/error_rb.html +107 -0
- data/doc-api/files/erubis/evaluator_rb.html +115 -0
- data/doc-api/files/erubis/generator_rb.html +114 -0
- data/doc-api/files/erubis/helper/rails_rb.html +140 -0
- data/doc-api/files/erubis/helper_rb.html +1 -1
- data/doc-api/files/erubis/local-setting_rb.html +1 -1
- data/doc-api/files/erubis/main_rb.html +2 -2
- data/doc-api/files/erubis/tiny_rb.html +2 -2
- data/doc-api/files/erubis_rb.html +2 -2
- data/doc-api/fr_class_index.html +32 -0
- data/doc-api/fr_file_index.html +6 -0
- data/doc-api/fr_method_index.html +178 -136
- data/doc/users-guide.html +538 -199
- data/examples/{Makefile → basic/Makefile} +11 -11
- data/examples/basic/example.ec +42 -0
- data/examples/{example.ejava → basic/example.ejava} +5 -1
- data/examples/{example.ejavascript → basic/example.ejs} +0 -0
- data/examples/{example.eperl → basic/example.eperl} +2 -2
- data/examples/{example.ephp → basic/example.ephp} +0 -0
- data/examples/{example.eruby → basic/example.eruby} +0 -0
- data/examples/{example.escheme → basic/example.escheme} +0 -0
- data/examples/pi/Makefile +54 -0
- data/examples/pi/example.ec +42 -0
- data/examples/pi/example.ejava +45 -0
- data/examples/pi/example.ejs +16 -0
- data/examples/pi/example.eperl +16 -0
- data/examples/pi/example.ephp +17 -0
- data/examples/pi/example.eruby +15 -0
- data/examples/pi/example.escheme +26 -0
- data/lib/erubis.rb +15 -8
- data/lib/erubis/context.rb +54 -0
- data/lib/erubis/converter.rb +307 -0
- data/lib/erubis/engine.rb +57 -227
- data/lib/erubis/engine/ec.rb +31 -20
- data/lib/erubis/engine/ejava.rb +33 -21
- data/lib/erubis/engine/ejavascript.rb +30 -18
- data/lib/erubis/engine/enhanced.rb +7 -2
- data/lib/erubis/engine/eperl.rb +33 -19
- data/lib/erubis/engine/ephp.rb +32 -14
- data/lib/erubis/engine/eruby.rb +34 -7
- data/lib/erubis/engine/escheme.rb +34 -15
- data/lib/erubis/engine/optimized.rb +25 -11
- data/lib/erubis/enhancer.rb +53 -19
- data/lib/erubis/error.rb +24 -0
- data/lib/erubis/evaluator.rb +71 -0
- data/lib/erubis/generator.rb +83 -0
- data/lib/erubis/helper.rb +1 -1
- data/lib/erubis/helper/rails.rb +159 -0
- data/lib/erubis/local-setting.rb +1 -1
- data/lib/erubis/main.rb +87 -46
- data/lib/erubis/tiny.rb +81 -7
- data/test/assert-text-equal.rb +1 -1
- data/test/test-engines.rb +8 -9
- data/test/test-erubis.rb +196 -18
- data/test/{test-bin.rb → test-main.rb} +123 -21
- data/test/test-notext.rb +27 -0
- data/test/test.rb +5 -3
- data/test/testutil.rb +25 -36
- metadata +75 -13
- data/COPYING +0 -340
- data/examples/example.ec +0 -24
data/doc/users-guide.html
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
|
|
15
15
|
<div align="left"><h1>Erubis Users' Guide</h1></div>
|
|
16
16
|
<div align="left">
|
|
17
|
-
last update: $Date: 2006-
|
|
17
|
+
last update: $Date: 2006-09-24 21:01:00 +0900 (Sun, 24 Sep 2006) $<br>
|
|
18
18
|
</div>
|
|
19
19
|
|
|
20
20
|
<a name="preface"></a>
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
It has the following features.
|
|
24
24
|
</p>
|
|
25
25
|
<ul type="disc">
|
|
26
|
-
<li>Very fast, almost three times faster than ERB and even
|
|
26
|
+
<li>Very fast, almost three times faster than ERB and even faster than eruby (implemented in C)
|
|
27
27
|
</li>
|
|
28
28
|
<li>Auto escaping support
|
|
29
29
|
</li>
|
|
@@ -31,13 +31,17 @@ It has the following features.
|
|
|
31
31
|
</li>
|
|
32
32
|
<li>Embedded pattern changeable (default '<% %>')
|
|
33
33
|
</li>
|
|
34
|
-
<li>
|
|
34
|
+
<li>Enable to handle Processing Instructions (PI) as embedded pattern (ex. '<?rb ... ?>')
|
|
35
|
+
</li>
|
|
36
|
+
<li>Multi-language support (Ruby/PHP/C/Java/Scheme/Perl/Javascript)
|
|
35
37
|
</li>
|
|
36
38
|
<li>Context object available and easy to combine eRuby template with YAML datafile
|
|
37
39
|
</li>
|
|
38
40
|
<li>Print statement available
|
|
39
41
|
</li>
|
|
40
|
-
<li>Easy to
|
|
42
|
+
<li>Easy to expand and customize in subclass
|
|
43
|
+
</li>
|
|
44
|
+
<li>Ruby on Rails support
|
|
41
45
|
</li>
|
|
42
46
|
</ul>
|
|
43
47
|
<p>Erubis is implemented in pure Ruby. It requires Ruby 1.8 or higher.
|
|
@@ -69,6 +73,8 @@ It has the following features.
|
|
|
69
73
|
</li>
|
|
70
74
|
<li><a href="#tut-preamble">Preamble and Postamble</a>
|
|
71
75
|
</li>
|
|
76
|
+
<li><a href="#tut-pi">Processing Instruction (PI) Converter</a>
|
|
77
|
+
</li>
|
|
72
78
|
</ul>
|
|
73
79
|
</li>
|
|
74
80
|
<li><a href="#enhancer">Enhancer</a>
|
|
@@ -89,6 +95,8 @@ It has the following features.
|
|
|
89
95
|
</li>
|
|
90
96
|
<li><a href="#notext-enhancer">NoTextEnhancer</a>
|
|
91
97
|
</li>
|
|
98
|
+
<li><a href="#nocode-enhancer">NoCodeEnhancer</a>
|
|
99
|
+
</li>
|
|
92
100
|
<li><a href="#simplify-enhancer">SimplifyEnhancer</a>
|
|
93
101
|
</li>
|
|
94
102
|
<li><a href="#bipattern-enhancer">BiPatternEnhancer</a>
|
|
@@ -119,7 +127,11 @@ It has the following features.
|
|
|
119
127
|
<ul>
|
|
120
128
|
<li><a href="#topics-tinyeruby">TinyEruby class</a>
|
|
121
129
|
</li>
|
|
122
|
-
<li><a href="#topics-
|
|
130
|
+
<li><a href="#topics-rails">Ruby on Rails Support</a>
|
|
131
|
+
</li>
|
|
132
|
+
<li><a href="#topics-php">NoTextEnhancer and NoCodeEnhancer in PHP</a>
|
|
133
|
+
</li>
|
|
134
|
+
<li><a href="#topics-notext">Command <code>notext</code></a>
|
|
123
135
|
</li>
|
|
124
136
|
<li><a href="#topics-benchmark">Benchmark</a>
|
|
125
137
|
</li>
|
|
@@ -185,10 +197,10 @@ $ contrib/inline-require -I lib bin/erubis > contrib/erubis
|
|
|
185
197
|
<div class="program_caption">
|
|
186
198
|
example1.eruby</div>
|
|
187
199
|
<pre class="program"><ul>
|
|
188
|
-
<
|
|
189
|
-
<li><
|
|
190
|
-
<
|
|
191
|
-
<
|
|
200
|
+
<strong><% for item in list %></strong>
|
|
201
|
+
<li><strong><%= item %></strong></li>
|
|
202
|
+
<strong><% end %></strong>
|
|
203
|
+
<strong><%# here is ignored because starting with '#' %></strong>
|
|
192
204
|
</ul>
|
|
193
205
|
</pre>
|
|
194
206
|
<a name="example1.rb"></a>
|
|
@@ -196,14 +208,20 @@ example1.eruby</div>
|
|
|
196
208
|
example1.rb</div>
|
|
197
209
|
<pre class="program">require 'erubis'
|
|
198
210
|
input = File.read('example1.eruby')
|
|
199
|
-
eruby = <
|
|
211
|
+
eruby = <strong>Erubis::Eruby.new(input)</strong> # create Eruby object
|
|
200
212
|
|
|
201
213
|
puts "---------- script source ---"
|
|
202
|
-
puts <
|
|
214
|
+
puts <strong>eruby.src</strong> # print script source
|
|
203
215
|
|
|
204
216
|
puts "---------- result ----------"
|
|
205
217
|
list = ['aaa', 'bbb', 'ccc']
|
|
206
|
-
puts <
|
|
218
|
+
puts <strong>eruby.result(binding())</strong> # get result
|
|
219
|
+
|
|
220
|
+
## # or
|
|
221
|
+
## eruby = Erubis::Eruby.new
|
|
222
|
+
## input = File.read('example1.eruby')
|
|
223
|
+
## src = eruby.convert(input)
|
|
224
|
+
## eval src
|
|
207
225
|
</pre>
|
|
208
226
|
<div class="terminal_caption">
|
|
209
227
|
output</div>
|
|
@@ -228,7 +246,7 @@ _buf.join
|
|
|
228
246
|
</p>
|
|
229
247
|
<div class="terminal_caption">
|
|
230
248
|
example of command-line option '-x'</div>
|
|
231
|
-
<pre class="terminal">$ erubis <
|
|
249
|
+
<pre class="terminal">$ erubis <strong>-x</strong> example1.eruby
|
|
232
250
|
_buf = []; _buf << '<ul>
|
|
233
251
|
'; for item in list
|
|
234
252
|
_buf << ' <li>'; _buf << ( item ).to_s; _buf << '</li>
|
|
@@ -273,7 +291,7 @@ _buf.join
|
|
|
273
291
|
</p>
|
|
274
292
|
<div class="terminal_caption">
|
|
275
293
|
compiled source code with command-line option '-T'</div>
|
|
276
|
-
<pre class="terminal">$ erubis -x <
|
|
294
|
+
<pre class="terminal">$ erubis -x <strong>-T</strong> example2.eruby
|
|
277
295
|
_buf = []; _buf << '<ul>
|
|
278
296
|
'; _buf << ' '; for item in list ; _buf << '
|
|
279
297
|
'; _buf << ' <li>
|
|
@@ -291,7 +309,7 @@ _buf.join
|
|
|
291
309
|
example2.rb</div>
|
|
292
310
|
<pre class="program">require 'erubis'
|
|
293
311
|
input = File.read('example2.eruby')
|
|
294
|
-
eruby = Erubis::Eruby.new(input<
|
|
312
|
+
eruby = Erubis::Eruby.new(input<strong>, :trim=>false</strong>)
|
|
295
313
|
|
|
296
314
|
puts "---------- script source ---"
|
|
297
315
|
puts eruby.src # print script source
|
|
@@ -339,13 +357,13 @@ _buf.join
|
|
|
339
357
|
Erubis::Eruby class act as the following:
|
|
340
358
|
</p>
|
|
341
359
|
<ul type="disc">
|
|
342
|
-
<li><code><%= <
|
|
360
|
+
<li><code><%= <em>expr</em> %></code> - not escaped.
|
|
343
361
|
</li>
|
|
344
|
-
<li><code><%== <
|
|
362
|
+
<li><code><%== <em>expr</em> %></code> - escaped.
|
|
345
363
|
</li>
|
|
346
|
-
<li><code><%=== <
|
|
364
|
+
<li><code><%=== <em>expr</em> %></code> - out to $stderr.
|
|
347
365
|
</li>
|
|
348
|
-
<li><code><%==== <
|
|
366
|
+
<li><code><%==== <em>expr</em> %></code> - ignored.
|
|
349
367
|
</li>
|
|
350
368
|
</ul>
|
|
351
369
|
<p>Erubis::EscapedEruby<sup>(<a href="#fnref:1" name="fnlink:1">*1</a>)</sup> class handle '<%= %>' as escaped and '<%== %>' as not escaped.
|
|
@@ -356,9 +374,9 @@ Also Erubis::XmlEruby class (which is equivalent to Erubis::EscapedEruby) is pro
|
|
|
356
374
|
<div class="program_caption">
|
|
357
375
|
example3.eruby</div>
|
|
358
376
|
<pre class="program"><% for item in list %>
|
|
359
|
-
<p><
|
|
360
|
-
<p><
|
|
361
|
-
<p><
|
|
377
|
+
<p><strong><%=</strong> item <strong>%></strong></p>
|
|
378
|
+
<p><strong><%==</strong> item <strong>%></strong></p>
|
|
379
|
+
<p><strong><%===</strong> item <strong>%></strong></p>
|
|
362
380
|
|
|
363
381
|
<% end %>
|
|
364
382
|
</pre>
|
|
@@ -367,13 +385,13 @@ example3.eruby</div>
|
|
|
367
385
|
example3.rb</div>
|
|
368
386
|
<pre class="program">require 'erubis'
|
|
369
387
|
input = File.read('example3.eruby')
|
|
370
|
-
eruby = Erubis::<
|
|
388
|
+
eruby = Erubis::<strong>EscapedEruby</strong>.new(input) # or Erubis::XmlEruby
|
|
371
389
|
|
|
372
390
|
puts "---------- script source ---"
|
|
373
391
|
puts eruby.src # print script source
|
|
374
392
|
|
|
375
393
|
puts "---------- result ----------"
|
|
376
|
-
<
|
|
394
|
+
<strong>list = ['<aaa>', 'b&b', '"ccc"']</strong>
|
|
377
395
|
puts eruby.result(binding()) # get result
|
|
378
396
|
</pre>
|
|
379
397
|
<div class="terminal_caption">
|
|
@@ -381,9 +399,9 @@ output</div>
|
|
|
381
399
|
<pre class="terminal">$ ruby example3.rb 2> stderr.log
|
|
382
400
|
---------- script source ---
|
|
383
401
|
_buf = []; for item in list
|
|
384
|
-
_buf << ' <p>'; <
|
|
385
|
-
<p>'; <
|
|
386
|
-
<p>'; <
|
|
402
|
+
_buf << ' <p>'; <strong>_buf << Erubis::XmlHelper.escape_xml( item );</strong> _buf << '</p>
|
|
403
|
+
<p>'; <strong>_buf << ( item ).to_s;</strong> _buf << '</p>
|
|
404
|
+
<p>'; <strong>$stderr.puts("*** debug: item=#{(item).inspect}");</strong> _buf << '</p>
|
|
387
405
|
|
|
388
406
|
'; end
|
|
389
407
|
_buf.join
|
|
@@ -405,10 +423,10 @@ $ cat stderr.log
|
|
|
405
423
|
*** debug: item="b&b"
|
|
406
424
|
*** debug: item="\"ccc\""
|
|
407
425
|
</pre>
|
|
408
|
-
<p>The command-line option '-e'
|
|
426
|
+
<p>The command-line option '-e' will do the same action as Erubis::EscapedEruby.
|
|
409
427
|
This option is available for any language.
|
|
410
428
|
</p>
|
|
411
|
-
<pre class="terminal">$ erubis -l ruby <
|
|
429
|
+
<pre class="terminal">$ erubis -l ruby <strong>-e</strong> example3.eruby
|
|
412
430
|
_buf = []; for item in list
|
|
413
431
|
_buf << ' <p>'; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << '</p>
|
|
414
432
|
<p>'; _buf << ( item ).to_s; _buf << '</p>
|
|
@@ -417,19 +435,19 @@ _buf = []; for item in list
|
|
|
417
435
|
'; end
|
|
418
436
|
_buf.join
|
|
419
437
|
</pre>
|
|
420
|
-
<p>Escaping function (default 'Erubis::XmlHelper.escape_xml()') can be changed by command-line property '--
|
|
438
|
+
<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.
|
|
421
439
|
</p>
|
|
422
440
|
<div class="program_caption">
|
|
423
441
|
example to override Erubis::Eruby#escaped_expr()</div>
|
|
424
442
|
<pre class="program">class CGIEruby < Erubis::Eruby
|
|
425
|
-
def <
|
|
443
|
+
def <strong>escaped_expr(code)</strong>
|
|
426
444
|
return "CGI.escapeHTML((#{code.strip}).to_s)"
|
|
427
445
|
#return "h(#{code.strip})"
|
|
428
446
|
end
|
|
429
447
|
end
|
|
430
448
|
|
|
431
449
|
class LatexEruby < Erubi::Eruby
|
|
432
|
-
def <
|
|
450
|
+
def <strong>escaped_expr(code)</strong>
|
|
433
451
|
return "(#{code}).gsub(/[%\\]/,'\\\\\&')"
|
|
434
452
|
end
|
|
435
453
|
end
|
|
@@ -438,8 +456,6 @@ end
|
|
|
438
456
|
<dl compact>
|
|
439
457
|
<dt>(<a name="fnref:1" href="#fnlink:1">*1</a>)</dt>
|
|
440
458
|
<dd>Erubis::EscapedEruby class includes Erubis::EscapeEnhancer which swtches the action of '<%= %>' and '<%== %>'.</dd>
|
|
441
|
-
<dt>(<a name="fnref:2" href="#fnlink:2">*2</a>)</dt>
|
|
442
|
-
<dd>Command-line option '-e' is equivarent to '-E Escape'.</dd>
|
|
443
459
|
</dl>
|
|
444
460
|
</div>
|
|
445
461
|
<br>
|
|
@@ -447,18 +463,18 @@ end
|
|
|
447
463
|
|
|
448
464
|
<a name="tut-pattern"></a>
|
|
449
465
|
<h3 class="section2">Embedded Pattern</h3>
|
|
450
|
-
<p>You can change embedded pattern '<code><% %></code>' to another
|
|
466
|
+
<p>You can change embedded pattern '<code><% %></code>' to another by command-line option '-p' or option '<code>:pattern=>...</code>' of Erubis::Eruby.new().
|
|
451
467
|
</p>
|
|
452
468
|
<a name="example4.eruby"></a>
|
|
453
469
|
<div class="program_caption">
|
|
454
470
|
example4.eruby</div>
|
|
455
|
-
<pre class="program"><
|
|
456
|
-
<p><
|
|
457
|
-
<
|
|
471
|
+
<pre class="program"><strong><!--%</strong> for item in list <strong>%--></strong>
|
|
472
|
+
<p><strong><!--%=</strong> item <strong>%--></strong></p>
|
|
473
|
+
<strong><!--%</strong> end <strong>%--></strong>
|
|
458
474
|
</pre>
|
|
459
475
|
<div class="terminal_caption">
|
|
460
476
|
compiled source code with command-line option '-p'</div>
|
|
461
|
-
<pre class="terminal">$ erubis -x <
|
|
477
|
+
<pre class="terminal">$ erubis -x <strong>-p '<!--% %-->'</strong> example4.eruby
|
|
462
478
|
_buf = []; for item in list
|
|
463
479
|
_buf << ' <p>'; _buf << ( item ).to_s; _buf << '</p>
|
|
464
480
|
'; end
|
|
@@ -469,7 +485,7 @@ _buf.join
|
|
|
469
485
|
example4.rb</div>
|
|
470
486
|
<pre class="program">require 'erubis'
|
|
471
487
|
input = File.read('example4.eruby')
|
|
472
|
-
eruby = Erubis::Eruby.new(input<
|
|
488
|
+
eruby = Erubis::Eruby.new(input<strong>, :pattern=>'<!--% %-->'</strong>)
|
|
473
489
|
# or '<(?:!--)?% %(?:--)?>'
|
|
474
490
|
|
|
475
491
|
puts "---------- script source ---"
|
|
@@ -510,9 +526,9 @@ In Erubis, Hash object and Erubis::Context object are available as context objec
|
|
|
510
526
|
<a name="example5.eruby"></a>
|
|
511
527
|
<div class="program_caption">
|
|
512
528
|
example5.eruby</div>
|
|
513
|
-
<pre class="program"><span><%= <
|
|
529
|
+
<pre class="program"><span><%= <strong>@val</strong> %></span>
|
|
514
530
|
<ul>
|
|
515
|
-
<% for item in <
|
|
531
|
+
<% for item in <strong>@list</strong> %>
|
|
516
532
|
<li><%= item %></li>
|
|
517
533
|
<% end %>
|
|
518
534
|
</ul>
|
|
@@ -526,16 +542,16 @@ eruby = Erubis::Eruby.new(input) # create Eruby object
|
|
|
526
542
|
|
|
527
543
|
## create context object
|
|
528
544
|
## (key means var name, which may be string or symbol.)
|
|
529
|
-
<
|
|
545
|
+
<strong>context = {
|
|
530
546
|
:val => 'Erubis Example',
|
|
531
547
|
'list' => ['aaa', 'bbb', 'ccc'],
|
|
532
|
-
}</
|
|
548
|
+
}</strong>
|
|
533
549
|
# or
|
|
534
550
|
# context = Erubis::Context.new()
|
|
535
551
|
# context['val'] = 'Erubis Example'
|
|
536
552
|
# context[:list] = ['aaa', 'bbb', 'ccc'],
|
|
537
553
|
|
|
538
|
-
puts <
|
|
554
|
+
puts <strong>eruby.evaluate(context)</strong> # get result
|
|
539
555
|
</pre>
|
|
540
556
|
<div class="terminal_caption">
|
|
541
557
|
output</div>
|
|
@@ -561,7 +577,7 @@ definition of result(binding) and evaluate(context)</div>
|
|
|
561
577
|
eval _h.keys.inject("") {|s,k| s << "#{k} = _h[#{k.inspect}];"}
|
|
562
578
|
_binding = binding()
|
|
563
579
|
end
|
|
564
|
-
return <
|
|
580
|
+
return <strong>eval(@src, _binding)</strong>
|
|
565
581
|
end
|
|
566
582
|
|
|
567
583
|
def evaluate(context)
|
|
@@ -571,7 +587,7 @@ def evaluate(context)
|
|
|
571
587
|
context = Erubis::Context.new
|
|
572
588
|
hash.each { |key, val| context[key] = val }
|
|
573
589
|
end
|
|
574
|
-
return <
|
|
590
|
+
return <strong>context.instance_eval(@src)</strong>
|
|
575
591
|
end
|
|
576
592
|
</pre>
|
|
577
593
|
<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.
|
|
@@ -584,13 +600,13 @@ example6.rb</div>
|
|
|
584
600
|
end
|
|
585
601
|
|
|
586
602
|
## any object can be a context object
|
|
587
|
-
<
|
|
588
|
-
<
|
|
589
|
-
<
|
|
603
|
+
<strong>mydata = MyData.new</strong>
|
|
604
|
+
<strong>mydata.val = 'Erubis Example'</strong>
|
|
605
|
+
<strong>mydata.list = ['aaa', 'bbb', 'ccc']</strong>
|
|
590
606
|
|
|
591
607
|
require 'erubis'
|
|
592
608
|
eruby = Erubis::Eruby.new(File.read('example5.eruby'))
|
|
593
|
-
puts eruby.evaluate(<
|
|
609
|
+
puts eruby.evaluate(<strong>mydata</strong>)
|
|
594
610
|
</pre>
|
|
595
611
|
<div class="terminal_caption">
|
|
596
612
|
output</div>
|
|
@@ -612,8 +628,8 @@ output</div>
|
|
|
612
628
|
<a name="example7.yaml"></a>
|
|
613
629
|
<div class="program_caption">
|
|
614
630
|
example7.yaml</div>
|
|
615
|
-
<pre class="program"><
|
|
616
|
-
<
|
|
631
|
+
<pre class="program"><strong>title:</strong> Users List
|
|
632
|
+
<strong>users:</strong>
|
|
617
633
|
- name: foo
|
|
618
634
|
mail: foo@mail.com
|
|
619
635
|
- name: bar
|
|
@@ -624,9 +640,9 @@ example7.yaml</div>
|
|
|
624
640
|
<a name="example7.eruby"></a>
|
|
625
641
|
<div class="program_caption">
|
|
626
642
|
example7.eruby</div>
|
|
627
|
-
<pre class="program"><h1><%= <
|
|
643
|
+
<pre class="program"><h1><%= <strong>@title</strong> %></h1>
|
|
628
644
|
<ul>
|
|
629
|
-
<% for user in <
|
|
645
|
+
<% for user in <strong>@users</strong> %>
|
|
630
646
|
<li>
|
|
631
647
|
<a href="mailto:<%= user['mail']%>"><%= user['name'] %></a>
|
|
632
648
|
</li>
|
|
@@ -641,10 +657,10 @@ input = File.read('example7.eruby')
|
|
|
641
657
|
eruby = Erubis::Eruby.new(input) # create Eruby object
|
|
642
658
|
|
|
643
659
|
## load YAML document as context object
|
|
644
|
-
<
|
|
645
|
-
<
|
|
660
|
+
<strong>require 'yaml'</strong>
|
|
661
|
+
<strong>context = YAML.load_file('example7.yaml')</strong>
|
|
646
662
|
|
|
647
|
-
puts <
|
|
663
|
+
puts <strong>eruby.evaluate(context)</strong> # get result
|
|
648
664
|
</pre>
|
|
649
665
|
<div class="terminal_caption">
|
|
650
666
|
output</div>
|
|
@@ -667,7 +683,7 @@ You don't have to write ruby script such as 'example7.rb'.
|
|
|
667
683
|
</p>
|
|
668
684
|
<div class="terminal_caption">
|
|
669
685
|
example of command-line option '-f'</div>
|
|
670
|
-
<pre class="terminal">$ erubis <
|
|
686
|
+
<pre class="terminal">$ erubis <strong>-f example7.yaml</strong> example7.eruby
|
|
671
687
|
<h1>Users List</h1>
|
|
672
688
|
<ul>
|
|
673
689
|
<li>
|
|
@@ -705,13 +721,13 @@ example8.eruby</div>
|
|
|
705
721
|
</p>
|
|
706
722
|
<pre class="terminal">## without '-b'
|
|
707
723
|
$ erubis -x example8.eruby
|
|
708
|
-
<
|
|
724
|
+
<strong>_buf = [];</strong> for item in @list
|
|
709
725
|
_buf << ' <b>'; _buf << ( item ).to_s; _buf << '</b>
|
|
710
726
|
'; end
|
|
711
|
-
<
|
|
727
|
+
<strong>_buf.join</strong>
|
|
712
728
|
|
|
713
729
|
## with '-b'
|
|
714
|
-
$ erubis -x <
|
|
730
|
+
$ erubis -x <strong>-b</strong> example8.eruby
|
|
715
731
|
for item in @list
|
|
716
732
|
_buf << ' <b>'; _buf << ( item ).to_s; _buf << '</b>
|
|
717
733
|
'; end
|
|
@@ -724,7 +740,7 @@ example8.rb</div>
|
|
|
724
740
|
<pre class="program">require 'erubis'
|
|
725
741
|
input = File.read('example8.eruby')
|
|
726
742
|
eruby1 = Erubis::Eruby.new(input)
|
|
727
|
-
eruby2 = Erubis::Eruby.new(input, <
|
|
743
|
+
eruby2 = Erubis::Eruby.new(input, <strong>:preamble=>false, :postamble=>false</strong>)
|
|
728
744
|
|
|
729
745
|
puts eruby1.src # print preamble and postamble
|
|
730
746
|
puts "--------------"
|
|
@@ -745,6 +761,118 @@ _buf.join
|
|
|
745
761
|
<br>
|
|
746
762
|
|
|
747
763
|
|
|
764
|
+
<a name="tut-pi"></a>
|
|
765
|
+
<h3 class="section2">Processing Instruction (PI) Converter</h3>
|
|
766
|
+
<p>Erubis can parse Processing Instructions (PI) as embedded pattern.
|
|
767
|
+
</p>
|
|
768
|
+
<ul type="disc">
|
|
769
|
+
<li>'<code><?rb <em>...</em> ?></code>' represents Ruby statement.
|
|
770
|
+
</li>
|
|
771
|
+
<li>'<code>@{<em>...</em>}@</code>' represents escaped expression value.
|
|
772
|
+
</li>
|
|
773
|
+
<li>'<code>@!{<em>...</em>}@</code>' represents normal expression value.
|
|
774
|
+
</li>
|
|
775
|
+
<li>'<code>@!!{<em>...</em>}@</code>' prints expression value to standard output.
|
|
776
|
+
</li>
|
|
777
|
+
<li>(experimental) '<code><%= <em>...</em> %></code>' is also available to print expression value.
|
|
778
|
+
</li>
|
|
779
|
+
</ul>
|
|
780
|
+
<p>This is more useful than basic embedded pattern ('<code><% ... ></code>') because PI doesn't break XML or HTML at all.
|
|
781
|
+
For example the following XHTML file is well-formed and HTML validator got no errors on this example.
|
|
782
|
+
</p>
|
|
783
|
+
<a name="example9.xhtml"></a>
|
|
784
|
+
<div class="program_caption">
|
|
785
|
+
example9.xhtml</div>
|
|
786
|
+
<pre class="program"><?xml version="1.0" ?>
|
|
787
|
+
<strong><?rb
|
|
788
|
+
lang = 'en'
|
|
789
|
+
list = ['<aaa>', 'b&b', '"ccc"']
|
|
790
|
+
?></strong>
|
|
791
|
+
<html lang="<strong>@!{lang}@</strong>">
|
|
792
|
+
<body>
|
|
793
|
+
<ul>
|
|
794
|
+
<strong><?rb for item in list ?></strong>
|
|
795
|
+
<li><strong>@{item}@</strong></li>
|
|
796
|
+
<strong><?rb end ?></strong>
|
|
797
|
+
</ul>
|
|
798
|
+
</body>
|
|
799
|
+
</html>
|
|
800
|
+
</pre>
|
|
801
|
+
<p>If the command-line property '--pi=<em>name</em>' is specified, erubis command parses input with PI converter.
|
|
802
|
+
If <em>name</em> is omitted then the following name is used according to '-l <em>lang</em>'.
|
|
803
|
+
</p>
|
|
804
|
+
<div align="center">
|
|
805
|
+
<table class="table1" border="1" cellspacing="0" summary=" mapping of '-l' option and PI name
|
|
806
|
+
">
|
|
807
|
+
<caption class="caption1">
|
|
808
|
+
mapping of '-l' option and PI name
|
|
809
|
+
</caption>
|
|
810
|
+
<tr class="tr1">
|
|
811
|
+
<th class="th1">'-l' option</th>
|
|
812
|
+
<th class="th1">PI name</th>
|
|
813
|
+
</tr>
|
|
814
|
+
<tr class="tr1">
|
|
815
|
+
<td class="td1">-l ruby</td>
|
|
816
|
+
<td class="td1"><?rb ... ?></td>
|
|
817
|
+
</tr>
|
|
818
|
+
<tr class="tr1">
|
|
819
|
+
<td class="td1">-l php</td>
|
|
820
|
+
<td class="td1"><?php ... ?></td>
|
|
821
|
+
</tr>
|
|
822
|
+
<tr class="tr1">
|
|
823
|
+
<td class="td1">-l perl</td>
|
|
824
|
+
<td class="td1"><?perl ... ?></td>
|
|
825
|
+
</tr>
|
|
826
|
+
<tr class="tr1">
|
|
827
|
+
<td class="td1">-l java</td>
|
|
828
|
+
<td class="td1"><?java ... ?></td>
|
|
829
|
+
</tr>
|
|
830
|
+
<tr class="tr1">
|
|
831
|
+
<td class="td1">-l javascript</td>
|
|
832
|
+
<td class="td1"><?js ... ?></td>
|
|
833
|
+
</tr>
|
|
834
|
+
<tr class="tr1">
|
|
835
|
+
<td class="td1">-l scheme</td>
|
|
836
|
+
<td class="td1"><?scheme ... ?></td>
|
|
837
|
+
</tr>
|
|
838
|
+
</table>
|
|
839
|
+
</div>
|
|
840
|
+
<div class="terminal_caption">
|
|
841
|
+
output</div>
|
|
842
|
+
<pre class="terminal">$ erubis <strong>--pi</strong> example9.xhtml
|
|
843
|
+
<?xml version="1.0" ?>
|
|
844
|
+
<html lang="en">
|
|
845
|
+
<body>
|
|
846
|
+
<ul>
|
|
847
|
+
<li>&lt;aaa&gt;</li>
|
|
848
|
+
<li>b&amp;b</li>
|
|
849
|
+
<li>&quot;ccc&quot;</li>
|
|
850
|
+
</ul>
|
|
851
|
+
</body>
|
|
852
|
+
</html>
|
|
853
|
+
</pre>
|
|
854
|
+
<p>Expression character can be changeable by command-line property '--embchar=<em>char</em>. Default is '<code>@</code>'.
|
|
855
|
+
</p>
|
|
856
|
+
<p><strong>(experimental)</strong> Erubis supports '<%= ... %>' pattern with PI pattern.
|
|
857
|
+
</p>
|
|
858
|
+
<div class="program_caption">
|
|
859
|
+
example of Rails view template</div>
|
|
860
|
+
<pre class="program"><table>
|
|
861
|
+
<tr>
|
|
862
|
+
<?rb for item in @list ?>
|
|
863
|
+
<td>@{item.id}@</td>
|
|
864
|
+
<td>@{item.name}@</td>
|
|
865
|
+
<td>
|
|
866
|
+
<strong><%=</strong> link_to 'Destroy', {:action=>'destroy', :id=>item.id},
|
|
867
|
+
:confirm=>'Are you OK?' <strong>%></strong>
|
|
868
|
+
</td>
|
|
869
|
+
<?rb end ?>
|
|
870
|
+
</tr>
|
|
871
|
+
</table>
|
|
872
|
+
</pre>
|
|
873
|
+
<br>
|
|
874
|
+
|
|
875
|
+
|
|
748
876
|
<br>
|
|
749
877
|
|
|
750
878
|
|
|
@@ -811,6 +939,11 @@ The following is an example to use some enhancers in command-line.
|
|
|
811
939
|
Print embedded code only and ignore normal text.
|
|
812
940
|
</dd>
|
|
813
941
|
<dt class="dt1">
|
|
942
|
+
<a href="#nocode-enhancer">NoCodeEnhancer</a> (language-independent)</dt>
|
|
943
|
+
<dd class="dd1">
|
|
944
|
+
Print normal text only and ignore code.
|
|
945
|
+
</dd>
|
|
946
|
+
<dt class="dt1">
|
|
814
947
|
<a href="#simplify-enhancer">SimplifyEnhancer</a> (language-independent)</dt>
|
|
815
948
|
<dd class="dd1">
|
|
816
949
|
Make compile faster but don't trim spaces around '<% %>'.
|
|
@@ -853,8 +986,8 @@ compiled source code</div>
|
|
|
853
986
|
<pre class="terminal">$ erubis -xE Escape example.eruby
|
|
854
987
|
_buf = []; _buf << '<div>
|
|
855
988
|
'; for item in list
|
|
856
|
-
_buf << ' <p>'; <
|
|
857
|
-
<p>'; <
|
|
989
|
+
_buf << ' <p>'; <strong>_buf << Erubis::XmlHelper.escape_xml( item );</strong> _buf << '</p>
|
|
990
|
+
<p>'; <strong>_buf << ( item ).to_s;</strong> _buf << '</p>
|
|
858
991
|
'; end
|
|
859
992
|
_buf << '</div>
|
|
860
993
|
';
|
|
@@ -873,7 +1006,7 @@ Therefore, you can use 'print' statement in embedded ruby code.
|
|
|
873
1006
|
<div class="terminal_caption">
|
|
874
1007
|
compiled source code</div>
|
|
875
1008
|
<pre class="terminal">$ erubis -xE Stdout example.eruby
|
|
876
|
-
<
|
|
1009
|
+
<strong>_buf = $stdout;</strong> _buf << '<div>
|
|
877
1010
|
'; for item in list
|
|
878
1011
|
_buf << ' <p>'; _buf << ( item ).to_s; _buf << '</p>
|
|
879
1012
|
<p>'; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << '</p>
|
|
@@ -894,12 +1027,12 @@ compiled source code</div>
|
|
|
894
1027
|
<div class="terminal_caption">
|
|
895
1028
|
compiled source code</div>
|
|
896
1029
|
<pre class="terminal">$ erubis -xE PrintOut example.eruby
|
|
897
|
-
<
|
|
1030
|
+
<strong>print</strong> '<div>
|
|
898
1031
|
'; for item in list
|
|
899
|
-
<
|
|
900
|
-
<p>'; <
|
|
1032
|
+
<strong>print</strong> ' <p>'; <strong>print</strong>(( item ).to_s); <strong>print</strong> '</p>
|
|
1033
|
+
<p>'; <strong>print</strong> Erubis::XmlHelper.escape_xml( item ); <strong>print</strong> '</p>
|
|
901
1034
|
'; end
|
|
902
|
-
<
|
|
1035
|
+
<strong>print</strong> '</div>
|
|
903
1036
|
';
|
|
904
1037
|
</pre>
|
|
905
1038
|
<p>PrintOutEnhancer is only for Eruby.
|
|
@@ -915,7 +1048,7 @@ compiled source code</div>
|
|
|
915
1048
|
<div class="program_caption">
|
|
916
1049
|
printenabled-example.eruby</div>
|
|
917
1050
|
<pre class="program"><% for item in @list %>
|
|
918
|
-
<b><
|
|
1051
|
+
<b><strong><% print item %></strong></b>
|
|
919
1052
|
<% end %>
|
|
920
1053
|
</pre>
|
|
921
1054
|
<a name="printenabled-example.rb"></a>
|
|
@@ -951,14 +1084,14 @@ because print() method in '<% ... %>' invokes not Kernel#print() but Print
|
|
|
951
1084
|
</p>
|
|
952
1085
|
<div class="terminal_caption">
|
|
953
1086
|
compiled source code</div>
|
|
954
|
-
<pre class="terminal"><
|
|
1087
|
+
<pre class="terminal"><strong>_buf = [];</strong> _buf << '<div>
|
|
955
1088
|
'; for item in list
|
|
956
1089
|
_buf << ' <p>'; _buf << ( item ).to_s; _buf << '</p>
|
|
957
1090
|
<p>'; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << '</p>
|
|
958
1091
|
'; end
|
|
959
1092
|
_buf << '</div>
|
|
960
1093
|
';
|
|
961
|
-
<
|
|
1094
|
+
<strong>_buf</strong>
|
|
962
1095
|
</pre>
|
|
963
1096
|
<p>ArrayEnhancer is only for Eruby.
|
|
964
1097
|
</p>
|
|
@@ -981,14 +1114,14 @@ Erubis::Eruby includes this enhancer by default.
|
|
|
981
1114
|
<p>StringBufferEnhancer makes Eruby to use string buffer.
|
|
982
1115
|
</p>
|
|
983
1116
|
<pre class="terminal">$ erubis -xE StringBuffer example.eruby
|
|
984
|
-
<
|
|
1117
|
+
<strong>_buf = '';</strong> _buf << '<div>
|
|
985
1118
|
'; for item in list
|
|
986
1119
|
_buf << ' <p>'; _buf << ( item ).to_s; _buf << '</p>
|
|
987
1120
|
<p>'; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << '</p>
|
|
988
1121
|
'; end
|
|
989
1122
|
_buf << '</div>
|
|
990
1123
|
';
|
|
991
|
-
<
|
|
1124
|
+
<strong>_buf</strong>
|
|
992
1125
|
</pre>
|
|
993
1126
|
<p>StringBufferEnhancer is only for Eruby.
|
|
994
1127
|
</p>
|
|
@@ -1042,6 +1175,52 @@ _buf.join
|
|
|
1042
1175
|
<br>
|
|
1043
1176
|
|
|
1044
1177
|
|
|
1178
|
+
<a name="nocode-enhancer"></a>
|
|
1179
|
+
<h3 class="section2">NoCodeEnhancer</h3>
|
|
1180
|
+
<p>NoCodeEnhancer suppress output of embedded code and prints only normal text.
|
|
1181
|
+
This is useful especially when validating HTML tags.
|
|
1182
|
+
</p>
|
|
1183
|
+
<a name="nocode-example.eruby"></a>
|
|
1184
|
+
<div class="program_caption">
|
|
1185
|
+
nocode-example.eruby</div>
|
|
1186
|
+
<pre class="program"><h3>List</h3>
|
|
1187
|
+
<% if !@list || @list.empty? %>
|
|
1188
|
+
<p>not found.</p>
|
|
1189
|
+
<% else %>
|
|
1190
|
+
<table>
|
|
1191
|
+
<tbody>
|
|
1192
|
+
<% @list.each_with_index do |item, i| %>
|
|
1193
|
+
<tr bgcolor="<%= i%2 == 0 ? '#FFCCCC' : '#CCCCFF' %>">
|
|
1194
|
+
<td><%= item %></td>
|
|
1195
|
+
</tr>
|
|
1196
|
+
<% end %>
|
|
1197
|
+
</tbody>
|
|
1198
|
+
</table>
|
|
1199
|
+
<% end %>
|
|
1200
|
+
</pre>
|
|
1201
|
+
<div class="terminal_caption">
|
|
1202
|
+
output example of NoTextEnhancer</div>
|
|
1203
|
+
<pre class="terminal">$ erubis -TxE NoCode notext-example.eruby
|
|
1204
|
+
<h3>List</h3>
|
|
1205
|
+
|
|
1206
|
+
<p>not found.</p>
|
|
1207
|
+
|
|
1208
|
+
<table>
|
|
1209
|
+
<tbody>
|
|
1210
|
+
|
|
1211
|
+
<tr bgcolor="">
|
|
1212
|
+
<td></td>
|
|
1213
|
+
</tr>
|
|
1214
|
+
|
|
1215
|
+
</tbody>
|
|
1216
|
+
</table>
|
|
1217
|
+
|
|
1218
|
+
</pre>
|
|
1219
|
+
<p>NoCodeEnhancer is language-independent. It is useful even if you are PHP user, see <a href="#topics-php">this section</a>.
|
|
1220
|
+
</p>
|
|
1221
|
+
<br>
|
|
1222
|
+
|
|
1223
|
+
|
|
1045
1224
|
<a name="simplify-enhancer"></a>
|
|
1046
1225
|
<h3 class="section2">SimplifyEnhancer</h3>
|
|
1047
1226
|
<p>SimplifyEnhancer makes compiling a little faster but don't trim spaces around '<% %>'.
|
|
@@ -1073,16 +1252,16 @@ You can specify pattern by :bipattern property.
|
|
|
1073
1252
|
<div class="program_caption">
|
|
1074
1253
|
bipattern-example.rhtml</div>
|
|
1075
1254
|
<pre class="program"><% for item in list %>
|
|
1076
|
-
<b><
|
|
1077
|
-
<b><
|
|
1255
|
+
<b><strong>[= item =]</strong></b>
|
|
1256
|
+
<b><strong>[== item =]</strong></b>
|
|
1078
1257
|
<% end %>
|
|
1079
1258
|
</pre>
|
|
1080
1259
|
<div class="terminal_caption">
|
|
1081
1260
|
compiled source code</div>
|
|
1082
1261
|
<pre class="terminal">$ erubis -xE BiPattern bipattern-example.rhtml
|
|
1083
1262
|
_buf = []; for item in list
|
|
1084
|
-
_buf << ' <b>'; <
|
|
1085
|
-
<b>'; <
|
|
1263
|
+
_buf << ' <b>'; <strong>_buf << ( item ).to_s;</strong> _buf << '</b>
|
|
1264
|
+
<b>'; <strong>_buf << Erubis::XmlHelper.escape_xml( item );</strong> _buf << '</b>
|
|
1086
1265
|
'; end
|
|
1087
1266
|
_buf.join
|
|
1088
1267
|
</pre>
|
|
@@ -1099,17 +1278,17 @@ This is for compatibility with eruby and ERB.
|
|
|
1099
1278
|
<a name="percentline-example.rhtml"></a>
|
|
1100
1279
|
<div class="program_caption">
|
|
1101
1280
|
percentline-example.rhtml</div>
|
|
1102
|
-
<pre class="program"><
|
|
1281
|
+
<pre class="program"><strong>% for item in list</strong>
|
|
1103
1282
|
<b><%= item %></b>
|
|
1104
|
-
<
|
|
1283
|
+
<strong>% end</strong>
|
|
1105
1284
|
%% lines with '%%'
|
|
1106
1285
|
</pre>
|
|
1107
1286
|
<div class="terminal_caption">
|
|
1108
1287
|
compiled source code</div>
|
|
1109
1288
|
<pre class="terminal">$ erubis -xE PercentLine percentline-example.rhtml
|
|
1110
|
-
_buf = []; <
|
|
1289
|
+
_buf = []; <strong>for item in list</strong>
|
|
1111
1290
|
_buf << ' <b>'; _buf << ( item ).to_s; _buf << '</b>
|
|
1112
|
-
'; <
|
|
1291
|
+
'; <strong>end</strong>
|
|
1113
1292
|
_buf << '% lines with \'%%\'
|
|
1114
1293
|
';
|
|
1115
1294
|
_buf.join
|
|
@@ -1128,28 +1307,28 @@ _buf.join
|
|
|
1128
1307
|
<a name="headerfooter-example.eruby"></a>
|
|
1129
1308
|
<div class="program_caption">
|
|
1130
1309
|
headerfooter-example.eruby</div>
|
|
1131
|
-
<pre class="program"><
|
|
1132
|
-
<
|
|
1133
|
-
<
|
|
1310
|
+
<pre class="program"><strong><!--#header:</strong>
|
|
1311
|
+
<strong>def list_items(items)</strong>
|
|
1312
|
+
<strong>#--></strong>
|
|
1134
1313
|
<% for item in items %>
|
|
1135
1314
|
<b><%= item %></b>
|
|
1136
1315
|
<% end %>
|
|
1137
|
-
<
|
|
1138
|
-
<
|
|
1139
|
-
<
|
|
1316
|
+
<strong><!--#footer:</strong>
|
|
1317
|
+
<strong>end</strong>
|
|
1318
|
+
<strong>#--></strong>
|
|
1140
1319
|
</pre>
|
|
1141
1320
|
<div class="terminal_caption">
|
|
1142
1321
|
compiled source code</div>
|
|
1143
1322
|
<pre class="terminal">$ erubis -xE HeaderFooter headerfooter-example.eruby
|
|
1144
1323
|
|
|
1145
|
-
<
|
|
1324
|
+
<strong>def list_items(items)</strong>
|
|
1146
1325
|
|
|
1147
1326
|
_buf = []; for item in items
|
|
1148
1327
|
_buf << ' <b>'; _buf << ( item ).to_s; _buf << '</b>
|
|
1149
1328
|
'; end
|
|
1150
1329
|
_buf.join
|
|
1151
1330
|
|
|
1152
|
-
<
|
|
1331
|
+
<strong>end</strong>
|
|
1153
1332
|
|
|
1154
1333
|
</pre>
|
|
1155
1334
|
<p>Compare to the following:
|
|
@@ -1157,27 +1336,27 @@ _buf.join
|
|
|
1157
1336
|
<a name="normal-eruby-test.eruby"></a>
|
|
1158
1337
|
<div class="program_caption">
|
|
1159
1338
|
normal-eruby-test.eruby</div>
|
|
1160
|
-
<pre class="program"><
|
|
1161
|
-
<
|
|
1162
|
-
<
|
|
1339
|
+
<pre class="program"><strong><%</strong>
|
|
1340
|
+
<strong>def list_items(items)</strong>
|
|
1341
|
+
<strong>%></strong>
|
|
1163
1342
|
<% for item in items %>
|
|
1164
1343
|
<li><%= item %></li>
|
|
1165
1344
|
<% end %>
|
|
1166
|
-
<
|
|
1167
|
-
<
|
|
1168
|
-
<
|
|
1345
|
+
<strong><%</strong>
|
|
1346
|
+
<strong>end</strong>
|
|
1347
|
+
<strong>%></strong>
|
|
1169
1348
|
</pre>
|
|
1170
1349
|
<div class="terminal_caption">
|
|
1171
1350
|
compiled source code</div>
|
|
1172
1351
|
<pre class="terminal">$ erubis -x normal-eruby-test.eruby
|
|
1173
1352
|
_buf = [];
|
|
1174
|
-
<
|
|
1353
|
+
<strong>def list_items(items)</strong>
|
|
1175
1354
|
|
|
1176
1355
|
for item in items
|
|
1177
1356
|
_buf << '<li>'; _buf << ( item ).to_s; _buf << '</li>
|
|
1178
1357
|
'; end
|
|
1179
1358
|
|
|
1180
|
-
<
|
|
1359
|
+
<strong>end</strong>
|
|
1181
1360
|
|
|
1182
1361
|
_buf.join
|
|
1183
1362
|
</pre>
|
|
@@ -1189,20 +1368,20 @@ that is, header is no need to be in the head of eRuby script.
|
|
|
1189
1368
|
headerfooter-example2.rhtml</div>
|
|
1190
1369
|
<pre class="program"><?xml version="1.0"?>
|
|
1191
1370
|
<html>
|
|
1192
|
-
<
|
|
1193
|
-
<
|
|
1194
|
-
<
|
|
1371
|
+
<strong><!--#header:</strong>
|
|
1372
|
+
<strong>def page(list)</strong>
|
|
1373
|
+
<strong>#--></strong>
|
|
1195
1374
|
:
|
|
1196
|
-
<
|
|
1197
|
-
<
|
|
1198
|
-
<
|
|
1375
|
+
<strong><!--#footer:</strong>
|
|
1376
|
+
<strong>end</strong>
|
|
1377
|
+
<strong>#--></strong>
|
|
1199
1378
|
</html>
|
|
1200
1379
|
</pre>
|
|
1201
1380
|
<div class="terminal_caption">
|
|
1202
1381
|
compiled source code</div>
|
|
1203
1382
|
<pre class="terminal">$ erubis -xE HeaderFooter headerfooter-example2.rhtml
|
|
1204
1383
|
|
|
1205
|
-
<
|
|
1384
|
+
<strong>def page(list)</strong>
|
|
1206
1385
|
|
|
1207
1386
|
_buf = []; _buf << '<?xml version="1.0"?>
|
|
1208
1387
|
<html>
|
|
@@ -1211,7 +1390,7 @@ _buf = []; _buf << '<?xml version="1.0"?>
|
|
|
1211
1390
|
';
|
|
1212
1391
|
_buf.join
|
|
1213
1392
|
|
|
1214
|
-
<
|
|
1393
|
+
<strong>end</strong>
|
|
1215
1394
|
|
|
1216
1395
|
</pre>
|
|
1217
1396
|
<p>HeaderFooterEnhancer is experimental and is language-independent.
|
|
@@ -1250,15 +1429,15 @@ example.ephp</div>
|
|
|
1250
1429
|
<pre class="program"><?xml version="1.0"?>
|
|
1251
1430
|
<html>
|
|
1252
1431
|
<body>
|
|
1253
|
-
<p>Hello <
|
|
1432
|
+
<p>Hello <strong><%= $user %></strong>!</p>
|
|
1254
1433
|
<table>
|
|
1255
1434
|
<tbody>
|
|
1256
|
-
<
|
|
1257
|
-
<
|
|
1258
|
-
<
|
|
1259
|
-
<tr bgcolor=<
|
|
1260
|
-
<td><
|
|
1261
|
-
<td><
|
|
1435
|
+
<strong><% $i = 0; %></strong>
|
|
1436
|
+
<strong><% foreach ($list as $item) { %></strong>
|
|
1437
|
+
<strong><% $i++; %></strong>
|
|
1438
|
+
<tr bgcolor=<strong>"<%= $i % 2 == 0 ? '#FFCCCC' : '#CCCCFF' %>"</strong>>
|
|
1439
|
+
<td><strong><%= $i %></strong></td>
|
|
1440
|
+
<td><strong><%== $item %></strong></td>
|
|
1262
1441
|
</tr>
|
|
1263
1442
|
<% } %>
|
|
1264
1443
|
</tbody>
|
|
@@ -1296,33 +1475,33 @@ compiled source code</div>
|
|
|
1296
1475
|
<a name="example.ec"></a>
|
|
1297
1476
|
<div class="program_caption">
|
|
1298
1477
|
example.ec</div>
|
|
1299
|
-
<pre class="program"><
|
|
1478
|
+
<pre class="program"><strong><%
|
|
1300
1479
|
#include <stdio.h>
|
|
1301
1480
|
|
|
1302
1481
|
int main(int argc, char *argv[])
|
|
1303
1482
|
{
|
|
1304
1483
|
int i;
|
|
1305
1484
|
|
|
1306
|
-
%></
|
|
1485
|
+
%></strong>
|
|
1307
1486
|
<html>
|
|
1308
1487
|
<body>
|
|
1309
|
-
<p>Hello <
|
|
1488
|
+
<p>Hello <strong><%= "%s", argv[0] %></strong>!</p>
|
|
1310
1489
|
<table>
|
|
1311
1490
|
<tbody>
|
|
1312
|
-
<
|
|
1313
|
-
<tr bgcolor="<
|
|
1314
|
-
<td><
|
|
1315
|
-
<td><
|
|
1491
|
+
<strong><% for (i = 1; i < argc; i++) { %></strong>
|
|
1492
|
+
<tr bgcolor="<strong><%= i % 2 == 0 ? "#FFCCCC" : "#CCCCFF" %></strong>">
|
|
1493
|
+
<td><strong><%= "%d", i %></strong></td>
|
|
1494
|
+
<td><strong><%= "%s", argv[i] %></strong></td>
|
|
1316
1495
|
</tr>
|
|
1317
|
-
<
|
|
1496
|
+
<strong><% } %></strong>
|
|
1318
1497
|
</tbody>
|
|
1319
1498
|
</table>
|
|
1320
1499
|
</body>
|
|
1321
1500
|
</html>
|
|
1322
|
-
<
|
|
1501
|
+
<strong><%
|
|
1323
1502
|
return 0;
|
|
1324
1503
|
}
|
|
1325
|
-
%></
|
|
1504
|
+
%></strong>
|
|
1326
1505
|
</pre>
|
|
1327
1506
|
<div class="terminal_caption">
|
|
1328
1507
|
compiled source code</div>
|
|
@@ -1364,7 +1543,7 @@ fputs(" </tbody>\n"
|
|
|
1364
1543
|
<a name="Example.ejava"></a>
|
|
1365
1544
|
<div class="program_caption">
|
|
1366
1545
|
Example.ejava</div>
|
|
1367
|
-
<pre class="program"><
|
|
1546
|
+
<pre class="program"><strong><%
|
|
1368
1547
|
import java.util.*;
|
|
1369
1548
|
|
|
1370
1549
|
public class Example {
|
|
@@ -1377,23 +1556,23 @@ public class Example {
|
|
|
1377
1556
|
|
|
1378
1557
|
public String view() {
|
|
1379
1558
|
StringBuffer _buf = new StringBuffer();
|
|
1380
|
-
%></
|
|
1559
|
+
%></strong>
|
|
1381
1560
|
<html>
|
|
1382
1561
|
<body>
|
|
1383
|
-
<p>Hello <
|
|
1562
|
+
<p>Hello <strong><%= user %></strong>!</p>
|
|
1384
1563
|
<table>
|
|
1385
1564
|
<tbody>
|
|
1386
|
-
<
|
|
1387
|
-
<tr bgcolor=<
|
|
1388
|
-
<td><
|
|
1389
|
-
<td><
|
|
1565
|
+
<strong><% for (int i = 0; i < list.length; i++) { %></strong>
|
|
1566
|
+
<tr bgcolor=<strong>"<%= i % 2 == 0 ? "#FFCCCC" : "#CCCCFF" %>"</strong>>
|
|
1567
|
+
<td><strong><%= i + 1 %></strong></td>
|
|
1568
|
+
<td><strong><%== list[i] %></strong></td>
|
|
1390
1569
|
</tr>
|
|
1391
|
-
<
|
|
1570
|
+
<strong><% } %></strong>
|
|
1392
1571
|
</tbody>
|
|
1393
1572
|
</table>
|
|
1394
1573
|
<body>
|
|
1395
1574
|
</html>
|
|
1396
|
-
<
|
|
1575
|
+
<strong><%
|
|
1397
1576
|
return _buf.toString();
|
|
1398
1577
|
}
|
|
1399
1578
|
|
|
@@ -1418,7 +1597,7 @@ public class Example {
|
|
|
1418
1597
|
return sb.toString();
|
|
1419
1598
|
}
|
|
1420
1599
|
}
|
|
1421
|
-
%></
|
|
1600
|
+
%></strong>
|
|
1422
1601
|
</pre>
|
|
1423
1602
|
<div class="terminal_caption">
|
|
1424
1603
|
compiled source code</div>
|
|
@@ -1490,30 +1669,30 @@ return _buf.toString();
|
|
|
1490
1669
|
example.escheme</div>
|
|
1491
1670
|
<pre class="program"><html>
|
|
1492
1671
|
<body>
|
|
1493
|
-
<
|
|
1672
|
+
<strong><%
|
|
1494
1673
|
(let ((user "Erubis")
|
|
1495
1674
|
(items '("<aaa>" "b&b" "\"ccc\""))
|
|
1496
1675
|
(i 0))
|
|
1497
|
-
%></
|
|
1498
|
-
<p>Hello <
|
|
1676
|
+
%></strong>
|
|
1677
|
+
<p>Hello <strong><%= user %></strong>!</p>
|
|
1499
1678
|
<table>
|
|
1500
|
-
<
|
|
1679
|
+
<strong><%
|
|
1501
1680
|
(for-each
|
|
1502
1681
|
(lambda (item)
|
|
1503
1682
|
(set! i (+ i 1))
|
|
1504
|
-
%></
|
|
1505
|
-
<tr bgcolor="<
|
|
1506
|
-
<td><
|
|
1507
|
-
<td><
|
|
1683
|
+
%></strong>
|
|
1684
|
+
<tr bgcolor="<strong><%= (if (= (modulo i 2) 0) "#FFCCCC" "#CCCCFF") %></strong>">
|
|
1685
|
+
<td><strong><%= i %></strong></td>
|
|
1686
|
+
<td><strong><%= item %></strong></td>
|
|
1508
1687
|
</tr>
|
|
1509
|
-
<
|
|
1688
|
+
<strong><%
|
|
1510
1689
|
) ; lambda end
|
|
1511
1690
|
items) ; for-each end
|
|
1512
|
-
%></
|
|
1691
|
+
%></strong>
|
|
1513
1692
|
</table>
|
|
1514
|
-
<
|
|
1693
|
+
<strong><%
|
|
1515
1694
|
) ; let end
|
|
1516
|
-
%></
|
|
1695
|
+
%></strong>
|
|
1517
1696
|
</body>
|
|
1518
1697
|
</html>
|
|
1519
1698
|
</pre>
|
|
@@ -1590,21 +1769,21 @@ compiled source code (with --func=display property)</div>
|
|
|
1590
1769
|
<a name="example.eperl"></a>
|
|
1591
1770
|
<div class="program_caption">
|
|
1592
1771
|
example.eprl</div>
|
|
1593
|
-
<pre class="program"><
|
|
1772
|
+
<pre class="program"><strong><%
|
|
1594
1773
|
my $user = 'Erubis';
|
|
1595
1774
|
my @list = ('<aaa>', 'b&b', '"ccc"');
|
|
1596
|
-
%></
|
|
1775
|
+
%></strong>
|
|
1597
1776
|
<html>
|
|
1598
1777
|
<body>
|
|
1599
|
-
<p>Hello <
|
|
1778
|
+
<p>Hello <strong><%= $user %></strong>!</p>
|
|
1600
1779
|
<table>
|
|
1601
|
-
<
|
|
1602
|
-
<
|
|
1603
|
-
<tr bgcolor=<
|
|
1604
|
-
<td><
|
|
1605
|
-
<td><
|
|
1780
|
+
<strong><% $i = 0; %></strong>
|
|
1781
|
+
<strong><% for $item (@list) { %></strong>
|
|
1782
|
+
<tr bgcolor=<strong><%= ++$i % 2 == 0 ? '#FFCCCC' : '#CCCCFF' %></strong>">
|
|
1783
|
+
<td><strong><%= $i %></strong></td>
|
|
1784
|
+
<td><strong><%= $item %></strong></td>
|
|
1606
1785
|
</tr>
|
|
1607
|
-
<
|
|
1786
|
+
<strong><% } %></strong>
|
|
1608
1787
|
</table>
|
|
1609
1788
|
</body>
|
|
1610
1789
|
</html>
|
|
@@ -1612,7 +1791,7 @@ example.eprl</div>
|
|
|
1612
1791
|
<div class="terminal_caption">
|
|
1613
1792
|
compiled source code</div>
|
|
1614
1793
|
<pre class="terminal">$ erubis -l perl example.eperl
|
|
1615
|
-
|
|
1794
|
+
use HTML::Entities;
|
|
1616
1795
|
my $user = 'Erubis';
|
|
1617
1796
|
my @list = ('<aaa>', 'b&b', '"ccc"');
|
|
1618
1797
|
|
|
@@ -1637,25 +1816,25 @@ print(' </table>
|
|
|
1637
1816
|
|
|
1638
1817
|
<a name="lang-javascript"></a>
|
|
1639
1818
|
<h3 class="section2">JavaScript</h3>
|
|
1640
|
-
<p>example.ejs
|
|
1641
|
-
</p>
|
|
1642
1819
|
<a name="example.ejs"></a>
|
|
1643
|
-
<
|
|
1820
|
+
<div class="program_caption">
|
|
1821
|
+
example.ejs</div>
|
|
1822
|
+
<pre class="program"><strong><%
|
|
1644
1823
|
var user = 'Erubis';
|
|
1645
1824
|
var list = ['<aaa>', 'b&b', '"ccc"'];
|
|
1646
|
-
%></
|
|
1825
|
+
%></strong>
|
|
1647
1826
|
<html>
|
|
1648
1827
|
<body>
|
|
1649
|
-
<p>Hello <
|
|
1828
|
+
<p>Hello <strong><%= user %></strong>!</p>
|
|
1650
1829
|
<table>
|
|
1651
1830
|
<tbody>
|
|
1652
|
-
<
|
|
1653
|
-
<
|
|
1654
|
-
<tr bgcolor="<
|
|
1655
|
-
<td><
|
|
1656
|
-
<td><
|
|
1831
|
+
<strong><% var i; %></strong>
|
|
1832
|
+
<strong><% for (i = 0; i < list.length; i++) { %></strong>
|
|
1833
|
+
<tr bgcolor="<strong><%= i % 2 == 0 ? '#FFCCCC' : '#CCCCFF' %></strong>">
|
|
1834
|
+
<td><strong><%= i + 1 %></strong></td>
|
|
1835
|
+
<td><strong><%= list[i] %></strong></td>
|
|
1657
1836
|
</tr>
|
|
1658
|
-
<
|
|
1837
|
+
<strong><% } %></strong>
|
|
1659
1838
|
</tbody>
|
|
1660
1839
|
</table>
|
|
1661
1840
|
</body>
|
|
@@ -1703,10 +1882,43 @@ try TinyEruby class.
|
|
|
1703
1882
|
<br>
|
|
1704
1883
|
|
|
1705
1884
|
|
|
1885
|
+
<a name="topics-rails"></a>
|
|
1886
|
+
<h3 class="section2">Ruby on Rails Support</h3>
|
|
1887
|
+
<p>Erubis supports Ruby on Rails.
|
|
1888
|
+
Add the following code to your 'app/controllers/application.rb'.
|
|
1889
|
+
</p>
|
|
1890
|
+
<div class="program_caption">
|
|
1891
|
+
app/controllers/application.rb</div>
|
|
1892
|
+
<pre class="program">require 'erubis/helper/rails'
|
|
1893
|
+
suffix = 'erubis'
|
|
1894
|
+
ActionView::Base.register_template_handler(suffix, Erubis::Helper::RailsTemplate)
|
|
1895
|
+
#Erubis::Helper::RailsTemplate.engine_class = Erubis::EscapedEruby ## or Erubis::PI::Eruby
|
|
1896
|
+
#Erubis::Helper::RailsTemplate.default_properties = { :escape=>true, :escapefunc=>'h' }
|
|
1897
|
+
</pre>
|
|
1898
|
+
<p>If you specify Erubis::Helper::CachedRailsTemplate class instead of Erubis::Helper::RailsTemplate class, template string is cached in memory (but template is not reloaded when it is modified).
|
|
1899
|
+
</p>
|
|
1900
|
+
<p>You may got the followign error:
|
|
1901
|
+
</p>
|
|
1902
|
+
<pre class="program">(eval):10:in `render': no block given
|
|
1903
|
+
</pre>
|
|
1904
|
+
<p>It is because layout file scaffold created contains 'yield' method.
|
|
1905
|
+
Use '@content_for_layout' instead 'yield'.
|
|
1906
|
+
</p>
|
|
1907
|
+
<div class="program_caption">
|
|
1908
|
+
app/views/layouts/xxx.erubis</div>
|
|
1909
|
+
<pre class="program"> ...
|
|
1910
|
+
<%<strong>#</strong>= yield %>
|
|
1911
|
+
<strong><%= @content_for_layout %></strong>
|
|
1912
|
+
...
|
|
1913
|
+
</pre>
|
|
1914
|
+
<br>
|
|
1915
|
+
|
|
1916
|
+
|
|
1706
1917
|
<a name="topics-php"></a>
|
|
1707
|
-
<h3 class="section2">NoTextEnhancer in PHP</h3>
|
|
1708
|
-
<p>NoTextEnhancer
|
|
1709
|
-
|
|
1918
|
+
<h3 class="section2">NoTextEnhancer and NoCodeEnhancer in PHP</h3>
|
|
1919
|
+
<p>NoTextEnhancer and NoCodEnahncer are quite useful not only for eRuby but also for PHP.
|
|
1920
|
+
The former "drops" HTML text and show up embedded Ruby/PHP code
|
|
1921
|
+
and the latter drops embedded Ruby/PHP code and leave HTML text.
|
|
1710
1922
|
</p>
|
|
1711
1923
|
<p>For example, see the following PHP script.
|
|
1712
1924
|
</p>
|
|
@@ -1738,22 +1950,150 @@ NoTextEnhancer can separate PHP code from HTML document.
|
|
|
1738
1950
|
</p>
|
|
1739
1951
|
<div class="terminal_caption">
|
|
1740
1952
|
example of using NoTextEnhancer with PHP file</div>
|
|
1741
|
-
<pre class="terminal">$ erubis -T -l php -E NoText -p '<\?php \?>' notext-example.php |
|
|
1953
|
+
<pre class="terminal">$ erubis -T -l php -E NoText -p '<\?php \?>' notext-example.php | cat -n
|
|
1954
|
+
1
|
|
1955
|
+
2
|
|
1956
|
+
3
|
|
1957
|
+
4 <?php if (!$list || count($list) == 0) { ?>
|
|
1958
|
+
5
|
|
1959
|
+
6 <?php } else { ?>
|
|
1960
|
+
7
|
|
1961
|
+
8
|
|
1962
|
+
9 <?php $i = 0; ?>
|
|
1963
|
+
10 <?php foreach ($list as $item) { ?>
|
|
1964
|
+
11 <?php echo ++$i % 2 == 1 ? '#FFCCCC' : '#CCCCFF'; ?>
|
|
1965
|
+
12 <?php echo $item; ?>
|
|
1966
|
+
13
|
|
1967
|
+
14 <?php } ?>
|
|
1968
|
+
15
|
|
1969
|
+
16
|
|
1970
|
+
17 <?php } ?>
|
|
1971
|
+
18
|
|
1972
|
+
19
|
|
1973
|
+
</pre>
|
|
1974
|
+
<p>In the same way, NoCodeEnhancer can extract HTML tags.
|
|
1975
|
+
</p>
|
|
1976
|
+
<div class="terminal_caption">
|
|
1977
|
+
example of using NoCodeEnhancer with PHP file</div>
|
|
1978
|
+
<pre class="terminal">$ erubis -T -l php -E NoCode -p '<\?php \?>' notext-example.php | cat -n
|
|
1979
|
+
1 <html>
|
|
1980
|
+
2 <body>
|
|
1981
|
+
3 <h3>List</h3>
|
|
1982
|
+
4
|
|
1983
|
+
5 <p>not found.</p>
|
|
1984
|
+
6
|
|
1985
|
+
7 <table>
|
|
1986
|
+
8 <tbody>
|
|
1987
|
+
9
|
|
1988
|
+
10
|
|
1989
|
+
11 <tr bgcolor="">
|
|
1990
|
+
12 <td></td>
|
|
1991
|
+
13 </tr>
|
|
1992
|
+
14
|
|
1993
|
+
15 </tbody>
|
|
1994
|
+
16 </table>
|
|
1995
|
+
17
|
|
1996
|
+
18 </body>
|
|
1997
|
+
19 </html>
|
|
1998
|
+
</pre>
|
|
1999
|
+
<br>
|
|
1742
2000
|
|
|
1743
|
-
<?php if (!$list || count($list) == 0) { ?>
|
|
1744
2001
|
|
|
1745
|
-
|
|
2002
|
+
<a name="topics-notext"></a>
|
|
2003
|
+
<h3 class="section2">Command <code>notext</code></h3>
|
|
2004
|
+
<p>Command 'notext' removes text part from eRuby script and leaves only embedded Ruby code.
|
|
2005
|
+
This is very useful when debugging eRuby script.
|
|
2006
|
+
</p>
|
|
2007
|
+
<div class="program_caption">
|
|
2008
|
+
notext-ex.rhtml</div>
|
|
2009
|
+
<pre class="program"><html>
|
|
2010
|
+
<head>
|
|
2011
|
+
<title>notext example</title>
|
|
2012
|
+
</head>
|
|
2013
|
+
<body>
|
|
2014
|
+
<table>
|
|
2015
|
+
<% @list.each_with_index do |item, i| %>
|
|
2016
|
+
<% klass = i % 2 == 0 ? 'odd' : 'even' %>
|
|
2017
|
+
<tr class="<%= klass %>">
|
|
2018
|
+
<td><%== item %></td>
|
|
2019
|
+
</tr>
|
|
2020
|
+
<% end %>
|
|
2021
|
+
</table>
|
|
2022
|
+
</body>
|
|
2023
|
+
</html>
|
|
2024
|
+
</pre>
|
|
2025
|
+
<div class="terminal_caption">
|
|
2026
|
+
command line example</div>
|
|
2027
|
+
<pre class="terminal">$ notext notext-ex.rhtml
|
|
2028
|
+
_buf = [];
|
|
1746
2029
|
|
|
1747
|
-
<?php $i = 0; ?>
|
|
1748
|
-
<?php foreach ($list as $item) { ?>
|
|
1749
|
-
<?php echo ++$i % 2 == 1 ? '#FFCCCC' : '#CCCCFF'; ?>
|
|
1750
|
-
<?php echo $item; ?>
|
|
1751
2030
|
|
|
1752
|
-
<?php } ?>
|
|
1753
2031
|
|
|
1754
|
-
<?php } ?>
|
|
1755
2032
|
|
|
2033
|
+
|
|
2034
|
+
@list.each_with_index do |item, i| ;
|
|
2035
|
+
klass = i % 2 == 0 ? 'odd' : 'even' ;
|
|
2036
|
+
_buf << ( klass ).to_s;
|
|
2037
|
+
_buf << Erubis::XmlHelper.escape_xml( item );
|
|
2038
|
+
|
|
2039
|
+
end ;
|
|
2040
|
+
|
|
2041
|
+
|
|
2042
|
+
_buf.join
|
|
1756
2043
|
</pre>
|
|
2044
|
+
<p>Command-line option '-u' deletes continuous empty lines to one empty line.
|
|
2045
|
+
</p>
|
|
2046
|
+
<div class="terminal_caption">
|
|
2047
|
+
command-line example</div>
|
|
2048
|
+
<pre class="terminal">$ notext <strong>-u</strong> notext-ex.rhtml
|
|
2049
|
+
_buf = [];
|
|
2050
|
+
|
|
2051
|
+
@list.each_with_index do |item, i| ;
|
|
2052
|
+
klass = i % 2 == 0 ? 'odd' : 'even' ;
|
|
2053
|
+
_buf << ( klass ).to_s;
|
|
2054
|
+
_buf << Erubis::XmlHelper.escape_xml( item );
|
|
2055
|
+
|
|
2056
|
+
end ;
|
|
2057
|
+
|
|
2058
|
+
_buf.join
|
|
2059
|
+
</pre>
|
|
2060
|
+
<p>Command-line option '-c' deletes all empty lines.
|
|
2061
|
+
</p>
|
|
2062
|
+
<div class="terminal_caption">
|
|
2063
|
+
command-line example</div>
|
|
2064
|
+
<pre class="terminal">$ notext <strong>-c</strong> notext-ex.rhtml
|
|
2065
|
+
_buf = [];
|
|
2066
|
+
@list.each_with_index do |item, i| ;
|
|
2067
|
+
klass = i % 2 == 0 ? 'odd' : 'even' ;
|
|
2068
|
+
_buf << ( klass ).to_s;
|
|
2069
|
+
_buf << Erubis::XmlHelper.escape_xml( item );
|
|
2070
|
+
end ;
|
|
2071
|
+
_buf.join
|
|
2072
|
+
</pre>
|
|
2073
|
+
<p>Command-line option '-n' shows line numbers. It can work with '-c' or '-u'.
|
|
2074
|
+
</p>
|
|
2075
|
+
<pre class="terminal">$ notext <strong>-nu</strong> example2.rhtml
|
|
2076
|
+
1: _buf = [];
|
|
2077
|
+
|
|
2078
|
+
7: @list.each_with_index do |item, i| ;
|
|
2079
|
+
8: klass = i % 2 == 0 ? 'odd' : 'even' ;
|
|
2080
|
+
9: _buf << ( klass ).to_s;
|
|
2081
|
+
10: _buf << Erubis::XmlHelper.escape_xml( item );
|
|
2082
|
+
|
|
2083
|
+
12: end ;
|
|
2084
|
+
|
|
2085
|
+
16: _buf.join
|
|
2086
|
+
$ notext <strong>-nc</strong> example2.rhtml
|
|
2087
|
+
1: _buf = [];
|
|
2088
|
+
7: @list.each_with_index do |item, i| ;
|
|
2089
|
+
8: klass = i % 2 == 0 ? 'odd' : 'even' ;
|
|
2090
|
+
9: _buf << ( klass ).to_s;
|
|
2091
|
+
10: _buf << Erubis::XmlHelper.escape_xml( item );
|
|
2092
|
+
12: end ;
|
|
2093
|
+
16: _buf.join
|
|
2094
|
+
</pre>
|
|
2095
|
+
<p>Command 'notext' supports '-l <em>lang</em>' options. For example, command-line option '-l php' retrieves embedded PHP code from PHP file.
|
|
2096
|
+
</p>
|
|
1757
2097
|
<br>
|
|
1758
2098
|
|
|
1759
2099
|
|
|
@@ -1763,43 +2103,37 @@ example of using NoTextEnhancer with PHP file</div>
|
|
|
1763
2103
|
Here is an example result of benchmark.
|
|
1764
2104
|
</p>
|
|
1765
2105
|
<div class="output_caption">
|
|
1766
|
-
Env: Linux FedoraCore4, Celeron 667MHz, Mem512MB, Ruby1.8.4</div>
|
|
2106
|
+
Env: Linux FedoraCore4, Celeron 667MHz, Mem512MB, Ruby1.8.4, eruby1.0.5</div>
|
|
1767
2107
|
<pre class="output"> user system total real
|
|
1768
|
-
ERuby
|
|
1769
|
-
ERB
|
|
1770
|
-
Erubis::Eruby
|
|
1771
|
-
Erubis::StringBufferEruby
|
|
1772
|
-
Erubis::
|
|
1773
|
-
Erubis::
|
|
1774
|
-
Erubis::
|
|
1775
|
-
Erubis::TinyEruby 118.740000 2.360000 121.100000 (122.141380)
|
|
1776
|
-
Erubis::TinyStdoutEruby 86.140000 1.840000 87.980000 ( 88.679196)
|
|
2108
|
+
ERuby 131.990000 1.900000 133.890000 (135.061456)
|
|
2109
|
+
ERB 385.040000 3.180000 388.220000 (391.570653)
|
|
2110
|
+
Erubis::Eruby 127.750000 2.520000 130.270000 (131.385922)
|
|
2111
|
+
Erubis::StringBufferEruby 167.610000 2.600000 170.210000 (171.712798)
|
|
2112
|
+
Erubis::StdoutEruby 92.790000 2.000000 94.790000 ( 95.532633)
|
|
2113
|
+
Erubis::TinyEruby 118.720000 2.250000 120.970000 (122.022996)
|
|
2114
|
+
Erubis::TinyStdoutEruby 87.130000 2.030000 89.160000 ( 89.879538)
|
|
1777
2115
|
</pre>
|
|
1778
2116
|
<div class="output_caption">
|
|
1779
|
-
Env: MacOS X 10.4, PowerPC 1.42GHz, Mem1.5GB, Ruby1.8.4</div>
|
|
2117
|
+
Env: MacOS X 10.4, PowerPC 1.42GHz, Mem1.5GB, Ruby1.8.4, eruby1.0.5</div>
|
|
1780
2118
|
<pre class="output"> user system total real
|
|
1781
2119
|
ERuby 55.040000 2.120000 57.160000 ( 89.311397)
|
|
1782
2120
|
ERB 103.960000 3.480000 107.440000 (159.231792)
|
|
1783
2121
|
Erubis::Eruby 36.130000 1.570000 37.700000 ( 52.188574)
|
|
1784
2122
|
Erubis::StringBufferEruby 47.270000 1.980000 49.250000 ( 73.867537)
|
|
1785
|
-
Erubis::SimplifiedEruby 34.310000 1.600000 35.910000 ( 51.762841)
|
|
1786
2123
|
Erubis::StdoutEruby 26.240000 1.490000 27.730000 ( 41.840430)
|
|
1787
|
-
Erubis::StdoutSimplifiedEruby 25.380000 1.340000 26.720000 ( 37.231918)
|
|
1788
2124
|
Erubis::TinyEruby 31.690000 1.590000 33.280000 ( 49.862091)
|
|
1789
2125
|
Erubis::TinyStdoutEruby 22.550000 1.230000 23.780000 ( 33.316978)
|
|
1790
2126
|
</pre>
|
|
1791
2127
|
<p>This shows that:
|
|
1792
2128
|
</p>
|
|
1793
2129
|
<ul type="disc">
|
|
1794
|
-
<li>Erubis::Eruby is about
|
|
2130
|
+
<li>Erubis::Eruby is about 3 times faster than ERB.
|
|
1795
2131
|
</li>
|
|
1796
|
-
<li>Erubis::Eruby is about
|
|
1797
|
-
</li>
|
|
1798
|
-
<li>Erubis::SimplifiedEruby (which incudes SimplifiedEnhander) is faster than ERuby.
|
|
2132
|
+
<li>Erubis::Eruby is about 3% faster than ERuby in linux or 1.5 times faster in Mac.
|
|
1799
2133
|
</li>
|
|
1800
2134
|
<li>String buffer (StringBufferEnhancer) is slower than array buffer (ArrayBufferEnhancer which Erubis::Eruby includes)
|
|
1801
2135
|
</li>
|
|
1802
|
-
<li>Using
|
|
2136
|
+
<li>Using $stdout is faster than array buffer and string buffer.
|
|
1803
2137
|
</li>
|
|
1804
2138
|
<li>Erubis::TinyEruby (at 'erubis/tiny.rb') and it's subclasses are the fastest in all eRuby implementation.
|
|
1805
2139
|
</li>
|
|
@@ -1814,7 +2148,7 @@ Erubis::TinyStdoutEruby 22.550000 1.230000 23.780000 ( 33.316978)
|
|
|
1814
2148
|
<h2 class="section1">Command Reference</h2>
|
|
1815
2149
|
<a name="command-usage"></a>
|
|
1816
2150
|
<h3 class="section2">Usage</h3>
|
|
1817
|
-
<p>erubis [..options..] [<
|
|
2151
|
+
<p>erubis [..options..] [<em>file</em> ...]
|
|
1818
2152
|
</p>
|
|
1819
2153
|
<br>
|
|
1820
2154
|
|
|
@@ -1858,7 +2192,7 @@ Erubis::TinyStdoutEruby 22.550000 1.230000 23.780000 ( 33.316978)
|
|
|
1858
2192
|
-p pattern </b></dt>
|
|
1859
2193
|
<dd class="dd3">
|
|
1860
2194
|
Embedded pattern (default '<% %>').
|
|
1861
|
-
This is equivarent to '--pattern=<
|
|
2195
|
+
This is equivarent to '--pattern=<em>pattern</em>'.
|
|
1862
2196
|
</dd>
|
|
1863
2197
|
<dt class="dt3"><b>
|
|
1864
2198
|
-l lang </b></dt>
|
|
@@ -1907,6 +2241,11 @@ Erubis::TinyStdoutEruby 22.550000 1.230000 23.780000 ( 33.316978)
|
|
|
1907
2241
|
<dd class="dd3">
|
|
1908
2242
|
invoke Eruby#result() instead of Eruby#evaluate()
|
|
1909
2243
|
</dd>
|
|
2244
|
+
<dt class="dt3"><b>
|
|
2245
|
+
--pi[=name] </b></dt>
|
|
2246
|
+
<dd class="dd3">
|
|
2247
|
+
parse '<?name ... ?>' instead of '<% ... %>'
|
|
2248
|
+
</dd>
|
|
1910
2249
|
</dl>
|
|
1911
2250
|
<br>
|
|
1912
2251
|
|