eoutvar can be used to set the name of the variable ERB will build up its output in. This is useful when you need to run multiple ERB templates through the same binding and/or when you want to control where output ends up. Pass the name of the variable to be used inside a String.

Image

ERB is often used in .rhtml files (HTML with embedded Ruby). Notice the need in this example to provide a special binding when the template is run, so that the instance variables in the Product object can be resolved.

ERB provides an easy to use but powerful templating system for Ruby. Using ERB, actual Ruby code can be added to any plain text document for the purposes of generating document information details and/or flow control.

Sign up for inside scoop on promotions, discounts, special events, training opportunities, and company news! Learn more about Email Subscription by visiting our FAQs page.

ERB (or Ruby code generated by ERB) returns a string in the same character encoding as the input string. When the input string has a magic comment, however, it returns a string in the encoding specified by the magic comment.

Executes the generated ERB code to produce a completed template, returning the results of that code. (See ERB::new for details on how this process can be affected by safe_level.)

If trim_mode is passed a String containing one or more of the following modifiers, ERB will adjust its code generation as listed:

Image

Can be used to set eoutvar as described in ERB::new. It's probably easier to just use the constructor though, since calling this method requires the setup of an ERB compiler object.

There are a variety of templating solutions available in various Ruby projects. For example, RDoc, distributed with Ruby, uses its own template engine, which can be reused elsewhere.

Sets optional filename and line number that will be used in ERB code evaluation and error reporting. See also filename= and lineno=

ERB is useful for any generic templating situation. Note that in this example, we use the convenient “% at start of line” tag, and we quote the template literally with %q{...} to avoid trouble with the backslash.

Image