一个易于使用但功能强大的模板系统,适用于 Ruby
ERB is an easy-to-use, but also very powerful, template processor.
ERB is commonly used to produce:
Like method sprintf, ERB can format run-time data into a string.
ERB, however, is much more powerful.
Using ERB, you can create a template: a plain-text string that has specially-formatted tags, then store it into an ERB object; when ERB produces the result string, it:
In the result:
There are three types of tags:
| Tag | Form | Action | Text in Result |
|---|---|---|---|
| Expression tag | `` | Evaluates ruby_expression. |
Value of expression. |
| Execution tag | `` | Executes ruby_code. |
None. |
| Comment tag | `` | None. | None. |
These examples use erb, the ERB command-line interface;
each "echoes" a string template and pipes it to erb as input:
$ echo "" | erb
false
$ echo "" | erb
4$ echo " Long message. Short message. " | erb
Short message.$ echo " Nonsense." | erb
Nonsense.You can use ERB either:
ERB is installed with Ruby, and so there's no further installation needed.
There are a variety of template engines available in various Ruby projects. For example, RDoc, distributed with Ruby, uses its own template engine, which can be reused elsewhere.
Other popular template engines may be found in the Ruby Toolbox.
The ERB source code is in GitHub project ruby/erb.
Bugfixes may be filed at ERB Pull Requests.
This software is available as open source under the terms of the 2-Clause BSD License.
暂无开放 Issues,或尚未同步最近议题。