erubis 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +340 -0
- data/ChangeLog +8 -0
- data/README +50 -0
- data/bin/erubis +187 -0
- data/doc/docstyle.css +188 -0
- data/doc/users-guide.html +676 -0
- data/lib/erubis.rb +254 -0
- data/setup.rb +1331 -0
- data/test/test-bin.rb +229 -0
- data/test/test-erubis.rb +461 -0
- metadata +52 -0
data/doc/docstyle.css
ADDED
@@ -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 '<% %>'
|
27
|
+
</li>
|
28
|
+
<li>Auto sanitizing
|
29
|
+
</li>
|
30
|
+
<li>Change embedded pattern (default '<% %>')
|
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"><ul>
|
123
|
+
<strong><% for item in list %></strong>
|
124
|
+
<li>
|
125
|
+
<strong><%= item %></strong>
|
126
|
+
</li>
|
127
|
+
<strong><% end %></strong>
|
128
|
+
</ul>
|
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 << "<ul>\n"
|
152
|
+
for item in list
|
153
|
+
_out << " <li>\n"
|
154
|
+
_out << " "; _out << ( item ).to_s; _out << "\n"
|
155
|
+
_out << " </li>\n"
|
156
|
+
end
|
157
|
+
_out << "</ul>\n"
|
158
|
+
_out
|
159
|
+
--- result ---
|
160
|
+
<ul>
|
161
|
+
<li>
|
162
|
+
aaa
|
163
|
+
</li>
|
164
|
+
<li>
|
165
|
+
bbb
|
166
|
+
</li>
|
167
|
+
<li>
|
168
|
+
ccc
|
169
|
+
</li>
|
170
|
+
</ul>
|
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 '<% %>' automatically, while it leaves spaces around '<%= %>'.
|
178
|
+
If you want leave spaces around '<% %>', add <code>:trim=>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"><ul>
|
184
|
+
<% for item in list %>
|
185
|
+
<li>
|
186
|
+
<%= item %>
|
187
|
+
</li>
|
188
|
+
<% end %>
|
189
|
+
</ul>
|
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=>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 << "<ul>\n"
|
213
|
+
<strong>_out << " ";</strong> for item in list <strong>; _out << "\n"</strong>
|
214
|
+
_out << " <li>\n"
|
215
|
+
_out << " "; _out << ( item ).to_s; _out << "\n"
|
216
|
+
_out << " </li>\n"
|
217
|
+
<strong>_out << " ";</strong> end <strong>; _out << "\n"</strong>
|
218
|
+
_out << "</ul>\n"
|
219
|
+
_out
|
220
|
+
--- result ---
|
221
|
+
<ul>
|
222
|
+
|
223
|
+
<li>
|
224
|
+
aaa
|
225
|
+
</li>
|
226
|
+
|
227
|
+
<li>
|
228
|
+
bbb
|
229
|
+
</li>
|
230
|
+
|
231
|
+
<li>
|
232
|
+
ccc
|
233
|
+
</li>
|
234
|
+
|
235
|
+
</ul>
|
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><%= <em>expr</em> %></code> will be sanitized.
|
248
|
+
</li>
|
249
|
+
<li><code><%== <em>expr</em> %></code> will be out as it is.
|
250
|
+
</li>
|
251
|
+
<li><code><%=== <em>expr</em> %></code> will be out to $stderr.
|
252
|
+
</li>
|
253
|
+
<li><code><%==== <em>expr</em> %></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"><ul>
|
260
|
+
<% for item in list %>
|
261
|
+
<li><strong><%=</strong> item <strong>%></strong></li>
|
262
|
+
<li><strong><%==</strong> item <strong>%></strong></li>
|
263
|
+
<li><strong><%===</strong> item <strong>%></strong></li>
|
264
|
+
|
265
|
+
<% end %>
|
266
|
+
</ul>
|
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 = ['<aaa>', 'b&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> stderr.log
|
288
|
+
--- script source ---
|
289
|
+
_out = ''; _out << "<ul>\n"
|
290
|
+
for item in list
|
291
|
+
_out << " <li>"; <strong>_out << Erubis::XmlEruby.escape( item )</strong>; _out << "</li>\n"
|
292
|
+
_out << " <li>"; _out << ( item ).to_s; _out << "</li>\n"
|
293
|
+
_out << " <li>"; <strong>$stderr.puts("** erubis: item = #{(item).inspect}")</strong>; _out << "</li>\n"
|
294
|
+
_out << "\n"
|
295
|
+
end
|
296
|
+
_out << "</ul>\n"
|
297
|
+
_out
|
298
|
+
--- result ---
|
299
|
+
<ul>
|
300
|
+
<li><strong>&lt;aaa&gt;</strong></li>
|
301
|
+
<li><aaa></li>
|
302
|
+
<li></li>
|
303
|
+
|
304
|
+
<li><strong>b&amp;b</strong></li>
|
305
|
+
<li>b&b</li>
|
306
|
+
<li></li>
|
307
|
+
|
308
|
+
<li><strong>&quot;ccc&quot;</strong></li>
|
309
|
+
<li>"ccc"</li>
|
310
|
+
<li></li>
|
311
|
+
|
312
|
+
</ul>
|
313
|
+
$ cat stderr.log
|
314
|
+
** erubis: item = "<aaa>"
|
315
|
+
** erubis: item = "b&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><% %></code>' to another.
|
324
|
+
</p>
|
325
|
+
<a name="example4.eruby"></a>
|
326
|
+
<div class="program_caption">
|
327
|
+
example4.eruby</div>
|
328
|
+
<pre class="program"><ul>
|
329
|
+
<strong><!--%</strong> for item in list <strong>%--></strong>
|
330
|
+
<li><strong><!--%=</strong> item <strong>%--></strong></li>
|
331
|
+
<strong><!--%</strong> end <strong>%--></strong>
|
332
|
+
</ul>
|
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=>'<!--% %-->'</strong>)
|
341
|
+
# or '<(?:!--)?% %(?:--)?>'
|
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 << "<ul>\n"
|
357
|
+
for item in list
|
358
|
+
_out << " <li>"; _out << ( item ).to_s; _out << "</li>\n"
|
359
|
+
end
|
360
|
+
_out << "</ul>\n"
|
361
|
+
_out
|
362
|
+
--- result ---
|
363
|
+
<ul>
|
364
|
+
<li>aaa</li>
|
365
|
+
<li>bbb</li>
|
366
|
+
<li>ccc</li>
|
367
|
+
</ul>
|
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><(!--)?% %(--)?></code>' will not work and '<code><(?:!--)?% %(?:--)?></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"><span><%= val %></span>
|
388
|
+
<ul>
|
389
|
+
<% for item in list %>
|
390
|
+
<li><%= item %></li>
|
391
|
+
<% end %>
|
392
|
+
</ul>
|
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
|
+
<span>Erubis Example</span>
|
417
|
+
<ul>
|
418
|
+
<li>aaa</li>
|
419
|
+
<li>bbb</li>
|
420
|
+
<li>ccc</li>
|
421
|
+
</ul>
|
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"><h1><%= title %></h1>
|
441
|
+
<ul>
|
442
|
+
<% for user in users %>
|
443
|
+
<li>
|
444
|
+
<a href="mailto:<%= user['mail']%>"><%= user['name'] %></a>
|
445
|
+
</li>
|
446
|
+
<% end %>
|
447
|
+
</ul>
|
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
|
+
<h1>Users List</h1>
|
468
|
+
<ul>
|
469
|
+
<li>
|
470
|
+
<a href="mailto:foo@mail.com">foo</a>
|
471
|
+
</li>
|
472
|
+
<li>
|
473
|
+
<a href="mailto:bar@mail.net">bar</a>
|
474
|
+
</li>
|
475
|
+
<li>
|
476
|
+
<a href="mailto:baz@mail.org">baz</a>
|
477
|
+
</li>
|
478
|
+
</ul>
|
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"><table>
|
492
|
+
<tbody>
|
493
|
+
<% for item in list %>
|
494
|
+
<tr>
|
495
|
+
<td><%= item %></td>
|
496
|
+
</tr>
|
497
|
+
<% end %>
|
498
|
+
</tbody>
|
499
|
+
</table>
|
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 << "<table>\n"
|
521
|
+
_out << " <tbody>\n"
|
522
|
+
for item in list
|
523
|
+
_out << " <tr>\n"
|
524
|
+
_out << " <td>"; _out << ( item ).to_s; _out << "</td>\n"
|
525
|
+
_out << " </tr>\n"
|
526
|
+
end
|
527
|
+
_out << " </tbody>\n"
|
528
|
+
_out << "</table>\n"
|
529
|
+
_out
|
530
|
+
--- script source (FastEruby) ---
|
531
|
+
_out = ''; _out << "<table>\n <tbody>\n"
|
532
|
+
|
533
|
+
for item in list
|
534
|
+
_out << " <tr>\n <td>"
|
535
|
+
_out << ( item ).to_s; _out << "</td>\n </tr>\n"
|
536
|
+
|
537
|
+
end
|
538
|
+
_out << " </tbody>\n</table>\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"><ul>
|
554
|
+
<% for item in list %>
|
555
|
+
<li><strong><% print item %></strong></li>
|
556
|
+
<% end %>
|
557
|
+
</ul>
|
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 << "<ul>\n"
|
582
|
+
for item in list
|
583
|
+
_out << " <li>"; <strong>print item</strong> ; _out << "</li>\n"
|
584
|
+
end
|
585
|
+
_out << "</ul>\n"
|
586
|
+
nil
|
587
|
+
--- result ---
|
588
|
+
<ul>
|
589
|
+
<li>aaa</li>
|
590
|
+
<li>bbb</li>
|
591
|
+
<li>ccc</li>
|
592
|
+
</ul>
|
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 '<% %>'.
|
631
|
+
</dd>
|
632
|
+
<dt class="dt3"><b>
|
633
|
+
-p pattern </b></dt>
|
634
|
+
<dd class="dd3">
|
635
|
+
Embedded pattern (default '<% %>').
|
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>
|