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>