erubis 1.0.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.
@@ -0,0 +1,188 @@
1
+ body {
2
+ background-color:#FFFFFF;
3
+ }
4
+
5
+ .mainbody {
6
+ color:#333333;
7
+ line-height:150%;
8
+ margin: 5px 30px 5px 30px;
9
+ }
10
+
11
+ a:link, a:active, a:hover {
12
+ color:#CC6600;
13
+ }
14
+
15
+ a:visited {
16
+ color:#DD9900;
17
+ }
18
+
19
+ p {
20
+ color:#333333;
21
+ line-height:150%;
22
+ }
23
+
24
+ pre {
25
+ width: 100%;
26
+ line-height:130%;
27
+ white-space:pre;
28
+ }
29
+
30
+ .program {
31
+ border-style:solid;
32
+ border-width:1px;
33
+ border-color:#6699FF;
34
+ color:#333333;
35
+ background-color:#DDEEFF;
36
+ padding:8px 9px 8px 9px;
37
+ margin:0px;
38
+ word-break:break-all;
39
+ }
40
+
41
+ .terminal {
42
+ border-style:solid;
43
+ border-width:1;
44
+ border-color:#999999;
45
+ color:#333333;
46
+ background-color:#E0E0E0;
47
+ padding:9px 10px 9px 10px;
48
+ margin:0px;
49
+ word-break:break-all;
50
+ }
51
+
52
+ .output {
53
+ border-style:solid;
54
+ border-width:1px;
55
+ border-color:#CCCCCC;
56
+ color:#333333;
57
+ background-color:#FFFFFF;
58
+ padding:8px 9px 8px 9px;
59
+ margin:0px;
60
+ word-break:break-all;
61
+ }
62
+
63
+
64
+ .program_caption {
65
+ margin-top: 20px;
66
+ }
67
+
68
+ .terminal_caption {
69
+ margin-top: 20px;
70
+ }
71
+
72
+ .output_caption {
73
+ margin-top: 20px;
74
+ }
75
+
76
+
77
+ ul,ol,dl {
78
+ /* margin:0px; */
79
+ /* padding:0px; */
80
+ color:#333333;
81
+ line-height:140%;
82
+ }
83
+
84
+ .dt2, .dt3 {
85
+ font-weight:bold;
86
+ }
87
+
88
+ .table1 {
89
+ padding:2px;
90
+ color:#333333;
91
+ background-color:#DDDDCC;
92
+ line-height:130%;
93
+ /*
94
+ border-width:1px;
95
+ border-style:solid;
96
+ border-color:#FFFFFF;
97
+ */
98
+ margin:5;
99
+ }
100
+
101
+ .th1, .th2 {
102
+ padding:1px;
103
+ color:#333333;
104
+ /* background-color:#DDDDCC; */
105
+ background-color:#CCCCBB;
106
+ line-height:130%;
107
+ }
108
+
109
+ .td1, .th2 {
110
+ padding:1px;
111
+ color:#333333;
112
+ background-color:#EEEEDD;
113
+ line-height:130%;
114
+ }
115
+
116
+ .caption1, .caption2 {
117
+ /* font-size:x-small; */
118
+ color:#333333;
119
+ }
120
+
121
+ .table2 {
122
+ padding:1px;
123
+ color:#333333;
124
+ background-color:#DDDDCC;
125
+ line-height:130%;
126
+ /*
127
+ border-width:1px;
128
+ border-style:solid;
129
+ border-color:#FFFFFF;
130
+ */
131
+ margin:5;
132
+ }
133
+
134
+ h1, .chapter, .doctitle {
135
+ color:#333333;
136
+ font-weight:bold;
137
+ padding:30px 0px 10px 0px;
138
+ }
139
+
140
+ h2, .section {
141
+ color:#333333;
142
+ font-weight:bold;
143
+ border-style:solid;
144
+ border-color:#6699FF;
145
+ border-width:0px 0px 2px 30px;
146
+ padding:10px 20px 0px 5px;
147
+ }
148
+
149
+ h3, .subsection {
150
+ color:#333333;
151
+ font-weight:bold;
152
+ border-style:solid;
153
+ border-color:#6699FF;
154
+ border-width: 0px 0px 0px 15px;
155
+ padding: 10px 20px 0px 5px;
156
+ }
157
+
158
+ .em {
159
+ font-weight:bold;
160
+ }
161
+
162
+ .toc {
163
+ /* font-size:small; */
164
+ /* line-height:100%; */
165
+ }
166
+
167
+ .footnote {
168
+ font-size:small;
169
+ }
170
+
171
+ .note {
172
+ background-color:#FFFFDD;
173
+ border-style:solid;
174
+ border-width:0px 1px 0px 1px;
175
+ border-color:#DDDD66;
176
+ color:#333300;
177
+ /* font-size:small; */
178
+ line-height:120%;
179
+ padding: 5px 20px 5px 20px;
180
+ }
181
+
182
+ .figure {
183
+ /*
184
+ border-width:1px;
185
+ border-color:#DDDD66;
186
+ white-space:pre;
187
+ */
188
+ }
@@ -0,0 +1,676 @@
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
+
12
+ <blockquote>
13
+ <div class="mainbody">
14
+
15
+ <div align="left"><h1>Erubis Users' Guide</h1></div>
16
+ <div align="left">
17
+ last update: $Date: 2006-01-31 12:54:23 +0900 (Tue, 31 Jan 2006) $<br>
18
+ </div>
19
+
20
+ <a name="preface"></a>
21
+ <h2 class="section1">Preface</h2>
22
+ <p>Erubis is an implementation of eRuby.
23
+ It has the following features.
24
+ </p>
25
+ <ul type="disc">
26
+ <li>Auto trimming spaces around '&lt;% %&gt;'
27
+ </li>
28
+ <li>Auto sanitizing
29
+ </li>
30
+ <li>Change embedded pattern (default '&lt;% %&gt;')
31
+ </li>
32
+ <li>Context object available
33
+ </li>
34
+ <li>Easy to expand in subclass
35
+ </li>
36
+ </ul>
37
+ <p>Erubis is implemented in pure Ruby. It requires Ruby 1.8 or higher.
38
+ </p>
39
+ <a name="toc"></a>
40
+ <h3 class="section2">Table of Contents</h3>
41
+ <ul>
42
+ <li><a href="#preface">Preface</a>
43
+ <ul>
44
+ <li><a href="#toc">Table of Contents</a>
45
+ </li>
46
+ </ul>
47
+ </li>
48
+ <li><a href="#install">Installation</a>
49
+ </li>
50
+ <li><a href="#tutorial">Tutorial</a>
51
+ <ul>
52
+ <li><a href="#tut-basic">Basic Example</a>
53
+ </li>
54
+ <li><a href="#tut-trim">Trimming Spaces</a>
55
+ </li>
56
+ <li><a href="#tut-xml">Auto Sanitizing</a>
57
+ </li>
58
+ <li><a href="#tut-pattern">Embedded Pattern</a>
59
+ </li>
60
+ <li><a href="#tut-context">Context Object</a>
61
+ </li>
62
+ <li><a href="#tut-fast">Faster Eruby</a>
63
+ </li>
64
+ <li><a href="#tut-stdout">Stdout Eruby</a>
65
+ </li>
66
+ </ul>
67
+ </li>
68
+ <li><a href="#command">Command Reference</a>
69
+ <ul>
70
+ <li><a href="#command-usage">Usage</a>
71
+ </li>
72
+ <li><a href="#command-options">Options</a>
73
+ </li>
74
+ </ul>
75
+ </li>
76
+ </ul>
77
+ <br>
78
+
79
+
80
+ <br>
81
+
82
+
83
+ <a name="install"></a>
84
+ <h2 class="section1">Installation</h2>
85
+ <ul type="disc">
86
+ <li>If you have installed RubyGems, just type <code>gem install --remote erubis</code>.
87
+ <pre class="terminal">$ sudo gem install --remote erubis
88
+ </pre>
89
+ </li>
90
+ </ul>
91
+ <ul type="disc">
92
+ <li>Or if you can be root user, download erubis-X.X.X.tar.bz2 and install by setup.rb.
93
+ <pre class="terminal">$ tar xjf erubis-X.X.X.tar.bz2
94
+ $ cd erubis-X.X.X/
95
+ $ ruby setup.rb config
96
+ $ ruby setup.rb setup
97
+ $ sudo ruby setup.rb install
98
+ </pre>
99
+ </li>
100
+ </ul>
101
+ <ul type="disc">
102
+ <li>Else you should copy 'lib/erubis.rb' and 'bin/erubis' into proper directory manually.
103
+ <pre class="terminal">$ tar xjf erubis-X.X.X.tar.bz2
104
+ $ cd erubis-X.X.X/
105
+ $ cp lib/erubis.rb /usr/local/lib/ruby/site_ruby/1.8
106
+ $ cp bin/erubis /usr/local/bin
107
+ </pre>
108
+ </li>
109
+ </ul>
110
+ <br>
111
+
112
+
113
+ <a name="tutorial"></a>
114
+ <h2 class="section1">Tutorial</h2>
115
+ <a name="tut-basic"></a>
116
+ <h3 class="section2">Basic Example</h3>
117
+ <p>Here is a most basic example of Erubis.
118
+ </p>
119
+ <a name="example1.eruby"></a>
120
+ <div class="program_caption">
121
+ example1.eruby</div>
122
+ <pre class="program">&lt;ul&gt;
123
+ <strong>&lt;% for item in list %&gt;</strong>
124
+ &lt;li&gt;
125
+ <strong>&lt;%= item %&gt;</strong>
126
+ &lt;/li&gt;
127
+ <strong>&lt;% end %&gt;</strong>
128
+ &lt;/ul&gt;
129
+ </pre>
130
+ <a name="example1.rb"></a>
131
+ <div class="program_caption">
132
+ example1.rb</div>
133
+ <pre class="program">## create Eruby object
134
+ require 'erubis'
135
+ input = File.read('example1.eruby')
136
+ eruby = <strong>Erubis::Eruby.new(input)</strong>
137
+
138
+ ## print script source
139
+ puts "--- script source ---"
140
+ puts <strong>eruby.src</strong>
141
+
142
+ ## get result
143
+ puts "--- result ---"
144
+ list = ['aaa', 'bbb', 'ccc']
145
+ puts <strong>eruby.result(binding())</strong>
146
+ </pre>
147
+ <div class="terminal_caption">
148
+ output</div>
149
+ <pre class="terminal">$ ruby example1.rb
150
+ --- script source ---
151
+ _out = ''; _out &lt;&lt; "&lt;ul&gt;\n"
152
+ for item in list
153
+ _out &lt;&lt; " &lt;li&gt;\n"
154
+ _out &lt;&lt; " "; _out &lt;&lt; ( item ).to_s; _out &lt;&lt; "\n"
155
+ _out &lt;&lt; " &lt;/li&gt;\n"
156
+ end
157
+ _out &lt;&lt; "&lt;/ul&gt;\n"
158
+ _out
159
+ --- result ---
160
+ &lt;ul&gt;
161
+ &lt;li&gt;
162
+ aaa
163
+ &lt;/li&gt;
164
+ &lt;li&gt;
165
+ bbb
166
+ &lt;/li&gt;
167
+ &lt;li&gt;
168
+ ccc
169
+ &lt;/li&gt;
170
+ &lt;/ul&gt;
171
+ </pre>
172
+ <br>
173
+
174
+
175
+ <a name="tut-trim"></a>
176
+ <h3 class="section2">Trimming Spaces</h3>
177
+ <p>Erubis deletes spaces around '&lt;% %&gt;' automatically, while it leaves spaces around '&lt;%= %&gt;'.
178
+ If you want leave spaces around '&lt;% %&gt;', add <code>:trim=&gt;false</code> option to Erubis::Eruby.new().
179
+ </p>
180
+ <a name="example2.eruby"></a>
181
+ <div class="program_caption">
182
+ example2.eruby</div>
183
+ <pre class="program">&lt;ul&gt;
184
+ &lt;% for item in list %&gt;
185
+ &lt;li&gt;
186
+ &lt;%= item %&gt;
187
+ &lt;/li&gt;
188
+ &lt;% end %&gt;
189
+ &lt;/ul&gt;
190
+ </pre>
191
+ <a name="example2.rb"></a>
192
+ <div class="program_caption">
193
+ example2.rb</div>
194
+ <pre class="program">## create Eruby object
195
+ require 'erubis'
196
+ input = File.read('example2.eruby')
197
+ eruby = Erubis::Eruby.new(input<strong>, :trim=&gt;false</strong>)
198
+
199
+ ## print script source
200
+ puts "--- script source ---"
201
+ puts eruby.src
202
+
203
+ ## get result
204
+ puts "--- result ---"
205
+ list = ['aaa', 'bbb', 'ccc']
206
+ puts eruby.result(binding())
207
+ </pre>
208
+ <div class="terminal_caption">
209
+ output</div>
210
+ <pre class="terminal">$ ruby example2.rb
211
+ --- script source ---
212
+ _out = ''; _out &lt;&lt; "&lt;ul&gt;\n"
213
+ <strong>_out &lt;&lt; " ";</strong> for item in list <strong>; _out &lt;&lt; "\n"</strong>
214
+ _out &lt;&lt; " &lt;li&gt;\n"
215
+ _out &lt;&lt; " "; _out &lt;&lt; ( item ).to_s; _out &lt;&lt; "\n"
216
+ _out &lt;&lt; " &lt;/li&gt;\n"
217
+ <strong>_out &lt;&lt; " ";</strong> end <strong>; _out &lt;&lt; "\n"</strong>
218
+ _out &lt;&lt; "&lt;/ul&gt;\n"
219
+ _out
220
+ --- result ---
221
+ &lt;ul&gt;
222
+
223
+ &lt;li&gt;
224
+ aaa
225
+ &lt;/li&gt;
226
+
227
+ &lt;li&gt;
228
+ bbb
229
+ &lt;/li&gt;
230
+
231
+ &lt;li&gt;
232
+ ccc
233
+ &lt;/li&gt;
234
+
235
+ &lt;/ul&gt;
236
+ </pre>
237
+ <br>
238
+
239
+
240
+ <a name="tut-xml"></a>
241
+ <h3 class="section2">Auto Sanitizing</h3>
242
+ <p>If you use Erubis::XmlEruby instead of Erubis::Eruby, output is sanitized automatically.
243
+ </p>
244
+ <p>Erubis::XmlEruby acts the following:
245
+ </p>
246
+ <ul type="disc">
247
+ <li><code>&lt;%= <em>expr</em> %&gt;</code> will be sanitized.
248
+ </li>
249
+ <li><code>&lt;%== <em>expr</em> %&gt;</code> will be out as it is.
250
+ </li>
251
+ <li><code>&lt;%=== <em>expr</em> %&gt;</code> will be out to $stderr.
252
+ </li>
253
+ <li><code>&lt;%==== <em>expr</em> %&gt;</code> will be ignored.
254
+ </li>
255
+ </ul>
256
+ <a name="example3.eruby"></a>
257
+ <div class="program_caption">
258
+ example3.eruby</div>
259
+ <pre class="program">&lt;ul&gt;
260
+ &lt;% for item in list %&gt;
261
+ &lt;li&gt;<strong>&lt;%=</strong> item <strong>%&gt;</strong>&lt;/li&gt;
262
+ &lt;li&gt;<strong>&lt;%==</strong> item <strong>%&gt;</strong>&lt;/li&gt;
263
+ &lt;li&gt;<strong>&lt;%===</strong> item <strong>%&gt;</strong>&lt;/li&gt;
264
+
265
+ &lt;% end %&gt;
266
+ &lt;/ul&gt;
267
+ </pre>
268
+ <a name="example3.rb"></a>
269
+ <div class="program_caption">
270
+ example3.rb</div>
271
+ <pre class="program">## create Eruby object
272
+ require 'erubis'
273
+ input = File.read('example3.eruby')
274
+ eruby = Erubis::<strong>XmlEruby</strong>.new(input)
275
+
276
+ ## print script source
277
+ puts "--- script source ---"
278
+ puts eruby.src
279
+
280
+ ## get result
281
+ puts "--- result ---"
282
+ <strong>list = ['&lt;aaa&gt;', 'b&amp;b', '"ccc"']</strong>
283
+ puts eruby.result(binding())
284
+ </pre>
285
+ <div class="terminal_caption">
286
+ output</div>
287
+ <pre class="terminal">$ ruby example3.rb 2&gt; stderr.log
288
+ --- script source ---
289
+ _out = ''; _out &lt;&lt; "&lt;ul&gt;\n"
290
+ for item in list
291
+ _out &lt;&lt; " &lt;li&gt;"; <strong>_out &lt;&lt; Erubis::XmlEruby.escape( item )</strong>; _out &lt;&lt; "&lt;/li&gt;\n"
292
+ _out &lt;&lt; " &lt;li&gt;"; _out &lt;&lt; ( item ).to_s; _out &lt;&lt; "&lt;/li&gt;\n"
293
+ _out &lt;&lt; " &lt;li&gt;"; <strong>$stderr.puts("** erubis: item = #{(item).inspect}")</strong>; _out &lt;&lt; "&lt;/li&gt;\n"
294
+ _out &lt;&lt; "\n"
295
+ end
296
+ _out &lt;&lt; "&lt;/ul&gt;\n"
297
+ _out
298
+ --- result ---
299
+ &lt;ul&gt;
300
+ &lt;li&gt;<strong>&amp;lt;aaa&amp;gt;</strong>&lt;/li&gt;
301
+ &lt;li&gt;&lt;aaa&gt;&lt;/li&gt;
302
+ &lt;li&gt;&lt;/li&gt;
303
+
304
+ &lt;li&gt;<strong>b&amp;amp;b</strong>&lt;/li&gt;
305
+ &lt;li&gt;b&amp;b&lt;/li&gt;
306
+ &lt;li&gt;&lt;/li&gt;
307
+
308
+ &lt;li&gt;<strong>&amp;quot;ccc&amp;quot;</strong>&lt;/li&gt;
309
+ &lt;li&gt;"ccc"&lt;/li&gt;
310
+ &lt;li&gt;&lt;/li&gt;
311
+
312
+ &lt;/ul&gt;
313
+ $ cat stderr.log
314
+ ** erubis: item = "&lt;aaa&gt;"
315
+ ** erubis: item = "b&amp;b"
316
+ ** erubis: item = "\"ccc\""
317
+ </pre>
318
+ <br>
319
+
320
+
321
+ <a name="tut-pattern"></a>
322
+ <h3 class="section2">Embedded Pattern</h3>
323
+ <p>You can change embedded pattern '<code>&lt;% %&gt;</code>' to another.
324
+ </p>
325
+ <a name="example4.eruby"></a>
326
+ <div class="program_caption">
327
+ example4.eruby</div>
328
+ <pre class="program">&lt;ul&gt;
329
+ <strong>&lt;!--%</strong> for item in list <strong>%--&gt;</strong>
330
+ &lt;li&gt;<strong>&lt;!--%=</strong> item <strong>%--&gt;</strong>&lt;/li&gt;
331
+ <strong>&lt;!--%</strong> end <strong>%--&gt;</strong>
332
+ &lt;/ul&gt;
333
+ </pre>
334
+ <a name="example4.rb"></a>
335
+ <div class="program_caption">
336
+ example4.rb</div>
337
+ <pre class="program">## create Eruby object
338
+ require 'erubis'
339
+ input = File.read('example4.eruby')
340
+ eruby = Erubis::Eruby.new(input<strong>, :pattern=&gt;'&lt;!--% %--&gt;'</strong>)
341
+ # or '&lt;(?:!--)?% %(?:--)?&gt;'
342
+
343
+ ## print script source
344
+ puts "--- script source ---"
345
+ puts eruby.src
346
+
347
+ ## get result
348
+ puts "--- result ---"
349
+ list = ['aaa', 'bbb', 'ccc']
350
+ puts eruby.result(binding())
351
+ </pre>
352
+ <div class="terminal_caption">
353
+ output</div>
354
+ <pre class="terminal">$ ruby example4.rb
355
+ --- script source ---
356
+ _out = ''; _out &lt;&lt; "&lt;ul&gt;\n"
357
+ for item in list
358
+ _out &lt;&lt; " &lt;li&gt;"; _out &lt;&lt; ( item ).to_s; _out &lt;&lt; "&lt;/li&gt;\n"
359
+ end
360
+ _out &lt;&lt; "&lt;/ul&gt;\n"
361
+ _out
362
+ --- result ---
363
+ &lt;ul&gt;
364
+ &lt;li&gt;aaa&lt;/li&gt;
365
+ &lt;li&gt;bbb&lt;/li&gt;
366
+ &lt;li&gt;ccc&lt;/li&gt;
367
+ &lt;/ul&gt;
368
+ </pre>
369
+ <p>It is able to specify regular expression with :pattern option.
370
+ Notice that you must use '<code>(?: )</code>' instead of '<code>( )</code>' for grouping.
371
+ For example, '<code>&lt;(!--)?% %(--)?&gt;</code>' will not work and '<code>&lt;(?:!--)?% %(?:--)?&gt;</code>' will work.
372
+ </p>
373
+ <br>
374
+
375
+
376
+ <a name="tut-context"></a>
377
+ <h3 class="section2">Context Object</h3>
378
+ <p>Context object is a set of data which are used in eRuby script.
379
+ Using context object makes clear which data to be used.
380
+ </p>
381
+ <p>In Erubis, Hash object is used as context object.
382
+ Hash key means variable name and it can be string or symbol.
383
+ </p>
384
+ <a name="example5.eruby"></a>
385
+ <div class="program_caption">
386
+ example5.eruby</div>
387
+ <pre class="program">&lt;span&gt;&lt;%= val %&gt;&lt;/span&gt;
388
+ &lt;ul&gt;
389
+ &lt;% for item in list %&gt;
390
+ &lt;li&gt;&lt;%= item %&gt;&lt;/li&gt;
391
+ &lt;% end %&gt;
392
+ &lt;/ul&gt;
393
+ </pre>
394
+ <a name="example5.rb"></a>
395
+ <div class="program_caption">
396
+ example5.rb</div>
397
+ <pre class="program">## create Eruby object
398
+ require 'erubis'
399
+ input = File.read('example5.eruby')
400
+ eruby = Erubis::Eruby.new(input)
401
+
402
+ ## create context object
403
+ ## (key means var name, which may be string or symbol.)
404
+ <strong>context = {}</strong>
405
+ <strong>context[:val] = 'Erubis Example'</strong>
406
+ <strong>context['list'] = ['aaa', 'bbb', 'ccc']</strong>
407
+
408
+ ## get result
409
+ puts "--- result ---"
410
+ puts <strong>eruby.evaluate(context)</strong>
411
+ </pre>
412
+ <div class="terminal_caption">
413
+ output</div>
414
+ <pre class="terminal">$ ruby example5.rb
415
+ --- result ---
416
+ &lt;span&gt;Erubis Example&lt;/span&gt;
417
+ &lt;ul&gt;
418
+ &lt;li&gt;aaa&lt;/li&gt;
419
+ &lt;li&gt;bbb&lt;/li&gt;
420
+ &lt;li&gt;ccc&lt;/li&gt;
421
+ &lt;/ul&gt;
422
+ </pre>
423
+ <p>It is very useful to import YAML document data into context object.
424
+ </p>
425
+ <a name="example6.yaml"></a>
426
+ <div class="program_caption">
427
+ example6.yaml</div>
428
+ <pre class="program">title: Users List
429
+ users:
430
+ - name: foo
431
+ mail: foo@mail.com
432
+ - name: bar
433
+ mail: bar@mail.net
434
+ - name: baz
435
+ mail: baz@mail.org
436
+ </pre>
437
+ <a name="example6.eruby"></a>
438
+ <div class="program_caption">
439
+ example6.eruby</div>
440
+ <pre class="program">&lt;h1&gt;&lt;%= title %&gt;&lt;/h1&gt;
441
+ &lt;ul&gt;
442
+ &lt;% for user in users %&gt;
443
+ &lt;li&gt;
444
+ &lt;a href="mailto:&lt;%= user['mail']%&gt;"&gt;&lt;%= user['name'] %&gt;&lt;/a&gt;
445
+ &lt;/li&gt;
446
+ &lt;% end %&gt;
447
+ &lt;/ul&gt;
448
+ </pre>
449
+ <a name="example6.rb"></a>
450
+ <div class="program_caption">
451
+ example6.rb</div>
452
+ <pre class="program">## create Eruby object
453
+ require 'erubis'
454
+ input = File.read('example6.eruby')
455
+ eruby = Erubis::Eruby.new(input)
456
+
457
+ ## load YAML document as context object
458
+ <strong>require 'yaml'</strong>
459
+ <strong>context = YAML.load_file('example6.yaml')</strong>
460
+
461
+ ## get result
462
+ puts <strong>eruby.evaluate(context)</strong>
463
+ </pre>
464
+ <div class="terminal_caption">
465
+ output</div>
466
+ <pre class="terminal">$ ruby example6.rb
467
+ &lt;h1&gt;Users List&lt;/h1&gt;
468
+ &lt;ul&gt;
469
+ &lt;li&gt;
470
+ &lt;a href="mailto:foo@mail.com"&gt;foo&lt;/a&gt;
471
+ &lt;/li&gt;
472
+ &lt;li&gt;
473
+ &lt;a href="mailto:bar@mail.net"&gt;bar&lt;/a&gt;
474
+ &lt;/li&gt;
475
+ &lt;li&gt;
476
+ &lt;a href="mailto:baz@mail.org"&gt;baz&lt;/a&gt;
477
+ &lt;/li&gt;
478
+ &lt;/ul&gt;
479
+ </pre>
480
+ <br>
481
+
482
+
483
+ <a name="tut-fast"></a>
484
+ <h3 class="section2">Faster Eruby</h3>
485
+ <p>Erubis::FastEruby and Erubis::FastXmlEruby make faster Erubis::Eruby and Erubis::XmlEruby
486
+ to combine several strings into a string.
487
+ </p>
488
+ <a name="example7.eruby"></a>
489
+ <div class="program_caption">
490
+ example7.eruby</div>
491
+ <pre class="program">&lt;table&gt;
492
+ &lt;tbody&gt;
493
+ &lt;% for item in list %&gt;
494
+ &lt;tr&gt;
495
+ &lt;td&gt;&lt;%= item %&gt;&lt;/td&gt;
496
+ &lt;/tr&gt;
497
+ &lt;% end %&gt;
498
+ &lt;/tbody&gt;
499
+ &lt;/table&gt;
500
+ </pre>
501
+ <a name="example7.rb"></a>
502
+ <div class="program_caption">
503
+ example7.rb</div>
504
+ <pre class="program">## print script source with Eruby
505
+ require 'erubis'
506
+ input = File.read('example7.eruby')
507
+ eruby = Erubis::Eruby.new(input)
508
+ puts "--- script source (Eruby) ---"
509
+ puts eruby.src
510
+
511
+ ## print script source with FastEruby
512
+ eruby = Erubis::<strong>FastEruby</strong>.new(input)
513
+ puts "--- script source (FastEruby) ---"
514
+ puts eruby.src
515
+ </pre>
516
+ <div class="terminal_caption">
517
+ output</div>
518
+ <pre class="terminal">$ ruby example7.rb
519
+ --- script source (Eruby) ---
520
+ _out = ''; _out &lt;&lt; "&lt;table&gt;\n"
521
+ _out &lt;&lt; " &lt;tbody&gt;\n"
522
+ for item in list
523
+ _out &lt;&lt; " &lt;tr&gt;\n"
524
+ _out &lt;&lt; " &lt;td&gt;"; _out &lt;&lt; ( item ).to_s; _out &lt;&lt; "&lt;/td&gt;\n"
525
+ _out &lt;&lt; " &lt;/tr&gt;\n"
526
+ end
527
+ _out &lt;&lt; " &lt;/tbody&gt;\n"
528
+ _out &lt;&lt; "&lt;/table&gt;\n"
529
+ _out
530
+ --- script source (FastEruby) ---
531
+ _out = ''; _out &lt;&lt; "&lt;table&gt;\n &lt;tbody&gt;\n"
532
+
533
+ for item in list
534
+ _out &lt;&lt; " &lt;tr&gt;\n &lt;td&gt;"
535
+ _out &lt;&lt; ( item ).to_s; _out &lt;&lt; "&lt;/td&gt;\n &lt;/tr&gt;\n"
536
+
537
+ end
538
+ _out &lt;&lt; " &lt;/tbody&gt;\n&lt;/table&gt;\n"
539
+
540
+ _out
541
+ </pre>
542
+ <br>
543
+
544
+
545
+ <a name="tut-stdout"></a>
546
+ <h3 class="section2">Stdout Eruby</h3>
547
+ <p>Erubis::StdoutEruby and Erubis::StdoutXmlEruby use $stdout instead of string object.
548
+ Therefore, you can use 'print' statement in embedded ruby code.
549
+ </p>
550
+ <a name="example8.eruby"></a>
551
+ <div class="program_caption">
552
+ example8.eruby</div>
553
+ <pre class="program">&lt;ul&gt;
554
+ &lt;% for item in list %&gt;
555
+ &lt;li&gt;<strong>&lt;% print item %&gt;</strong>&lt;/li&gt;
556
+ &lt;% end %&gt;
557
+ &lt;/ul&gt;
558
+ </pre>
559
+ <a name="example8.rb"></a>
560
+ <div class="program_caption">
561
+ example8.rb</div>
562
+ <pre class="program">## create Eruby object
563
+ require 'erubis'
564
+ input = File.read('example8.eruby')
565
+ eruby = Erubis::<strong>StdoutEruby</strong>.new(input)
566
+
567
+ ## print script source
568
+ puts "--- script source ---"
569
+ puts eruby.src
570
+
571
+ ## get result
572
+ puts "--- result ---"
573
+ list = ['aaa', 'bbb', 'ccc']
574
+ #puts eruby.result(binding())
575
+ <strong>eruby.result(binding())</strong> # returns nil
576
+ </pre>
577
+ <div class="terminal_caption">
578
+ output</div>
579
+ <pre class="terminal">$ ruby example8.rb
580
+ --- script source ---
581
+ <strong>_out = $stdout</strong>; _out &lt;&lt; "&lt;ul&gt;\n"
582
+ for item in list
583
+ _out &lt;&lt; " &lt;li&gt;"; <strong>print item</strong> ; _out &lt;&lt; "&lt;/li&gt;\n"
584
+ end
585
+ _out &lt;&lt; "&lt;/ul&gt;\n"
586
+ nil
587
+ --- result ---
588
+ &lt;ul&gt;
589
+ &lt;li&gt;aaa&lt;/li&gt;
590
+ &lt;li&gt;bbb&lt;/li&gt;
591
+ &lt;li&gt;ccc&lt;/li&gt;
592
+ &lt;/ul&gt;
593
+ </pre>
594
+ <br>
595
+
596
+
597
+ <br>
598
+
599
+
600
+ <a name="command"></a>
601
+ <h2 class="section1">Command Reference</h2>
602
+ <a name="command-usage"></a>
603
+ <h3 class="section2">Usage</h3>
604
+ <p>erubis [-hvsT] [-p <em>pattern</em>] [-c <em>class</em>] [-K <em>kanji</em>] [-f <em>file.yaml</em>] [<em>file</em> ...]
605
+ </p>
606
+ <br>
607
+
608
+
609
+ <a name="command-options"></a>
610
+ <h3 class="section2">Options</h3>
611
+ <dl class="dl3" compact>
612
+ <dt class="dt3"><b>
613
+ -h, --help </b></dt>
614
+ <dd class="dd3">
615
+ Help.
616
+ </dd>
617
+ <dt class="dt3"><b>
618
+ -v </b></dt>
619
+ <dd class="dd3">
620
+ Release version.
621
+ </dd>
622
+ <dt class="dt3"><b>
623
+ -s </b></dt>
624
+ <dd class="dd3">
625
+ Show script source.
626
+ </dd>
627
+ <dt class="dt3"><b>
628
+ -T </b></dt>
629
+ <dd class="dd3">
630
+ No trimming spaces around '&lt;% %&gt;'.
631
+ </dd>
632
+ <dt class="dt3"><b>
633
+ -p pattern </b></dt>
634
+ <dd class="dd3">
635
+ Embedded pattern (default '&lt;% %&gt;').
636
+ </dd>
637
+ <dt class="dt3"><b>
638
+ -c class </b></dt>
639
+ <dd class="dd3">
640
+ Class name (Eruby, XmlEruby, FastEruby, ...) (default Eruby).
641
+ </dd>
642
+ <dt class="dt3"><b>
643
+ -I path </b></dt>
644
+ <dd class="dd3">
645
+ Require library path ($:).
646
+ It is able to specify several paths separating with ',' (ex. -f path1,path2,path3).
647
+ </dd>
648
+ <dt class="dt3"><b>
649
+ -K kanji </b></dt>
650
+ <dd class="dd3">
651
+ Kanji code (euc, sjis, utf8, or none) (default none).
652
+ </dd>
653
+ <dt class="dt3"><b>
654
+ -f file.yaml </b></dt>
655
+ <dd class="dd3">
656
+ YAML file for context values (read stdin if filename is '-').
657
+ It is able to specify several filenames separating with ',' (ex. -f file1,file2,file3).
658
+ </dd>
659
+ <dt class="dt3"><b>
660
+ --name=value </b></dt>
661
+ <dd class="dd3">
662
+ Variable name and value
663
+ </dd>
664
+ </dl>
665
+ <br>
666
+
667
+
668
+ <br>
669
+
670
+
671
+
672
+ </div>
673
+ </blockquote>
674
+
675
+ </body>
676
+ </html>