ERB (Embedded 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

How ERB Works

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 result string, it:

In the result:

There are three types of tags:

Tag Form Action Text in Result
Expression tag '<%= <em>ruby_expression</em> %>' Evaluates <em>ruby_expression</em>. Value of expression.
Execution tag '<% <em>ruby_code</em> %>' Execute <em>ruby_code</em>. None.
Comment tag '<%# <em>comment_text</em> %>' None. None.

These examples use erb, the ERB command-line interface; each “echoes” a string template and pipes it to erb as input:

How to Use ERB

You can use ERB either:

Installation

ERB is installed with Ruby, and so there’s no further installation needed.

Other Template Engines

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.

Code

The ERB source code is in GitHub project ruby/erb.

Bugs

Bugs may be reported at ERB Issues.

License

This software is available as open source under the terms of the 2-Clause BSD License.