Skip to content

Commit 692597b

Browse files
Enhancements (mostly documentation) (#85)
1 parent 1512314 commit 692597b

File tree

4 files changed

+496
-384
lines changed

4 files changed

+496
-384
lines changed

README.md

Lines changed: 64 additions & 221 deletions
Original file line numberDiff line numberDiff line change
@@ -1,255 +1,98 @@
1-
# ERB
1+
# \ERB (Embedded Ruby)
22

3-
An easy to use but powerful templating system for Ruby.
3+
\ERB is an easy-to-use, but also very powerful, [template processor][template processor].
44

5-
## Introduction
5+
\ERB is commonly used to produce:
66

7-
ERB provides an easy to use but powerful templating system for Ruby. Using
8-
ERB, actual Ruby code can be added to any plain text document for the
9-
purposes of generating document information details and/or flow control.
7+
- Customized or personalized email messages.
8+
- Customized or personalized web pages.
9+
- Software code (in code-generating applications).
1010

11-
A very simple example is this:
11+
Like method [sprintf][sprintf], \ERB can format run-time data into a string.
12+
\ERB, however, is *much more powerful*
1213

13-
```rb
14-
require 'erb'
14+
## How \ERB Works
1515

16-
x = 42
17-
template = ERB.new <<-EOF
18-
The value of x is: <%= x %>
19-
EOF
20-
puts template.result(binding)
21-
```
16+
Using \ERB, you can create a *template*: a plain-text string that has specially-formatted *tags*,
17+
then store it into an \ERB object;
18+
when \ERB produces _result_ string, it:
2219

23-
Prints: `The value of x is: 42`
20+
- Inserts run-time-evaluated expressions into the result.
21+
- Executes snippets of Ruby code.
22+
- Omits comments from the results.
2423

25-
More complex examples are given below.
24+
In the result:
2625

27-
## Recognized Tags
26+
- All non-tag text is passed through, _unchanged_.
27+
- Each tag is either _replaced_ (expression tag),
28+
or _omitted_ entirely (execution tag or comment tag).
2829

29-
ERB recognizes certain tags in the provided template and converts them based
30-
on the rules below:
30+
There are three types of tags:
3131

32-
```erb
33-
<% Ruby code -- inline with output %>
34-
<%= Ruby expression -- replace with result %>
35-
<%# comment -- ignored -- useful in testing %> (`<% #` doesn't work. Don't use Ruby comments.)
36-
% a line of Ruby code -- treated as <% line %> (optional -- see ERB.new)
37-
%% replaced with % if first thing on a line and % processing is used
38-
<%% or %%> -- replace with <% or %> respectively
39-
```
32+
| Tag | Form | Action | Text in Result |
33+
|----------------|:------------------------------------:|:-------------------------------------:|:--------------------:|
34+
| Expression tag | <tt>'<%= _ruby_expression_ %>'</tt> | Evaluates <tt>_ruby_expression_</tt>. | Value of expression. |
35+
| Execution tag | <tt>'<% _ruby_code_ %>'</tt> | Execute <tt>_ruby_code_</tt>. | None. |
36+
| Comment tag | <tt>'<%# _comment_text_ %>'</tt> | None. | None. |
4037

41-
All other text is passed through ERB filtering unchanged.
38+
These examples use `erb`, the \ERB command-line interface;
39+
each "echoes" a string template and pipes it to `erb` as input:
4240

43-
## Options
4441

45-
There are several settings you can change when you use ERB:
46-
* the nature of the tags that are recognized;
47-
* the binding used to resolve local variables in the template.
42+
- Expression tag:
4843

49-
See the ERB.new and ERB#result methods for more detail.
44+
$ echo "<%= $VERBOSE %>" | erb
45+
"false"
46+
$ echo "<%= 2 + 2 %>" | erb
47+
"4"
5048

51-
## Character encodings
49+
- Execution tag:
5250

53-
ERB (or Ruby code generated by ERB) returns a string in the same
54-
character encoding as the input string. When the input string has
55-
a magic comment, however, it returns a string in the encoding specified
56-
by the magic comment.
51+
echo "<% if $VERBOSE %> Long message. <% else %> Short message. <% end %>" | erb
52+
" Short message. "
5753

58-
```rb
59-
# -*- coding: utf-8 -*-
60-
require 'erb'
54+
- Comment tag:
6155

62-
template = ERB.new <<EOF
63-
<%#-*- coding: Big5 -*-%>
64-
__ENCODING__ is <%= __ENCODING__ %>.
65-
EOF
66-
puts template.result
67-
```
56+
echo "<%# TODO: Fix this nonsense. %> Nonsense." | erb
57+
" Nonsense."
6858

69-
Prints: `__ENCODING__ is Big5.`
59+
## How to Use \ERB
7060

71-
## Examples
61+
You can use \ERB either:
7262

73-
### Plain Text
63+
- In a program: see class ERB.
64+
- From the command line: see [ERB Executable][erb executable].
7465

75-
ERB is useful for any generic templating situation. Note that in this example, we use the
76-
convenient "% at start of line" tag, and we quote the template literally with
77-
`%q{...}` to avoid trouble with the backslash.
66+
## Installation
7867

79-
```rb
80-
require "erb"
68+
\ERB is installed with Ruby, and so there's no further installation needed.
8169

82-
# Create template.
83-
template = %q{
84-
From: James Edward Gray II <james@grayproductions.net>
85-
To: <%= to %>
86-
Subject: Addressing Needs
70+
## Other Template Engines
8771

88-
<%= to[/\w+/] %>:
89-
90-
Just wanted to send a quick note assuring that your needs are being
91-
addressed.
92-
93-
I want you to know that my team will keep working on the issues,
94-
especially:
95-
96-
<%# ignore numerous minor requests -- focus on priorities %>
97-
% priorities.each do |priority|
98-
* <%= priority %>
99-
% end
100-
101-
Thanks for your patience.
102-
103-
James Edward Gray II
104-
}.gsub(/^ /, '')
105-
106-
message = ERB.new(template, trim_mode: "%<>")
107-
108-
# Set up template data.
109-
to = "Community Spokesman <spokesman@ruby_community.org>"
110-
priorities = [ "Run Ruby Quiz",
111-
"Document Modules",
112-
"Answer Questions on Ruby Talk" ]
113-
114-
# Produce result.
115-
email = message.result
116-
puts email
117-
```
118-
119-
Generates:
120-
121-
```
122-
From: James Edward Gray II <james@grayproductions.net>
123-
To: Community Spokesman <spokesman@ruby_community.org>
124-
Subject: Addressing Needs
125-
126-
Community:
127-
128-
Just wanted to send a quick note assuring that your needs are being addressed.
129-
130-
I want you to know that my team will keep working on the issues, especially:
131-
132-
* Run Ruby Quiz
133-
* Document Modules
134-
* Answer Questions on Ruby Talk
135-
136-
Thanks for your patience.
137-
138-
James Edward Gray II
139-
```
140-
141-
### Ruby in HTML
142-
143-
ERB is often used in .rhtml files (HTML with embedded Ruby). Notice the need in
144-
this example to provide a special binding when the template is run, so that the instance
145-
variables in the Product object can be resolved.
146-
147-
```rb
148-
require "erb"
149-
150-
# Build template data class.
151-
class Product
152-
def initialize( code, name, desc, cost )
153-
@code = code
154-
@name = name
155-
@desc = desc
156-
@cost = cost
157-
158-
@features = [ ]
159-
end
160-
161-
def add_feature( feature )
162-
@features << feature
163-
end
164-
165-
# Support templating of member data.
166-
def get_binding
167-
binding
168-
end
169-
170-
# ...
171-
end
172-
173-
# Create template.
174-
template = %{
175-
<html>
176-
<head><title>Ruby Toys -- <%= @name %></title></head>
177-
<body>
178-
179-
<h1><%= @name %> (<%= @code %>)</h1>
180-
<p><%= @desc %></p>
181-
182-
<ul>
183-
<% @features.each do |f| %>
184-
<li><b><%= f %></b></li>
185-
<% end %>
186-
</ul>
187-
188-
<p>
189-
<% if @cost < 10 %>
190-
<b>Only <%= @cost %>!!!</b>
191-
<% else %>
192-
Call for a price, today!
193-
<% end %>
194-
</p>
195-
196-
</body>
197-
</html>
198-
}.gsub(/^ /, '')
199-
200-
rhtml = ERB.new(template)
72+
There are a variety of template engines available in various Ruby projects.
73+
For example, [RDoc][rdoc], distributed with Ruby, uses its own template engine, which
74+
can be reused elsewhere.
20175

202-
# Set up template data.
203-
toy = Product.new( "TZ-1002",
204-
"Rubysapien",
205-
"Geek's Best Friend! Responds to Ruby commands...",
206-
999.95 )
207-
toy.add_feature("Listens for verbal commands in the Ruby language!")
208-
toy.add_feature("Ignores Perl, Java, and all C variants.")
209-
toy.add_feature("Karate-Chop Action!!!")
210-
toy.add_feature("Matz signature on left leg.")
211-
toy.add_feature("Gem studded eyes... Rubies, of course!")
76+
Other popular template engines may be found in the [Ruby Toolbox][ruby toolbox].
21277

213-
# Produce result.
214-
rhtml.run(toy.get_binding)
215-
```
78+
## Code
21679

217-
Generates (some blank lines removed):
80+
The \ERB source code is in GitHub project [ruby/erb][ruby/erb]/
21881

219-
```html
220-
<html>
221-
<head><title>Ruby Toys -- Rubysapien</title></head>
222-
<body>
223-
224-
<h1>Rubysapien (TZ-1002)</h1>
225-
<p>Geek's Best Friend! Responds to Ruby commands...</p>
226-
227-
<ul>
228-
<li><b>Listens for verbal commands in the Ruby language!</b></li>
229-
<li><b>Ignores Perl, Java, and all C variants.</b></li>
230-
<li><b>Karate-Chop Action!!!</b></li>
231-
<li><b>Matz signature on left leg.</b></li>
232-
<li><b>Gem studded eyes... Rubies, of course!</b></li>
233-
</ul>
234-
235-
<p>
236-
Call for a price, today!
237-
</p>
238-
239-
</body>
240-
</html>
241-
```
242-
243-
## Notes
244-
245-
There are a variety of templating solutions available in various Ruby projects.
246-
For example, RDoc, distributed with Ruby, uses its own template engine, which
247-
can be reused elsewhere.
82+
## Bugs
24883

249-
Other popular engines could be found in the corresponding
250-
[Category](https://www.ruby-toolbox.com/categories/template_engines) of
251-
The Ruby Toolbox.
84+
Bugs may be reported at [ERB Issues][erb issues].
25285

25386
## License
25487

255-
The gem is available as open source under the terms of the [2-Clause BSD License](https://opensource.org/licenses/BSD-2-Clause).
88+
This software is available as open source under the terms
89+
of the [2-Clause BSD License][2-clause bsd license].
90+
91+
[2-clause bsd license]: https://opensource.org/licenses/BSD-2-Clause
92+
[erb executable]: rdoc-ref:erb_executable.md
93+
[erb issues]: https://github.com/ruby/erb/issues
94+
[rdoc]: https://ruby.github.io/rdoc/
95+
[ruby/erb]: https://github.com/ruby/erb
96+
[ruby toolbox]: https://www.ruby-toolbox.com/categories/template_engines
97+
[sprintf]: https://docs.ruby-lang.org/en/master/Kernel.html#method-i-sprintf
98+
[template processor]: https://en.wikipedia.org/wiki/Template_processor_

0 commit comments

Comments
 (0)