33

I'm writing a command-line tool that will ultimately output an HTML report. The tool is written in Ruby. (I am not using Rails). I'm trying to keep the logic of the application in one set of files, and the HTML templates (the .erb files) in another set.

I'm having a really annoying problem though: I can't successfully include one .erb file into another.

To be specific, I'm trying to do something like this (in pseudo-code):

<html>
<head>
  <style type='text/css'>
    [include a stylesheet here]
    [and another one here]
  </style>
</head>
<body>
  <p>The rest of my document follows...

That example snippet is itself an erb file, which is being invoked from within the application logic.

I'm doing things this way so I can keep my stylesheets out of the main template to make it easier/cleaner to maintain the application. The end product (the report), though, needs to be a single, stand-alone HTML file which has no dependencies, and thus, I want to inline those stylesheets into the document head when the report is generated.

This seems like this should be easy, but I've been banging my head against a wall (and Googling, and RTMF'ing) for the last hour, and I'm not having any luck at all.

How is this supposed to be done? Thanks.

Chris Allen Lane
  • 6,334
  • 5
  • 25
  • 31

4 Answers4

41

ERB templates can be nested by evaluating the sub-template from within <%= %> of the main template.

<%= ERB.new(sub_template_content).result(binding) %>

For example:

require "erb"

class Page
  def initialize title, color
    @title = title
    @color = color
  end

  def render path
    content = File.read(File.expand_path(path))
    t = ERB.new(content)
    t.result(binding)
  end
end

page = Page.new("Home", "#CCCCCC")
puts page.render("home.html.erb")

home.html.erb:

<title><%= @title %></title>
<head>
  <style type="text/css">
<%= render "home.css.erb" %>
  </style>
</head>

home.css.erb:

body {
  background-color: <%= @color %>;
}

produces:

<title>Home</title>
<head>
  <style type="text/css">
body {
  background-color: #CCCCCC;
}
  </style>
</head>
cydparser
  • 2,057
  • 1
  • 16
  • 12
  • I'm not working in Rails, so I don't *think* that 'render' stuff applies to me (could be wrong), but that first line worked like a champ. Many thanks! – Chris Allen Lane Apr 20 '12 at 20:04
  • 5
    The render method was defined in the above example using pure std ruby, so it can be used in a rails-free environment. Cheers. – cydparser Apr 20 '12 at 21:03
  • Ha, wow. Don't know how I didn't notice that. Thanks for pointing that out, and thanks for the solution. It worked perfectly. – Chris Allen Lane Apr 22 '12 at 17:15
  • 2
    Related: if you are just including static content you should be able to do something like `<%= File.read("common.txt") %>`. – austinheiman Mar 02 '16 at 12:04
  • 1
    The part of the parent template above the call to the sub template would dissapear until I used a fix in @mighq's answer below (and Jack's comment) – Julien Lamarche Feb 09 '19 at 21:04
18

I'm needing this in a Sinatra app, and I find that I can just call it the same way I called the original:

In the sinatra app, I call the index:

erb :index

Then, in the index template, I can do the same for any sub-template:

<div id="controls">
  <%= erb :controls %>

..which shows the 'controls.erb' template.

Jon Carter
  • 2,836
  • 1
  • 20
  • 26
10
<%= ERB.new(sub_template_content).result(binding) %>

does not work, when you are using erb cli utility, multiple _erbout variables are overriden and only last one is used.

use it like this:

<%= ERB.new(sub_template_content, eoutvar: '_sub01').result(binding) %>
noraj
  • 3,964
  • 1
  • 30
  • 38
mighq
  • 1,004
  • 1
  • 13
  • 14
  • 6
    I needed to do `ERB.new(sub_template_context, nil, nil, '_sub01').result(binding)` when using test kitchen. It must use an older version of ruby or something. – Jacklynn Mar 18 '15 at 21:16
  • I think I was using Ruby 1.8 when adding this answer – mighq Mar 20 '15 at 19:30
9

From within my .erb file, I had to do this:

<%= ERB.new(File.read('pathToFile/myFile.erb'), nil, nil, '_sub01').result(binding) %>

The other answers in this thread assumed you had a variable with your content in it. This version retrieves the content.

Joseph Larson
  • 8,530
  • 1
  • 19
  • 36