The Terminal Programmer

Defining methods for ERB templates 1

Suraj N. Kurapati

While converting ruby-vpi documentation from DocBook-XML to RedCloth, I wanted an easy way to pass a bunch of text to my own methods, in a fashion similar to if-statements and iterators. This would allow me to make admonitions easily by writing code like the following in my ERB template.

<% caution do %>
Nothing is as it seems!
<% end %>

At first glance, it seems that the content between the do and end keywords is just passed as a string inside the block argument to the method. However, in truth, the block argument contains an expression that appends the content between do and end to the _erbout variable, like this:

_erbout << "Nothing is as it seems!"

ERB allows us to specify an alternate name for _erbout (its evaluation buffer) in its constructor. We can use this ability to capture the content passed from an ERB template to a Ruby method as follows.

TODO: finish this article!