ed-precompiled_erb 5.0.3-x64-mingw-ucrt
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.
- checksums.yaml +7 -0
- data/.document +6 -0
- data/.github/dependabot.yml +6 -0
- data/.github/workflows/dependabot_automerge.yml +30 -0
- data/.github/workflows/sync-ruby.yml +33 -0
- data/.github/workflows/test.yml +36 -0
- data/.gitignore +12 -0
- data/.rdoc_options +1 -0
- data/BDSL +22 -0
- data/COPYING +56 -0
- data/Gemfile +11 -0
- data/LICENSE.txt +2 -0
- data/NEWS.md +69 -0
- data/README.md +98 -0
- data/Rakefile +19 -0
- data/_doc/cgi.rb +3 -0
- data/_doc/erb_executable.md +240 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/erb.gemspec +36 -0
- data/ext/erb/escape/escape.c +102 -0
- data/ext/erb/escape/extconf.rb +9 -0
- data/lib/erb/3.2/escape.so +0 -0
- data/lib/erb/3.3/escape.so +0 -0
- data/lib/erb/3.4/escape.so +0 -0
- data/lib/erb/compiler.rb +488 -0
- data/lib/erb/def_method.rb +47 -0
- data/lib/erb/escape.so +0 -0
- data/lib/erb/util.rb +77 -0
- data/lib/erb/version.rb +5 -0
- data/lib/erb.rb +1220 -0
- data/libexec/erb +184 -0
- metadata +80 -0
data/lib/erb.rb
ADDED
|
@@ -0,0 +1,1220 @@
|
|
|
1
|
+
# -*- coding: us-ascii -*-
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
# = ERB -- Ruby Templating
|
|
4
|
+
#
|
|
5
|
+
# Author:: Masatoshi SEKI
|
|
6
|
+
# Documentation:: James Edward Gray II, Gavin Sinclair, and Simon Chiang
|
|
7
|
+
#
|
|
8
|
+
# See ERB for primary documentation and ERB::Util for a couple of utility
|
|
9
|
+
# routines.
|
|
10
|
+
#
|
|
11
|
+
# Copyright (c) 1999-2000,2002,2003 Masatoshi SEKI
|
|
12
|
+
#
|
|
13
|
+
# You can redistribute it and/or modify it under the same terms as Ruby.
|
|
14
|
+
|
|
15
|
+
# A NOTE ABOUT TERMS:
|
|
16
|
+
#
|
|
17
|
+
# Formerly: The documentation in this file used the term _template_ to refer to an ERB object.
|
|
18
|
+
#
|
|
19
|
+
# Now: The documentation in this file uses the term _template_
|
|
20
|
+
# to refer to the string input to ERB.new.
|
|
21
|
+
#
|
|
22
|
+
# The reason for the change: When documenting the ERB executable erb,
|
|
23
|
+
# we need a term that refers to its string input;
|
|
24
|
+
# _source_ is not a good idea, because ERB#src means something entirely different;
|
|
25
|
+
# the two different sorts of sources would bring confusion.
|
|
26
|
+
#
|
|
27
|
+
# Therefore we use the term _template_ to refer to:
|
|
28
|
+
#
|
|
29
|
+
# - The string input to ERB.new
|
|
30
|
+
# - The string input to executable erb.
|
|
31
|
+
#
|
|
32
|
+
|
|
33
|
+
require 'erb/version'
|
|
34
|
+
require 'erb/compiler'
|
|
35
|
+
require 'erb/def_method'
|
|
36
|
+
require 'erb/util'
|
|
37
|
+
|
|
38
|
+
# :markup: markdown
|
|
39
|
+
#
|
|
40
|
+
# Class **ERB** (the name stands for **Embedded Ruby**)
|
|
41
|
+
# is an easy-to-use, but also very powerful, [template processor][template processor].
|
|
42
|
+
#
|
|
43
|
+
# ## Usage
|
|
44
|
+
#
|
|
45
|
+
# Before you can use \ERB, you must first require it
|
|
46
|
+
# (examples on this page assume that this has been done):
|
|
47
|
+
#
|
|
48
|
+
# ```
|
|
49
|
+
# require 'erb'
|
|
50
|
+
# ```
|
|
51
|
+
#
|
|
52
|
+
# ## In Brief
|
|
53
|
+
#
|
|
54
|
+
# Here's how \ERB works:
|
|
55
|
+
#
|
|
56
|
+
# - You can create a *template*: a plain-text string that includes specially formatted *tags*..
|
|
57
|
+
# - You can create an \ERB object to store the template.
|
|
58
|
+
# - You can call instance method ERB#result to get the *result*.
|
|
59
|
+
#
|
|
60
|
+
# \ERB supports tags of three kinds:
|
|
61
|
+
#
|
|
62
|
+
# - [Expression tags][expression tags]:
|
|
63
|
+
# each begins with `'<%'`, ends with `'%>'`; contains a Ruby expression;
|
|
64
|
+
# in the result, the value of the expression replaces the entire tag:
|
|
65
|
+
#
|
|
66
|
+
# template = 'The magic word is <%= magic_word %>.'
|
|
67
|
+
# erb = ERB.new(template)
|
|
68
|
+
# magic_word = 'xyzzy'
|
|
69
|
+
# erb.result(binding) # => "The magic word is xyzzy."
|
|
70
|
+
#
|
|
71
|
+
# The above call to #result passes argument `binding`,
|
|
72
|
+
# which contains the binding of variable `magic_word` to its string value `'xyzzy'`.
|
|
73
|
+
#
|
|
74
|
+
# The below call to #result need not pass a binding,
|
|
75
|
+
# because its expression `Date::DAYNAMES` is globally defined.
|
|
76
|
+
#
|
|
77
|
+
# ERB.new('Today is <%= Date::DAYNAMES[Date.today.wday] %>.').result # => "Today is Monday."
|
|
78
|
+
#
|
|
79
|
+
# - [Execution tags][execution tags]:
|
|
80
|
+
# each begins with `'<%='`, ends with `'%>'`; contains Ruby code to be executed:
|
|
81
|
+
#
|
|
82
|
+
# template = '<% File.write("t.txt", "Some stuff.") %>'
|
|
83
|
+
# ERB.new(template).result
|
|
84
|
+
# File.read('t.txt') # => "Some stuff."
|
|
85
|
+
#
|
|
86
|
+
# - [Comment tags][comment tags]:
|
|
87
|
+
# each begins with `'<%#'`, ends with `'%>'`; contains comment text;
|
|
88
|
+
# in the result, the entire tag is omitted.
|
|
89
|
+
#
|
|
90
|
+
# template = 'Some stuff;<%# Note to self: figure out what the stuff is. %> more stuff.'
|
|
91
|
+
# ERB.new(template).result # => "Some stuff; more stuff."
|
|
92
|
+
#
|
|
93
|
+
# ## Some Simple Examples
|
|
94
|
+
#
|
|
95
|
+
# Here's a simple example of \ERB in action:
|
|
96
|
+
#
|
|
97
|
+
# ```
|
|
98
|
+
# template = 'The time is <%= Time.now %>.'
|
|
99
|
+
# erb = ERB.new(template)
|
|
100
|
+
# erb.result
|
|
101
|
+
# # => "The time is 2025-09-09 10:49:26 -0500."
|
|
102
|
+
# ```
|
|
103
|
+
#
|
|
104
|
+
# Details:
|
|
105
|
+
#
|
|
106
|
+
# 1. A plain-text string is assigned to variable `template`.
|
|
107
|
+
# Its embedded [expression tag][expression tags] `'<%= Time.now %>'` includes a Ruby expression, `Time.now`.
|
|
108
|
+
# 2. The string is put into a new \ERB object, and stored in variable `erb`.
|
|
109
|
+
# 4. Method call `erb.result` generates a string that contains the run-time value of `Time.now`,
|
|
110
|
+
# as computed at the time of the call.
|
|
111
|
+
#
|
|
112
|
+
# The
|
|
113
|
+
# \ERB object may be re-used:
|
|
114
|
+
#
|
|
115
|
+
# ```
|
|
116
|
+
# erb.result
|
|
117
|
+
# # => "The time is 2025-09-09 10:49:33 -0500."
|
|
118
|
+
# ```
|
|
119
|
+
#
|
|
120
|
+
# Another example:
|
|
121
|
+
#
|
|
122
|
+
# ```
|
|
123
|
+
# template = 'The magic word is <%= magic_word %>.'
|
|
124
|
+
# erb = ERB.new(template)
|
|
125
|
+
# magic_word = 'abracadabra'
|
|
126
|
+
# erb.result(binding)
|
|
127
|
+
# # => "The magic word is abracadabra."
|
|
128
|
+
# ```
|
|
129
|
+
#
|
|
130
|
+
# Details:
|
|
131
|
+
#
|
|
132
|
+
# 1. As before, a plain-text string is assigned to variable `template`.
|
|
133
|
+
# Its embedded [expression tag][expression tags] `'<%= magic_word %>'` has a variable *name*, `magic_word`.
|
|
134
|
+
# 2. The string is put into a new \ERB object, and stored in variable `erb`;
|
|
135
|
+
# note that `magic_word` need not be defined before the \ERB object is created.
|
|
136
|
+
# 3. `magic_word = 'abracadabra'` assigns a value to variable `magic_word`.
|
|
137
|
+
# 4. Method call `erb.result(binding)` generates a string
|
|
138
|
+
# that contains the *value* of `magic_word`.
|
|
139
|
+
#
|
|
140
|
+
# As before, the \ERB object may be re-used:
|
|
141
|
+
#
|
|
142
|
+
# ```
|
|
143
|
+
# magic_word = 'xyzzy'
|
|
144
|
+
# erb.result(binding)
|
|
145
|
+
# # => "The magic word is xyzzy."
|
|
146
|
+
# ```
|
|
147
|
+
#
|
|
148
|
+
# ## Bindings
|
|
149
|
+
#
|
|
150
|
+
# A call to method #result, which produces the formatted result string,
|
|
151
|
+
# requires a [Binding object][binding object] as its argument.
|
|
152
|
+
#
|
|
153
|
+
# The binding object provides the bindings for expressions in [expression tags][expression tags].
|
|
154
|
+
#
|
|
155
|
+
# There are three ways to provide the required binding:
|
|
156
|
+
#
|
|
157
|
+
# - [Default binding][default binding].
|
|
158
|
+
# - [Local binding][local binding].
|
|
159
|
+
# - [Augmented binding][augmented binding]
|
|
160
|
+
#
|
|
161
|
+
# ### Default Binding
|
|
162
|
+
#
|
|
163
|
+
# When you pass no `binding` argument to method #result,
|
|
164
|
+
# the method uses its default binding: the one returned by method #new_toplevel.
|
|
165
|
+
# This binding has the bindings defined by Ruby itself,
|
|
166
|
+
# which are those for Ruby's constants and variables.
|
|
167
|
+
#
|
|
168
|
+
# That binding is sufficient for an expression tag that refers only to Ruby's constants and variables;
|
|
169
|
+
# these expression tags refer only to Ruby's global constant `RUBY_COPYRIGHT` and global variable `$0`:
|
|
170
|
+
#
|
|
171
|
+
# ```
|
|
172
|
+
# template = <<TEMPLATE
|
|
173
|
+
# The Ruby copyright is <%= RUBY_COPYRIGHT.inspect %>.
|
|
174
|
+
# The current process is <%= $0 %>.
|
|
175
|
+
# TEMPLATE
|
|
176
|
+
# puts ERB.new(template).result
|
|
177
|
+
# The Ruby copyright is "ruby - Copyright (C) 1993-2025 Yukihiro Matsumoto".
|
|
178
|
+
# The current process is irb.
|
|
179
|
+
# ```
|
|
180
|
+
#
|
|
181
|
+
# (The current process is `irb` because that's where we're doing these examples!)
|
|
182
|
+
#
|
|
183
|
+
# ### Local Binding
|
|
184
|
+
#
|
|
185
|
+
# The default binding is *not* sufficient for an expression
|
|
186
|
+
# that refers to a a constant or variable that is not defined there:
|
|
187
|
+
#
|
|
188
|
+
# ```
|
|
189
|
+
# Foo = 1 # Defines local constant Foo.
|
|
190
|
+
# foo = 2 # Defines local variable foo.
|
|
191
|
+
# template = <<TEMPLATE
|
|
192
|
+
# The current value of constant Foo is <%= Foo %>.
|
|
193
|
+
# The current value of variable foo is <%= foo %>.
|
|
194
|
+
# The Ruby copyright is <%= RUBY_COPYRIGHT.inspect %>.
|
|
195
|
+
# The current process is <%= $0 %>.
|
|
196
|
+
# TEMPLATE
|
|
197
|
+
# erb = ERB.new(template)
|
|
198
|
+
# ```
|
|
199
|
+
#
|
|
200
|
+
# This call below raises `NameError` because although `Foo` and `foo` are defined locally,
|
|
201
|
+
# they are not defined in the default binding:
|
|
202
|
+
#
|
|
203
|
+
# ```
|
|
204
|
+
# erb.result # Raises NameError.
|
|
205
|
+
# ```
|
|
206
|
+
#
|
|
207
|
+
# To make the locally-defined constants and variables available,
|
|
208
|
+
# you can call #result with the local binding:
|
|
209
|
+
#
|
|
210
|
+
# ```
|
|
211
|
+
# puts erb.result(binding)
|
|
212
|
+
# The current value of constant Foo is 1.
|
|
213
|
+
# The current value of variable foo is 2.
|
|
214
|
+
# The Ruby copyright is "ruby - Copyright (C) 1993-2025 Yukihiro Matsumoto".
|
|
215
|
+
# The current process is irb.
|
|
216
|
+
# ```
|
|
217
|
+
#
|
|
218
|
+
# ### Augmented Binding
|
|
219
|
+
#
|
|
220
|
+
# Another way to make variable bindings (but not constant bindings) available
|
|
221
|
+
# is to use method #result_with_hash(hash);
|
|
222
|
+
# the passed hash has name/value pairs that are to be used to define and assign variables
|
|
223
|
+
# in a copy of the default binding:
|
|
224
|
+
#
|
|
225
|
+
# ```
|
|
226
|
+
# template = <<TEMPLATE
|
|
227
|
+
# The current value of variable bar is <%= bar %>.
|
|
228
|
+
# The current value of variable baz is <%= baz %>.
|
|
229
|
+
# The Ruby copyright is <%= RUBY_COPYRIGHT.inspect %>.
|
|
230
|
+
# The current process is <%= $0 %>.
|
|
231
|
+
# TEMPLATE
|
|
232
|
+
# erb = ERB.new(template)
|
|
233
|
+
# ```
|
|
234
|
+
#
|
|
235
|
+
# Both of these calls raise `NameError`, because `bar` and `baz`
|
|
236
|
+
# are not defined in either the default binding or the local binding.
|
|
237
|
+
#
|
|
238
|
+
# ```
|
|
239
|
+
# puts erb.result # Raises NameError.
|
|
240
|
+
# puts erb.result(binding) # Raises NameError.
|
|
241
|
+
# ```
|
|
242
|
+
#
|
|
243
|
+
# This call passes a hash that causes `bar` and `baz` to be defined
|
|
244
|
+
# in a new binding (derived from #new_toplevel):
|
|
245
|
+
#
|
|
246
|
+
# ```
|
|
247
|
+
# hash = {bar: 3, baz: 4}
|
|
248
|
+
# puts erb.result_with_hash(hash)
|
|
249
|
+
# The current value of variable bar is 3.
|
|
250
|
+
# The current value of variable baz is 4.
|
|
251
|
+
# The Ruby copyright is "ruby - Copyright (C) 1993-2025 Yukihiro Matsumoto".
|
|
252
|
+
# The current process is irb.
|
|
253
|
+
# ```
|
|
254
|
+
#
|
|
255
|
+
# ## Tags
|
|
256
|
+
#
|
|
257
|
+
# The examples above use expression tags.
|
|
258
|
+
# These are the tags available in \ERB:
|
|
259
|
+
#
|
|
260
|
+
# - [Expression tag][expression tags]: the tag contains a Ruby expression;
|
|
261
|
+
# in the result, the entire tag is to be replaced with the run-time value of the expression.
|
|
262
|
+
# - [Execution tag][execution tags]: the tag contains Ruby code;
|
|
263
|
+
# in the result, the entire tag is to be replaced with the run-time value of the code.
|
|
264
|
+
# - [Comment tag][comment tags]: the tag contains comment code;
|
|
265
|
+
# in the result, the entire tag is to be omitted.
|
|
266
|
+
#
|
|
267
|
+
# ### Expression Tags
|
|
268
|
+
#
|
|
269
|
+
# You can embed a Ruby expression in a template using an *expression tag*.
|
|
270
|
+
#
|
|
271
|
+
# Its syntax is `<%= _expression_ %>`,
|
|
272
|
+
# where *expression* is any valid Ruby expression.
|
|
273
|
+
#
|
|
274
|
+
# When you call method #result,
|
|
275
|
+
# the method evaluates the expression and replaces the entire expression tag with the expression's value:
|
|
276
|
+
#
|
|
277
|
+
# ```
|
|
278
|
+
# ERB.new('Today is <%= Date::DAYNAMES[Date.today.wday] %>.').result
|
|
279
|
+
# # => "Today is Monday."
|
|
280
|
+
# ERB.new('Tomorrow will be <%= Date::DAYNAMES[Date.today.wday + 1] %>.').result
|
|
281
|
+
# # => "Tomorrow will be Tuesday."
|
|
282
|
+
# ERB.new('Yesterday was <%= Date::DAYNAMES[Date.today.wday - 1] %>.').result
|
|
283
|
+
# # => "Yesterday was Sunday."
|
|
284
|
+
# ```
|
|
285
|
+
#
|
|
286
|
+
# Note that whitespace before and after the expression
|
|
287
|
+
# is allowed but not required,
|
|
288
|
+
# and that such whitespace is stripped from the result.
|
|
289
|
+
#
|
|
290
|
+
# ```
|
|
291
|
+
# ERB.new('My appointment is on <%=Date::DAYNAMES[Date.today.wday + 2]%>.').result
|
|
292
|
+
# # => "My appointment is on Wednesday."
|
|
293
|
+
# ERB.new('My appointment is on <%= Date::DAYNAMES[Date.today.wday + 2] %>.').result
|
|
294
|
+
# # => "My appointment is on Wednesday."
|
|
295
|
+
# ```
|
|
296
|
+
#
|
|
297
|
+
# ### Execution Tags
|
|
298
|
+
#
|
|
299
|
+
# You can embed Ruby executable code in template using an *execution tag*.
|
|
300
|
+
#
|
|
301
|
+
# Its syntax is `<% _code_ %>`,
|
|
302
|
+
# where *code* is any valid Ruby code.
|
|
303
|
+
#
|
|
304
|
+
# When you call method #result,
|
|
305
|
+
# the method executes the code and removes the entire execution tag
|
|
306
|
+
# (generating no text in the result):
|
|
307
|
+
#
|
|
308
|
+
# ```
|
|
309
|
+
# ERB.new('foo <% Dir.chdir("C:/") %> bar').result # => "foo bar"
|
|
310
|
+
# ```
|
|
311
|
+
#
|
|
312
|
+
# Whitespace before and after the embedded code is optional:
|
|
313
|
+
#
|
|
314
|
+
# ```
|
|
315
|
+
# ERB.new('foo <%Dir.chdir("C:/")%> bar').result # => "foo bar"
|
|
316
|
+
# ```
|
|
317
|
+
#
|
|
318
|
+
# You can interleave text with execution tags to form a control structure
|
|
319
|
+
# such as a conditional, a loop, or a `case` statements.
|
|
320
|
+
#
|
|
321
|
+
# Conditional:
|
|
322
|
+
#
|
|
323
|
+
# ```
|
|
324
|
+
# template = <<TEMPLATE
|
|
325
|
+
# <% if verbosity %>
|
|
326
|
+
# An error has occurred.
|
|
327
|
+
# <% else %>
|
|
328
|
+
# Oops!
|
|
329
|
+
# <% end %>
|
|
330
|
+
# TEMPLATE
|
|
331
|
+
# erb = ERB.new(template)
|
|
332
|
+
# verbosity = true
|
|
333
|
+
# erb.result(binding)
|
|
334
|
+
# # => "\nAn error has occurred.\n\n"
|
|
335
|
+
# verbosity = false
|
|
336
|
+
# erb.result(binding)
|
|
337
|
+
# # => "\nOops!\n\n"
|
|
338
|
+
# ```
|
|
339
|
+
#
|
|
340
|
+
# Note that the interleaved text may itself contain expression tags:
|
|
341
|
+
#
|
|
342
|
+
# Loop:
|
|
343
|
+
#
|
|
344
|
+
# ```
|
|
345
|
+
# template = <<TEMPLATE
|
|
346
|
+
# <% Date::ABBR_DAYNAMES.each do |dayname| %>
|
|
347
|
+
# <%= dayname %>
|
|
348
|
+
# <% end %>
|
|
349
|
+
# TEMPLATE
|
|
350
|
+
# ERB.new(template).result
|
|
351
|
+
# # => "\nSun\n\nMon\n\nTue\n\nWed\n\nThu\n\nFri\n\nSat\n\n"
|
|
352
|
+
# ```
|
|
353
|
+
#
|
|
354
|
+
# Other, non-control, lines of Ruby code may be interleaved with the text,
|
|
355
|
+
# and the Ruby code may itself contain regular Ruby comments:
|
|
356
|
+
#
|
|
357
|
+
# ```
|
|
358
|
+
# template = <<TEMPLATE
|
|
359
|
+
# <% 3.times do %>
|
|
360
|
+
# <%= Time.now %>
|
|
361
|
+
# <% sleep(1) # Let's make the times different. %>
|
|
362
|
+
# <% end %>
|
|
363
|
+
# TEMPLATE
|
|
364
|
+
# ERB.new(template).result
|
|
365
|
+
# # => "\n2025-09-09 11:36:02 -0500\n\n\n2025-09-09 11:36:03 -0500\n\n\n2025-09-09 11:36:04 -0500\n\n\n"
|
|
366
|
+
# ```
|
|
367
|
+
#
|
|
368
|
+
# The execution tag may also contain multiple lines of code:
|
|
369
|
+
#
|
|
370
|
+
# ```
|
|
371
|
+
# template = <<TEMPLATE
|
|
372
|
+
# <%
|
|
373
|
+
# (0..2).each do |i|
|
|
374
|
+
# (0..2).each do |j|
|
|
375
|
+
# %>
|
|
376
|
+
# * <%=i%>,<%=j%>
|
|
377
|
+
# <%
|
|
378
|
+
# end
|
|
379
|
+
# end
|
|
380
|
+
# %>
|
|
381
|
+
# TEMPLATE
|
|
382
|
+
# ERB.new(template).result
|
|
383
|
+
# # => "\n* 0,0\n\n* 0,1\n\n* 0,2\n\n* 1,0\n\n* 1,1\n\n* 1,2\n\n* 2,0\n\n* 2,1\n\n* 2,2\n\n"
|
|
384
|
+
# ```
|
|
385
|
+
#
|
|
386
|
+
# #### Shorthand Format for Execution Tags
|
|
387
|
+
#
|
|
388
|
+
# You can use keyword argument `trim_mode: '%'` to enable a shorthand format for execution tags;
|
|
389
|
+
# this example uses the shorthand format `% _code_` instead of `<% _code_ %>`:
|
|
390
|
+
#
|
|
391
|
+
# ```
|
|
392
|
+
# template = <<TEMPLATE
|
|
393
|
+
# % priorities.each do |priority|
|
|
394
|
+
# * <%= priority %>
|
|
395
|
+
# % end
|
|
396
|
+
# TEMPLATE
|
|
397
|
+
# erb = ERB.new(template, trim_mode: '%')
|
|
398
|
+
# priorities = [ 'Run Ruby Quiz',
|
|
399
|
+
# 'Document Modules',
|
|
400
|
+
# 'Answer Questions on Ruby Talk' ]
|
|
401
|
+
# puts erb.result(binding)
|
|
402
|
+
# * Run Ruby Quiz
|
|
403
|
+
# * Document Modules
|
|
404
|
+
# * Answer Questions on Ruby Talk
|
|
405
|
+
# ```
|
|
406
|
+
#
|
|
407
|
+
# Note that in the shorthand format, the character `'%'` must be the first character in the code line
|
|
408
|
+
# (no leading whitespace).
|
|
409
|
+
#
|
|
410
|
+
# #### Suppressing Unwanted Blank Lines
|
|
411
|
+
#
|
|
412
|
+
# With keyword argument `trim_mode` not given,
|
|
413
|
+
# all blank lines go into the result:
|
|
414
|
+
#
|
|
415
|
+
# ```
|
|
416
|
+
# template = <<TEMPLATE
|
|
417
|
+
# <% if true %>
|
|
418
|
+
# <%= RUBY_VERSION %>
|
|
419
|
+
# <% end %>
|
|
420
|
+
# TEMPLATE
|
|
421
|
+
# ERB.new(template).result.lines.each {|line| puts line.inspect }
|
|
422
|
+
# "\n"
|
|
423
|
+
# "3.4.5\n"
|
|
424
|
+
# "\n"
|
|
425
|
+
# ```
|
|
426
|
+
#
|
|
427
|
+
# You can give `trim_mode: '-'`, you can suppress each blank line
|
|
428
|
+
# whose source line ends with `-%>` (instead of `%>`):
|
|
429
|
+
#
|
|
430
|
+
# ```
|
|
431
|
+
# template = <<TEMPLATE
|
|
432
|
+
# <% if true -%>
|
|
433
|
+
# <%= RUBY_VERSION %>
|
|
434
|
+
# <% end -%>
|
|
435
|
+
# TEMPLATE
|
|
436
|
+
# ERB.new(template, trim_mode: '-').result.lines.each {|line| puts line.inspect }
|
|
437
|
+
# "3.4.5\n"
|
|
438
|
+
# ```
|
|
439
|
+
#
|
|
440
|
+
# It is an error to use the trailing `'-%>'` notation without `trim_mode: '-'`:
|
|
441
|
+
#
|
|
442
|
+
# ```
|
|
443
|
+
# ERB.new(template).result.lines.each {|line| puts line.inspect } # Raises SyntaxError.
|
|
444
|
+
# ```
|
|
445
|
+
#
|
|
446
|
+
# #### Suppressing Unwanted Newlines
|
|
447
|
+
#
|
|
448
|
+
# Consider this template:
|
|
449
|
+
#
|
|
450
|
+
# ```
|
|
451
|
+
# template = <<TEMPLATE
|
|
452
|
+
# <% RUBY_VERSION %>
|
|
453
|
+
# <%= RUBY_VERSION %>
|
|
454
|
+
# foo <% RUBY_VERSION %>
|
|
455
|
+
# foo <%= RUBY_VERSION %>
|
|
456
|
+
# TEMPLATE
|
|
457
|
+
# ```
|
|
458
|
+
#
|
|
459
|
+
# With keyword argument `trim_mode` not given, all newlines go into the result:
|
|
460
|
+
#
|
|
461
|
+
# ```
|
|
462
|
+
# ERB.new(template).result.lines.each {|line| puts line.inspect }
|
|
463
|
+
# "\n"
|
|
464
|
+
# "3.4.5\n"
|
|
465
|
+
# "foo \n"
|
|
466
|
+
# "foo 3.4.5\n"
|
|
467
|
+
# ```
|
|
468
|
+
#
|
|
469
|
+
# You can give `trim_mode: '>'` to suppress the trailing newline
|
|
470
|
+
# for each line that ends with `'%<'` (regardless of its beginning):
|
|
471
|
+
#
|
|
472
|
+
# ```
|
|
473
|
+
# ERB.new(template, trim_mode: '>').result.lines.each {|line| puts line.inspect }
|
|
474
|
+
# "3.4.5foo foo 3.4.5"
|
|
475
|
+
# ```
|
|
476
|
+
#
|
|
477
|
+
# You can give `trim_mode: '<>'` to suppress the trailing newline
|
|
478
|
+
# for each line that both begins with `'<%'` and ends with `'%>'`:
|
|
479
|
+
#
|
|
480
|
+
# ```
|
|
481
|
+
# ERB.new(template, trim_mode: '<>').result.lines.each {|line| puts line.inspect }
|
|
482
|
+
# "3.4.5foo \n"
|
|
483
|
+
# "foo 3.4.5\n"
|
|
484
|
+
# ```
|
|
485
|
+
#
|
|
486
|
+
# #### Combining Trim Modes
|
|
487
|
+
#
|
|
488
|
+
# You can combine certain trim modes:
|
|
489
|
+
#
|
|
490
|
+
# - `'%-'`: Enable shorthand and omit each blank line ending with `'-%>'`.
|
|
491
|
+
# - `'%>'`: Enable shorthand and omit newline for each line ending with `'%>'`.
|
|
492
|
+
# - `'%<>'`: Enable shorthand and omit newline for each line starting with `'<%'` and ending with `'%>'`.
|
|
493
|
+
#
|
|
494
|
+
# ### Comment Tags
|
|
495
|
+
#
|
|
496
|
+
# You can embed a comment in a template using a *comment tag*;
|
|
497
|
+
# its syntax is `<%# _text_ %>`,
|
|
498
|
+
# where *text* is the text of the comment.
|
|
499
|
+
#
|
|
500
|
+
# When you call method #result,
|
|
501
|
+
# it removes the entire comment tag
|
|
502
|
+
# (generating no text in the result).
|
|
503
|
+
#
|
|
504
|
+
# Example:
|
|
505
|
+
#
|
|
506
|
+
# ```
|
|
507
|
+
# template = 'Some stuff;<%# Note to self: figure out what the stuff is. %> more stuff.'
|
|
508
|
+
# ERB.new(template).result # => "Some stuff; more stuff."
|
|
509
|
+
# ```
|
|
510
|
+
#
|
|
511
|
+
# A comment tag may appear anywhere in the template.
|
|
512
|
+
#
|
|
513
|
+
# Note that the beginning of the tag must be `'<%#'`, not `'<% #'`.
|
|
514
|
+
#
|
|
515
|
+
# In this example, the tag begins with `'<% #'`, and so is an execution tag, not a comment tag;
|
|
516
|
+
# the cited code consists entirely of a Ruby-style comment (which is of course ignored):
|
|
517
|
+
#
|
|
518
|
+
# ```
|
|
519
|
+
# ERB.new('Some stuff;<% # Note to self: figure out what the stuff is. %> more stuff.').result
|
|
520
|
+
# # => "Some stuff;"
|
|
521
|
+
# ```
|
|
522
|
+
#
|
|
523
|
+
# ## Encodings
|
|
524
|
+
#
|
|
525
|
+
# An \ERB object has an [encoding][encoding],
|
|
526
|
+
# which is by default the encoding of the template string;
|
|
527
|
+
# the result string will also have that encoding.
|
|
528
|
+
#
|
|
529
|
+
# ```
|
|
530
|
+
# template = <<TEMPLATE
|
|
531
|
+
# <%# Comment. %>
|
|
532
|
+
# TEMPLATE
|
|
533
|
+
# erb = ERB.new(template)
|
|
534
|
+
# template.encoding # => #<Encoding:UTF-8>
|
|
535
|
+
# erb.encoding # => #<Encoding:UTF-8>
|
|
536
|
+
# erb.result.encoding # => #<Encoding:UTF-8>
|
|
537
|
+
# ```
|
|
538
|
+
#
|
|
539
|
+
# You can specify a different encoding by adding a [magic comment][magic comments]
|
|
540
|
+
# at the top of the given template:
|
|
541
|
+
#
|
|
542
|
+
# ```
|
|
543
|
+
# template = <<TEMPLATE
|
|
544
|
+
# <%#-*- coding: Big5 -*-%>
|
|
545
|
+
# <%# Comment. %>
|
|
546
|
+
# TEMPLATE
|
|
547
|
+
# erb = ERB.new(template)
|
|
548
|
+
# template.encoding # => #<Encoding:UTF-8>
|
|
549
|
+
# erb.encoding # => #<Encoding:Big5>
|
|
550
|
+
# erb.result.encoding # => #<Encoding:Big5>
|
|
551
|
+
# ```
|
|
552
|
+
#
|
|
553
|
+
# ## Error Reporting
|
|
554
|
+
#
|
|
555
|
+
# Consider this template (containing an error):
|
|
556
|
+
#
|
|
557
|
+
# ```
|
|
558
|
+
# template = '<%= nosuch %>'
|
|
559
|
+
# erb = ERB.new(template)
|
|
560
|
+
# ```
|
|
561
|
+
#
|
|
562
|
+
# When \ERB reports an error,
|
|
563
|
+
# it includes a file name (if available) and a line number;
|
|
564
|
+
# the file name comes from method #filename, the line number from method #lineno.
|
|
565
|
+
#
|
|
566
|
+
# Initially, those values are `nil` and `0`, respectively;
|
|
567
|
+
# these initial values are reported as `'(erb)'` and `1`, respectively:
|
|
568
|
+
#
|
|
569
|
+
# ```
|
|
570
|
+
# erb.filename # => nil
|
|
571
|
+
# erb.lineno # => 0
|
|
572
|
+
# erb.result
|
|
573
|
+
# (erb):1:in '<main>': undefined local variable or method 'nosuch' for main (NameError)
|
|
574
|
+
# ```
|
|
575
|
+
#
|
|
576
|
+
# You can use methods #filename= and #lineno= to assign values
|
|
577
|
+
# that are more meaningful in your context:
|
|
578
|
+
#
|
|
579
|
+
# ```
|
|
580
|
+
# erb.filename = 't.txt'
|
|
581
|
+
# erb.lineno = 555
|
|
582
|
+
# erb.result
|
|
583
|
+
# t.txt:556:in '<main>': undefined local variable or method 'nosuch' for main (NameError)
|
|
584
|
+
# ```
|
|
585
|
+
#
|
|
586
|
+
# You can use method #location= to set both values:
|
|
587
|
+
#
|
|
588
|
+
# ```
|
|
589
|
+
# erb.location = ['u.txt', 999]
|
|
590
|
+
# erb.result
|
|
591
|
+
# u.txt:1000:in '<main>': undefined local variable or method 'nosuch' for main (NameError)
|
|
592
|
+
# ```
|
|
593
|
+
#
|
|
594
|
+
# ## Plain Text with Embedded Ruby
|
|
595
|
+
#
|
|
596
|
+
# Here's a plain-text template;
|
|
597
|
+
# it uses the literal notation `'%q{ ... }'` to define the template
|
|
598
|
+
# (see [%q literals][%q literals]);
|
|
599
|
+
# this avoids problems with backslashes.
|
|
600
|
+
#
|
|
601
|
+
# ```
|
|
602
|
+
# template = %q{
|
|
603
|
+
# From: James Edward Gray II <james@grayproductions.net>
|
|
604
|
+
# To: <%= to %>
|
|
605
|
+
# Subject: Addressing Needs
|
|
606
|
+
#
|
|
607
|
+
# <%= to[/\w+/] %>:
|
|
608
|
+
#
|
|
609
|
+
# Just wanted to send a quick note assuring that your needs are being
|
|
610
|
+
# addressed.
|
|
611
|
+
#
|
|
612
|
+
# I want you to know that my team will keep working on the issues,
|
|
613
|
+
# especially:
|
|
614
|
+
#
|
|
615
|
+
# <%# ignore numerous minor requests -- focus on priorities %>
|
|
616
|
+
# % priorities.each do |priority|
|
|
617
|
+
# * <%= priority %>
|
|
618
|
+
# % end
|
|
619
|
+
#
|
|
620
|
+
# Thanks for your patience.
|
|
621
|
+
#
|
|
622
|
+
# James Edward Gray II
|
|
623
|
+
# }
|
|
624
|
+
# ```
|
|
625
|
+
#
|
|
626
|
+
# The template will need these:
|
|
627
|
+
#
|
|
628
|
+
# ```
|
|
629
|
+
# to = 'Community Spokesman <spokesman@ruby_community.org>'
|
|
630
|
+
# priorities = [ 'Run Ruby Quiz',
|
|
631
|
+
# 'Document Modules',
|
|
632
|
+
# 'Answer Questions on Ruby Talk' ]
|
|
633
|
+
# ```
|
|
634
|
+
#
|
|
635
|
+
# Finally, create the \ERB object and get the result
|
|
636
|
+
#
|
|
637
|
+
# ```
|
|
638
|
+
# erb = ERB.new(template, trim_mode: '%<>')
|
|
639
|
+
# puts erb.result(binding)
|
|
640
|
+
#
|
|
641
|
+
# From: James Edward Gray II <james@grayproductions.net>
|
|
642
|
+
# To: Community Spokesman <spokesman@ruby_community.org>
|
|
643
|
+
# Subject: Addressing Needs
|
|
644
|
+
#
|
|
645
|
+
# Community:
|
|
646
|
+
#
|
|
647
|
+
# Just wanted to send a quick note assuring that your needs are being
|
|
648
|
+
# addressed.
|
|
649
|
+
#
|
|
650
|
+
# I want you to know that my team will keep working on the issues,
|
|
651
|
+
# especially:
|
|
652
|
+
#
|
|
653
|
+
# * Run Ruby Quiz
|
|
654
|
+
# * Document Modules
|
|
655
|
+
# * Answer Questions on Ruby Talk
|
|
656
|
+
#
|
|
657
|
+
# Thanks for your patience.
|
|
658
|
+
#
|
|
659
|
+
# James Edward Gray II
|
|
660
|
+
# ```
|
|
661
|
+
#
|
|
662
|
+
# ## HTML with Embedded Ruby
|
|
663
|
+
#
|
|
664
|
+
# This example shows an HTML template.
|
|
665
|
+
#
|
|
666
|
+
# First, here's a custom class, `Product`:
|
|
667
|
+
#
|
|
668
|
+
# ```
|
|
669
|
+
# class Product
|
|
670
|
+
# def initialize(code, name, desc, cost)
|
|
671
|
+
# @code = code
|
|
672
|
+
# @name = name
|
|
673
|
+
# @desc = desc
|
|
674
|
+
# @cost = cost
|
|
675
|
+
# @features = []
|
|
676
|
+
# end
|
|
677
|
+
#
|
|
678
|
+
# def add_feature(feature)
|
|
679
|
+
# @features << feature
|
|
680
|
+
# end
|
|
681
|
+
#
|
|
682
|
+
# # Support templating of member data.
|
|
683
|
+
# def get_binding
|
|
684
|
+
# binding
|
|
685
|
+
# end
|
|
686
|
+
#
|
|
687
|
+
# end
|
|
688
|
+
# ```
|
|
689
|
+
#
|
|
690
|
+
# The template below will need these values:
|
|
691
|
+
#
|
|
692
|
+
# ```
|
|
693
|
+
# toy = Product.new('TZ-1002',
|
|
694
|
+
# 'Rubysapien',
|
|
695
|
+
# "Geek's Best Friend! Responds to Ruby commands...",
|
|
696
|
+
# 999.95
|
|
697
|
+
# )
|
|
698
|
+
# toy.add_feature('Listens for verbal commands in the Ruby language!')
|
|
699
|
+
# toy.add_feature('Ignores Perl, Java, and all C variants.')
|
|
700
|
+
# toy.add_feature('Karate-Chop Action!!!')
|
|
701
|
+
# toy.add_feature('Matz signature on left leg.')
|
|
702
|
+
# toy.add_feature('Gem studded eyes... Rubies, of course!')
|
|
703
|
+
# ```
|
|
704
|
+
#
|
|
705
|
+
# Here's the HTML:
|
|
706
|
+
#
|
|
707
|
+
# ```
|
|
708
|
+
# template = <<TEMPLATE
|
|
709
|
+
# <html>
|
|
710
|
+
# <head><title>Ruby Toys -- <%= @name %></title></head>
|
|
711
|
+
# <body>
|
|
712
|
+
# <h1><%= @name %> (<%= @code %>)</h1>
|
|
713
|
+
# <p><%= @desc %></p>
|
|
714
|
+
# <ul>
|
|
715
|
+
# <% @features.each do |f| %>
|
|
716
|
+
# <li><b><%= f %></b></li>
|
|
717
|
+
# <% end %>
|
|
718
|
+
# </ul>
|
|
719
|
+
# <p>
|
|
720
|
+
# <% if @cost < 10 %>
|
|
721
|
+
# <b>Only <%= @cost %>!!!</b>
|
|
722
|
+
# <% else %>
|
|
723
|
+
# Call for a price, today!
|
|
724
|
+
# <% end %>
|
|
725
|
+
# </p>
|
|
726
|
+
# </body>
|
|
727
|
+
# </html>
|
|
728
|
+
# TEMPLATE
|
|
729
|
+
# ```
|
|
730
|
+
#
|
|
731
|
+
# Finally, create the \ERB object and get the result (omitting some blank lines):
|
|
732
|
+
#
|
|
733
|
+
# ```
|
|
734
|
+
# erb = ERB.new(template)
|
|
735
|
+
# puts erb.result(toy.get_binding)
|
|
736
|
+
# <html>
|
|
737
|
+
# <head><title>Ruby Toys -- Rubysapien</title></head>
|
|
738
|
+
# <body>
|
|
739
|
+
# <h1>Rubysapien (TZ-1002)</h1>
|
|
740
|
+
# <p>Geek's Best Friend! Responds to Ruby commands...</p>
|
|
741
|
+
# <ul>
|
|
742
|
+
# <li><b>Listens for verbal commands in the Ruby language!</b></li>
|
|
743
|
+
# <li><b>Ignores Perl, Java, and all C variants.</b></li>
|
|
744
|
+
# <li><b>Karate-Chop Action!!!</b></li>
|
|
745
|
+
# <li><b>Matz signature on left leg.</b></li>
|
|
746
|
+
# <li><b>Gem studded eyes... Rubies, of course!</b></li>
|
|
747
|
+
# </ul>
|
|
748
|
+
# <p>
|
|
749
|
+
# Call for a price, today!
|
|
750
|
+
# </p>
|
|
751
|
+
# </body>
|
|
752
|
+
# </html>
|
|
753
|
+
# ```
|
|
754
|
+
#
|
|
755
|
+
#
|
|
756
|
+
# ## Other Template Processors
|
|
757
|
+
#
|
|
758
|
+
# Various Ruby projects have their own template processors.
|
|
759
|
+
# The Ruby Processing System [RDoc][rdoc], for example, has one that can be used elsewhere.
|
|
760
|
+
#
|
|
761
|
+
# Other popular template processors may found in the [Template Engines][template engines] page
|
|
762
|
+
# of the Ruby Toolbox.
|
|
763
|
+
#
|
|
764
|
+
# [%q literals]: https://docs.ruby-lang.org/en/master/syntax/literals_rdoc.html#label-25q-3A+Non-Interpolable+String+Literals
|
|
765
|
+
# [augmented binding]: rdoc-ref:ERB@Augmented+Binding
|
|
766
|
+
# [binding object]: https://docs.ruby-lang.org/en/master/Binding.html
|
|
767
|
+
# [comment tags]: rdoc-ref:ERB@Comment+Tags
|
|
768
|
+
# [default binding]: rdoc-ref:ERB@Default+Binding
|
|
769
|
+
# [encoding]: https://docs.ruby-lang.org/en/master/Encoding.html
|
|
770
|
+
# [execution tags]: rdoc-ref:ERB@Execution+Tags
|
|
771
|
+
# [expression tags]: rdoc-ref:ERB@Expression+Tags
|
|
772
|
+
# [kernel#binding]: https://docs.ruby-lang.org/en/master/Kernel.html#method-i-binding
|
|
773
|
+
# [local binding]: rdoc-ref:ERB@Local+Binding
|
|
774
|
+
# [magic comments]: https://docs.ruby-lang.org/en/master/syntax/comments_rdoc.html#label-Magic+Comments
|
|
775
|
+
# [rdoc]: https://ruby.github.io/rdoc
|
|
776
|
+
# [sprintf]: https://docs.ruby-lang.org/en/master/Kernel.html#method-i-sprintf
|
|
777
|
+
# [template engines]: https://www.ruby-toolbox.com/categories/template_engines
|
|
778
|
+
# [template processor]: https://en.wikipedia.org/wiki/Template_processor
|
|
779
|
+
#
|
|
780
|
+
class ERB
|
|
781
|
+
Revision = '$Date:: $' # :nodoc: #'
|
|
782
|
+
deprecate_constant :Revision
|
|
783
|
+
|
|
784
|
+
# :markup: markdown
|
|
785
|
+
#
|
|
786
|
+
# :call-seq:
|
|
787
|
+
# self.version -> string
|
|
788
|
+
#
|
|
789
|
+
# Returns the string \ERB version.
|
|
790
|
+
def self.version
|
|
791
|
+
VERSION
|
|
792
|
+
end
|
|
793
|
+
|
|
794
|
+
# :markup: markdown
|
|
795
|
+
#
|
|
796
|
+
# :call-seq:
|
|
797
|
+
# ERB.new(template, trim_mode: nil, eoutvar: '_erbout')
|
|
798
|
+
#
|
|
799
|
+
# Returns a new \ERB object containing the given string +template+.
|
|
800
|
+
#
|
|
801
|
+
# For details about `template`, its embedded tags, and generated results, see ERB.
|
|
802
|
+
#
|
|
803
|
+
# **Keyword Argument `trim_mode`**
|
|
804
|
+
#
|
|
805
|
+
# You can use keyword argument `trim_mode: '%'`
|
|
806
|
+
# to enable the [shorthand format][shorthand format] for execution tags.
|
|
807
|
+
#
|
|
808
|
+
# This value allows [blank line control][blank line control]:
|
|
809
|
+
#
|
|
810
|
+
# - `'-'`: Omit each blank line ending with `'%>'`.
|
|
811
|
+
#
|
|
812
|
+
# Other values allow [newline control][newline control]:
|
|
813
|
+
#
|
|
814
|
+
# - `'>'`: Omit newline for each line ending with `'%>'`.
|
|
815
|
+
# - `'<>'`: Omit newline for each line starting with `'<%'` and ending with `'%>'`.
|
|
816
|
+
#
|
|
817
|
+
# You can also [combine trim modes][combine trim modes].
|
|
818
|
+
#
|
|
819
|
+
# **Keyword Argument `eoutvar`**
|
|
820
|
+
#
|
|
821
|
+
# The string value of keyword argument `eoutvar` specifies the name of the variable
|
|
822
|
+
# that method #result uses to construct its result string;
|
|
823
|
+
# see #src.
|
|
824
|
+
#
|
|
825
|
+
# This is useful when you need to run multiple \ERB templates through the same binding
|
|
826
|
+
# and/or when you want to control where output ends up.
|
|
827
|
+
#
|
|
828
|
+
# It's good practice to choose a variable name that begins with an underscore: `'_'`.
|
|
829
|
+
#
|
|
830
|
+
# <b>Backward Compatibility</b>
|
|
831
|
+
#
|
|
832
|
+
# The calling sequence given above -- which is the one you should use --
|
|
833
|
+
# is a simplified version of the complete formal calling sequence,
|
|
834
|
+
# which is:
|
|
835
|
+
#
|
|
836
|
+
# ```
|
|
837
|
+
# ERB.new(template,
|
|
838
|
+
# safe_level=NOT_GIVEN, legacy_trim_mode=NOT_GIVEN, legacy_eoutvar=NOT_GIVEN,
|
|
839
|
+
# trim_mode: nil, eoutvar: '_erbout')
|
|
840
|
+
# ```
|
|
841
|
+
#
|
|
842
|
+
# The second, third, and fourth positional arguments (those in the second line above) are deprecated;
|
|
843
|
+
# this method issues warnings if they are given.
|
|
844
|
+
#
|
|
845
|
+
# However, their values, if given, are handled thus:
|
|
846
|
+
#
|
|
847
|
+
# - `safe_level`: ignored.
|
|
848
|
+
# - `legacy_trim_mode`: overrides keyword argument `trim_mode`.
|
|
849
|
+
# - `legacy_eoutvar`: overrides keyword argument `eoutvar`.
|
|
850
|
+
#
|
|
851
|
+
# [blank line control]: rdoc-ref:ERB@Suppressing+Unwanted+Blank+Lines
|
|
852
|
+
# [combine trim modes]: rdoc-ref:ERB@Combining+Trim+Modes
|
|
853
|
+
# [newline control]: rdoc-ref:ERB@Suppressing+Unwanted+Newlines
|
|
854
|
+
# [shorthand format]: rdoc-ref:ERB@Shorthand+Format+for+Execution+Tags
|
|
855
|
+
#
|
|
856
|
+
def initialize(str, safe_level=NOT_GIVEN, legacy_trim_mode=NOT_GIVEN, legacy_eoutvar=NOT_GIVEN, trim_mode: nil, eoutvar: '_erbout')
|
|
857
|
+
# Complex initializer for $SAFE deprecation at [Feature #14256]. Use keyword arguments to pass trim_mode or eoutvar.
|
|
858
|
+
if safe_level != NOT_GIVEN
|
|
859
|
+
warn 'Passing safe_level with the 2nd argument of ERB.new is deprecated. Do not use it, and specify other arguments as keyword arguments.', uplevel: 1
|
|
860
|
+
end
|
|
861
|
+
if legacy_trim_mode != NOT_GIVEN
|
|
862
|
+
warn 'Passing trim_mode with the 3rd argument of ERB.new is deprecated. Use keyword argument like ERB.new(str, trim_mode: ...) instead.', uplevel: 1
|
|
863
|
+
trim_mode = legacy_trim_mode
|
|
864
|
+
end
|
|
865
|
+
if legacy_eoutvar != NOT_GIVEN
|
|
866
|
+
warn 'Passing eoutvar with the 4th argument of ERB.new is deprecated. Use keyword argument like ERB.new(str, eoutvar: ...) instead.', uplevel: 1
|
|
867
|
+
eoutvar = legacy_eoutvar
|
|
868
|
+
end
|
|
869
|
+
|
|
870
|
+
compiler = make_compiler(trim_mode)
|
|
871
|
+
set_eoutvar(compiler, eoutvar)
|
|
872
|
+
@src, @encoding, @frozen_string = *compiler.compile(str)
|
|
873
|
+
@filename = nil
|
|
874
|
+
@lineno = 0
|
|
875
|
+
@_init = self.class.singleton_class
|
|
876
|
+
end
|
|
877
|
+
|
|
878
|
+
# :markup: markdown
|
|
879
|
+
#
|
|
880
|
+
# Placeholder constant; used as default value for certain method arguments.
|
|
881
|
+
NOT_GIVEN = defined?(Ractor) ? Ractor.make_shareable(Object.new) : Object.new
|
|
882
|
+
private_constant :NOT_GIVEN
|
|
883
|
+
|
|
884
|
+
# :markup: markdown
|
|
885
|
+
#
|
|
886
|
+
# :call-seq:
|
|
887
|
+
# make_compiler -> erb_compiler
|
|
888
|
+
#
|
|
889
|
+
# Returns a new ERB::Compiler with the given `trim_mode`;
|
|
890
|
+
# for `trim_mode` values, see ERB.new:
|
|
891
|
+
#
|
|
892
|
+
# ```
|
|
893
|
+
# ERB.new('').make_compiler(nil)
|
|
894
|
+
# # => #<ERB::Compiler:0x000001cff9467678 @insert_cmd="print", @percent=false, @post_cmd=[], @pre_cmd=[], @put_cmd="print", @trim_mode=nil>
|
|
895
|
+
# ```
|
|
896
|
+
#
|
|
897
|
+
|
|
898
|
+
def make_compiler(trim_mode)
|
|
899
|
+
ERB::Compiler.new(trim_mode)
|
|
900
|
+
end
|
|
901
|
+
|
|
902
|
+
# :markup: markdown
|
|
903
|
+
#
|
|
904
|
+
# Returns the Ruby code that, when executed, generates the result;
|
|
905
|
+
# the code is executed by method #result,
|
|
906
|
+
# and by its wrapper methods #result_with_hash and #run:
|
|
907
|
+
#
|
|
908
|
+
# ```
|
|
909
|
+
# template = 'The time is <%= Time.now %>.'
|
|
910
|
+
# erb = ERB.new(template)
|
|
911
|
+
# erb.src
|
|
912
|
+
# # => "#coding:UTF-8\n_erbout = +''; _erbout.<< \"The time is \".freeze; _erbout.<<(( Time.now ).to_s); _erbout.<< \".\".freeze; _erbout"
|
|
913
|
+
# erb.result
|
|
914
|
+
# # => "The time is 2025-09-18 15:58:08 -0500."
|
|
915
|
+
# ```
|
|
916
|
+
#
|
|
917
|
+
# In a more readable format:
|
|
918
|
+
#
|
|
919
|
+
# ```
|
|
920
|
+
# # puts erb.src.split('; ')
|
|
921
|
+
# # #coding:UTF-8
|
|
922
|
+
# # _erbout = +''
|
|
923
|
+
# # _erbout.<< "The time is ".freeze
|
|
924
|
+
# # _erbout.<<(( Time.now ).to_s)
|
|
925
|
+
# # _erbout.<< ".".freeze
|
|
926
|
+
# # _erbout
|
|
927
|
+
# ```
|
|
928
|
+
#
|
|
929
|
+
# Variable `_erbout` is used to store the intermediate results in the code;
|
|
930
|
+
# the name `_erbout` is the default in ERB.new,
|
|
931
|
+
# and can be changed via keyword argument `eoutvar`:
|
|
932
|
+
#
|
|
933
|
+
# ```
|
|
934
|
+
# erb = ERB.new(template, eoutvar: '_foo')
|
|
935
|
+
# puts template.src.split('; ')
|
|
936
|
+
# #coding:UTF-8
|
|
937
|
+
# _foo = +''
|
|
938
|
+
# _foo.<< "The time is ".freeze
|
|
939
|
+
# _foo.<<(( Time.now ).to_s)
|
|
940
|
+
# _foo.<< ".".freeze
|
|
941
|
+
# _foo
|
|
942
|
+
# ```
|
|
943
|
+
#
|
|
944
|
+
attr_reader :src
|
|
945
|
+
|
|
946
|
+
# :markup: markdown
|
|
947
|
+
#
|
|
948
|
+
# Returns the encoding of `self`;
|
|
949
|
+
# see [Encodings][encodings]:
|
|
950
|
+
#
|
|
951
|
+
# [encodings]: rdoc-ref:ERB@Encodings
|
|
952
|
+
#
|
|
953
|
+
attr_reader :encoding
|
|
954
|
+
|
|
955
|
+
# :markup: markdown
|
|
956
|
+
#
|
|
957
|
+
# Sets or returns the file name to be used in reporting errors;
|
|
958
|
+
# see [Error Reporting][error reporting].
|
|
959
|
+
#
|
|
960
|
+
# [error reporting]: rdoc-ref:ERB@Error+Reporting
|
|
961
|
+
attr_accessor :filename
|
|
962
|
+
|
|
963
|
+
# :markup: markdown
|
|
964
|
+
#
|
|
965
|
+
# Sets or returns the line number to be used in reporting errors;
|
|
966
|
+
# see [Error Reporting][error reporting].
|
|
967
|
+
#
|
|
968
|
+
# [error reporting]: rdoc-ref:ERB@Error+Reporting
|
|
969
|
+
attr_accessor :lineno
|
|
970
|
+
|
|
971
|
+
# :markup: markdown
|
|
972
|
+
#
|
|
973
|
+
# :call-seq:
|
|
974
|
+
# location = [filename, lineno] => [filename, lineno]
|
|
975
|
+
# location = filename -> filename
|
|
976
|
+
#
|
|
977
|
+
# Sets the values of #filename and, if given, #lineno;
|
|
978
|
+
# see [Error Reporting][error reporting].
|
|
979
|
+
#
|
|
980
|
+
# [error reporting]: rdoc-ref:ERB@Error+Reporting
|
|
981
|
+
def location=((filename, lineno))
|
|
982
|
+
@filename = filename
|
|
983
|
+
@lineno = lineno if lineno
|
|
984
|
+
end
|
|
985
|
+
|
|
986
|
+
# :markup: markdown
|
|
987
|
+
#
|
|
988
|
+
# :call-seq:
|
|
989
|
+
# set_eoutvar(compiler, eoutvar = '_erbout') -> [eoutvar]
|
|
990
|
+
#
|
|
991
|
+
# Sets the `eoutvar` value in the ERB::Compiler object `compiler`;
|
|
992
|
+
# returns a 1-element array containing the value of `eoutvar`:
|
|
993
|
+
#
|
|
994
|
+
# ```
|
|
995
|
+
# template = ERB.new('')
|
|
996
|
+
# compiler = template.make_compiler(nil)
|
|
997
|
+
# pp compiler
|
|
998
|
+
# #<ERB::Compiler:0x000001cff8a9aa00
|
|
999
|
+
# @insert_cmd="print",
|
|
1000
|
+
# @percent=false,
|
|
1001
|
+
# @post_cmd=[],
|
|
1002
|
+
# @pre_cmd=[],
|
|
1003
|
+
# @put_cmd="print",
|
|
1004
|
+
# @trim_mode=nil>
|
|
1005
|
+
# template.set_eoutvar(compiler, '_foo') # => ["_foo"]
|
|
1006
|
+
# pp compiler
|
|
1007
|
+
# #<ERB::Compiler:0x000001cff8a9aa00
|
|
1008
|
+
# @insert_cmd="_foo.<<",
|
|
1009
|
+
# @percent=false,
|
|
1010
|
+
# @post_cmd=["_foo"],
|
|
1011
|
+
# @pre_cmd=["_foo = +''"],
|
|
1012
|
+
# @put_cmd="_foo.<<",
|
|
1013
|
+
# @trim_mode=nil>
|
|
1014
|
+
# ```
|
|
1015
|
+
#
|
|
1016
|
+
def set_eoutvar(compiler, eoutvar = '_erbout')
|
|
1017
|
+
compiler.put_cmd = "#{eoutvar}.<<"
|
|
1018
|
+
compiler.insert_cmd = "#{eoutvar}.<<"
|
|
1019
|
+
compiler.pre_cmd = ["#{eoutvar} = +''"]
|
|
1020
|
+
compiler.post_cmd = [eoutvar]
|
|
1021
|
+
end
|
|
1022
|
+
|
|
1023
|
+
# :markup: markdown
|
|
1024
|
+
#
|
|
1025
|
+
# :call-seq:
|
|
1026
|
+
# run(binding = new_toplevel) -> nil
|
|
1027
|
+
#
|
|
1028
|
+
# Like #result, but prints the result string (instead of returning it);
|
|
1029
|
+
# returns `nil`.
|
|
1030
|
+
def run(b=new_toplevel)
|
|
1031
|
+
print self.result(b)
|
|
1032
|
+
end
|
|
1033
|
+
|
|
1034
|
+
# :markup: markdown
|
|
1035
|
+
#
|
|
1036
|
+
# :call-seq:
|
|
1037
|
+
# result(binding = new_toplevel) -> new_string
|
|
1038
|
+
#
|
|
1039
|
+
# Returns the string result formed by processing \ERB tags found in the stored template in `self`.
|
|
1040
|
+
#
|
|
1041
|
+
# With no argument given, uses the default binding;
|
|
1042
|
+
# see [Default Binding][default binding].
|
|
1043
|
+
#
|
|
1044
|
+
# With argument `binding` given, uses the local binding;
|
|
1045
|
+
# see [Local Binding][local binding].
|
|
1046
|
+
#
|
|
1047
|
+
# See also #result_with_hash.
|
|
1048
|
+
#
|
|
1049
|
+
# [default binding]: rdoc-ref:ERB@Default+Binding
|
|
1050
|
+
# [local binding]: rdoc-ref:ERB@Local+Binding
|
|
1051
|
+
#
|
|
1052
|
+
def result(b=new_toplevel)
|
|
1053
|
+
unless @_init.equal?(self.class.singleton_class)
|
|
1054
|
+
raise ArgumentError, "not initialized"
|
|
1055
|
+
end
|
|
1056
|
+
eval(@src, b, (@filename || '(erb)'), @lineno)
|
|
1057
|
+
end
|
|
1058
|
+
|
|
1059
|
+
# :markup: markdown
|
|
1060
|
+
#
|
|
1061
|
+
# :call-seq:
|
|
1062
|
+
# result_with_hash(hash) -> new_string
|
|
1063
|
+
#
|
|
1064
|
+
# Returns the string result formed by processing \ERB tags found in the stored string in `self`;
|
|
1065
|
+
# see [Augmented Binding][augmented binding].
|
|
1066
|
+
#
|
|
1067
|
+
# See also #result.
|
|
1068
|
+
#
|
|
1069
|
+
# [augmented binding]: rdoc-ref:ERB@Augmented+Binding
|
|
1070
|
+
#
|
|
1071
|
+
def result_with_hash(hash)
|
|
1072
|
+
b = new_toplevel(hash.keys)
|
|
1073
|
+
hash.each_pair do |key, value|
|
|
1074
|
+
b.local_variable_set(key, value)
|
|
1075
|
+
end
|
|
1076
|
+
result(b)
|
|
1077
|
+
end
|
|
1078
|
+
|
|
1079
|
+
# :markup: markdown
|
|
1080
|
+
#
|
|
1081
|
+
# :call-seq:
|
|
1082
|
+
# new_toplevel(symbols) -> new_binding
|
|
1083
|
+
#
|
|
1084
|
+
# Returns a new binding based on `TOPLEVEL_BINDING`;
|
|
1085
|
+
# used to create a default binding for a call to #result.
|
|
1086
|
+
#
|
|
1087
|
+
# See [Default Binding][default binding].
|
|
1088
|
+
#
|
|
1089
|
+
# Argument `symbols` is an array of symbols;
|
|
1090
|
+
# each symbol `symbol` is defined as a new variable to hide and
|
|
1091
|
+
# prevent it from overwriting a variable of the same name already
|
|
1092
|
+
# defined within the binding.
|
|
1093
|
+
#
|
|
1094
|
+
# [default binding]: rdoc-ref:ERB@Default+Binding
|
|
1095
|
+
def new_toplevel(vars = nil)
|
|
1096
|
+
b = TOPLEVEL_BINDING
|
|
1097
|
+
if vars
|
|
1098
|
+
vars = vars.select {|v| b.local_variable_defined?(v)}
|
|
1099
|
+
unless vars.empty?
|
|
1100
|
+
return b.eval("tap {|;#{vars.join(',')}| break binding}")
|
|
1101
|
+
end
|
|
1102
|
+
end
|
|
1103
|
+
b.dup
|
|
1104
|
+
end
|
|
1105
|
+
private :new_toplevel
|
|
1106
|
+
|
|
1107
|
+
# :markup: markdown
|
|
1108
|
+
#
|
|
1109
|
+
# :call-seq:
|
|
1110
|
+
# def_method(module, method_signature, filename = '(ERB)') -> method_name
|
|
1111
|
+
#
|
|
1112
|
+
# Creates and returns a new instance method in the given module `module`;
|
|
1113
|
+
# returns the method name as a symbol.
|
|
1114
|
+
#
|
|
1115
|
+
# The method is created from the given `method_signature`,
|
|
1116
|
+
# which consists of the method name and its argument names (if any).
|
|
1117
|
+
#
|
|
1118
|
+
# The `filename` sets the value of #filename;
|
|
1119
|
+
# see [Error Reporting][error reporting].
|
|
1120
|
+
#
|
|
1121
|
+
# [error reporting]: rdoc-ref:ERB@Error+Reporting
|
|
1122
|
+
#
|
|
1123
|
+
# ```
|
|
1124
|
+
# template = '<%= arg1 %> <%= arg2 %>'
|
|
1125
|
+
# erb = ERB.new(template)
|
|
1126
|
+
# MyModule = Module.new
|
|
1127
|
+
# erb.def_method(MyModule, 'render(arg1, arg2)') # => :render
|
|
1128
|
+
# class MyClass; include MyModule; end
|
|
1129
|
+
# MyClass.new.render('foo', 123) # => "foo 123"
|
|
1130
|
+
# ```
|
|
1131
|
+
#
|
|
1132
|
+
def def_method(mod, methodname, fname='(ERB)')
|
|
1133
|
+
src = self.src.sub(/^(?!#|$)/) {"def #{methodname}\n"} << "\nend\n"
|
|
1134
|
+
mod.module_eval do
|
|
1135
|
+
eval(src, binding, fname, -1)
|
|
1136
|
+
end
|
|
1137
|
+
end
|
|
1138
|
+
|
|
1139
|
+
# :markup: markdown
|
|
1140
|
+
#
|
|
1141
|
+
# :call-seq:
|
|
1142
|
+
# def_module(method_name = 'erb') -> new_module
|
|
1143
|
+
#
|
|
1144
|
+
# Returns a new nameless module that has instance method `method_name`.
|
|
1145
|
+
#
|
|
1146
|
+
# ```
|
|
1147
|
+
# template = '<%= arg1 %> <%= arg2 %>'
|
|
1148
|
+
# erb = ERB.new(template)
|
|
1149
|
+
# MyModule = template.def_module('render(arg1, arg2)')
|
|
1150
|
+
# class MyClass
|
|
1151
|
+
# include MyModule
|
|
1152
|
+
# end
|
|
1153
|
+
# MyClass.new.render('foo', 123)
|
|
1154
|
+
# # => "foo 123"
|
|
1155
|
+
# ```
|
|
1156
|
+
#
|
|
1157
|
+
def def_module(methodname='erb')
|
|
1158
|
+
mod = Module.new
|
|
1159
|
+
def_method(mod, methodname, @filename || '(ERB)')
|
|
1160
|
+
mod
|
|
1161
|
+
end
|
|
1162
|
+
|
|
1163
|
+
# :markup: markdown
|
|
1164
|
+
#
|
|
1165
|
+
# :call-seq:
|
|
1166
|
+
# def_class(super_class = Object, method_name = 'result') -> new_class
|
|
1167
|
+
#
|
|
1168
|
+
# Returns a new nameless class whose superclass is `super_class`,
|
|
1169
|
+
# and which has instance method `method_name`.
|
|
1170
|
+
#
|
|
1171
|
+
# Create a template from HTML that has embedded expression tags that use `@arg1` and `@arg2`:
|
|
1172
|
+
#
|
|
1173
|
+
# ```
|
|
1174
|
+
# html = <<TEMPLATE
|
|
1175
|
+
# <html>
|
|
1176
|
+
# <body>
|
|
1177
|
+
# <p><%= @arg1 %></p>
|
|
1178
|
+
# <p><%= @arg2 %></p>
|
|
1179
|
+
# </body>
|
|
1180
|
+
# </html>
|
|
1181
|
+
# TEMPLATE
|
|
1182
|
+
# template = ERB.new(html)
|
|
1183
|
+
# ```
|
|
1184
|
+
#
|
|
1185
|
+
# Create a base class that has `@arg1` and `@arg2`:
|
|
1186
|
+
#
|
|
1187
|
+
# ```
|
|
1188
|
+
# class MyBaseClass
|
|
1189
|
+
# def initialize(arg1, arg2)
|
|
1190
|
+
# @arg1 = arg1
|
|
1191
|
+
# @arg2 = arg2
|
|
1192
|
+
# end
|
|
1193
|
+
# end
|
|
1194
|
+
# ```
|
|
1195
|
+
#
|
|
1196
|
+
# Use method #def_class to create a subclass that has method `:render`:
|
|
1197
|
+
#
|
|
1198
|
+
# ```
|
|
1199
|
+
# MySubClass = template.def_class(MyBaseClass, :render)
|
|
1200
|
+
# ```
|
|
1201
|
+
#
|
|
1202
|
+
# Generate the result:
|
|
1203
|
+
#
|
|
1204
|
+
# ```
|
|
1205
|
+
# puts MySubClass.new('foo', 123).render
|
|
1206
|
+
# <html>
|
|
1207
|
+
# <body>
|
|
1208
|
+
# <p>foo</p>
|
|
1209
|
+
# <p>123</p>
|
|
1210
|
+
# </body>
|
|
1211
|
+
# </html>
|
|
1212
|
+
# ```
|
|
1213
|
+
#
|
|
1214
|
+
#
|
|
1215
|
+
def def_class(superklass=Object, methodname='result')
|
|
1216
|
+
cls = Class.new(superklass)
|
|
1217
|
+
def_method(cls, methodname, @filename || '(ERB)')
|
|
1218
|
+
cls
|
|
1219
|
+
end
|
|
1220
|
+
end
|