Ruby – The ERB templating system

The ERB templating system is a really versatile way for dynamically generating customised output from a template.

It’s similar to how php works when it generates web pages, but it is a lot more easier to embed ruby code in ERB files. It has some really powerful intelligence that lets you weave in and out of the template data where necessary.

ruby’s ERB template files, are files with the “.erb”. These files are basically text files with a bunch of ruby tags embeded in them. There are a few different types of these ruby tags:

Tag typeDescription
<% ruby code %>This executes the ruby code, but suppresses any string ouptut. Hence many used for processing. Note this has two optional variations,

"<%-" - This suppresses any leading whitespace
"- %>" - This suppresses any trailing whitespace/return-arriage.
<%= ruby code %>This procresses some ruby code and returns a string output.
<%# comment %>This is essentially a ruby comment. It is easy way to disable a ruby code tag for testing purposes, as well as for inserting comments into your erb files.
% single line of ruby codeThe previous tag types can span over multiple lines. However if you want to only embed a really short ruby code, then you can use this option for this.

The content that falls outside these tags can be pretty much any data you want, html, xml, plain text….etc.

You can write .erb files and then test them using the .erb command line utility.

For example, lets say we have the following erb file, called time.erb:

<%# here we display the current time. %>
Hello world. The time is now <%= Time.now %>. Have nice day.

Then from the command line, you do:

PS C:\Temp\ruby\erb> erb .\time.erb

Hello world. The time is now 2014-12-28 16:48:18 +0000. Have nice day.

Note: the leading blank line is caused by the fact that first line starts on the second line.

Now let’s see how to embed a loop into an erb file. Here’s a file called simpleloop.erb

Now lets try it:

This looks good. Erb noticed that there is a “for” tag and was smart enough to expect an “end” tag later on. When it did encounter the end tag, it managed to recongise that it is the end of the for-loop and every code tags inbetween should be treated as part of that loop.

We can use Powershell’s out-file command to capture the output into an html file:

PS C:\Temp\ruby\erb> erb .\simpleloop.erb | Out-File C:\Temp\simpleloop.html
PS C:\Temp\ruby\erb>

This will create a new html file, if we open this file we should see:

Since the “for” tag and and the “end” tag are single lines, then we could use the “%” syntax instead:

We can also re-write the above erb code in the form of a block, rather than an for-loop:

Notice this time, the start block iterator occupies a few lines, this is possible because the <%...%> tags allows for multi-line ruby code. Once again if you output it:

Then capture the output into a html file:

PS C:\Temp\ruby\erb> erb .\simpleblock.erb | Out-File C:\Temp\simpleblock.erb
PS C:\Temp\ruby\erb>

You’ll find that the output is the same again:

erb-output1