Templating with Ruby

A brief look at ERB

Created by Colin A Gross /

Purpose

“I want to write documents that have some variable parts.”

“I do not want to have lots of strings in my code.”

Web

Creating an html table for each item in an inventory:

<table>
<% @inventory.each do |item| %>
  <tr>
    <td><%= item.label %></td>
    <td><%= item.quantity %></td>
    <td><%= link_to "Details", item %></td>
    <td><%= link_to "Add", add_item_path(item) %></td>
    <td><%= link_to "Remove", item, method: :delete %></td>
  </tr>
<% end %>
</table>

DevOps

A template for ntp config:

driftfile /var/lib/ntp/drift
<% @boxes.each do |box| -%>
server <%= box.name -%><%= @ntp_server_suffix %>
<% end -%>

restrict -4 default kod notrap nomodify nopeer noquery
restrict -6 default kod notrap nomodify nopeer noquery
restrict 127.0.0.1

renders:

driftfile /var/lib/ntp/drift
server foo.ntp.cluster.org
server bar.ntp.cluster.org
server qux.ntp.cluster.org

restrict -4 default kod notrap nomodify nopeer noquery
restrict -6 default kod notrap nomodify nopeer noquery
restrict 127.0.0.1

Message Merge

Dear <%= @employee.name %>,

You need to update your <%= report.type %> reports.
Come on in on  <%= Date::DAYNAMES[Date.today.next_day.wday] %> to finish those up.

Dear Peter Gibbons,

You need to update your TPS reports. Come on in on Saturday to finish those up.

Expressions

<%= ruby %>

<%= "Ruby result "+ " is rendered" %>

<% ruby %>

<% "Ruby result" + " is NOT rendered"  %>

Flow Control

Combining printing and non-printing expressions.

<% if @brigade.is_the_best? %>
  <%= "The Best!" %>
<% else %>
  <%= "Not the best, yet.." %>
<% end %>

Binding

  • variables
  • methods
  • value of self
Kernel::binding

Using Binding to Render Foo

Create the template

  require 'erubi'

  template_content = ("<%= 'A demo ' + foo %>")
  template = Erubi::Engine.new(template_content)

Assign variables and save context

  foo = "bar"
  context = Kernel::binding

Render the template

  rendered = eval( template.src, context )
  puts rendered

Result

A demo bar

ERB, Erubis, Erubi

Template content and variable assignment

  require 'erb'
  require 'erubis'
  require 'erubi'

  template_content = ("<%= 'Engine Demo ' + blep %>\n")

  blep = "boop"
  context = Kernel::binding

Render using different engines.

  erb_t    = ERB.new(template_content)
  erubis_t = Erubis::Eruby.new(template_content)
  erubi_t  = Erubi::Engine.new(template_content)

  erb_rend    = erb_t.result(context)
  erubis_rend = erubis_t.result(context )
  erubi_rend  = eval( erubi_t.src, context )

  puts erb_rend, erubis_rend, erubi_rend

Results

Engine Demo boop
Engine Demo boop
Engine Demo boop

XML Example

<rdf:RDF xmlns:bd="http://example.org/bad/#">
<rdf:Description rdf:about="http://example.org/bad/inputMessage">
  <bd:noofparams> <%=parameters.count%> </ot:noofparams>"%>
  <bd:params>
  <% if parameters.count > 0 %>
    <rdf:Seq>
    <% parameters.each do |param| %>
    <rdf:li> "<%= param['name'] %>" </rdf:li>
    <% end %>
    </rdf:Seq>
  <% end %>
  </ot:params>
</rdf:Description> </rdf:RDF>