"Include file" in ERB templates

4,160 views
Skip to first unread message

Thomas Bellman

unread,
Jun 24, 2008, 8:34:22 AM6/24/08
to puppet...@googlegroups.com
I have a few ERB templates that have some common parts, and I
want to break out those parts into a separate file that I can
just "include" in the main templates. If this included file is
plain text, I can do

<%= File.read("common.txt") %>

This works as expected. However, I want to be able to use ERB
templating inside that common file also, and now I'm having
problems. After reading the documentation for ERB (starting at
http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/index.html), and
looking at the code in Puppet that invokes ERB, I came to the
following:

<%= ERB.new(File.read("common.erb"), 0, "-").result(binding) %>

This only works partly. It correctly reads the common.erb file
and expands the ERB code in it. Problem is, anything in my main
templates before calling ERB.new(...) is discarded. I.e, if I
have

foo
<%= ERB.new(File.read("common.erb"), 0, "-").result(binding) %>
bar

in my template, then the line "foo" just disappears. That's not
quite what I wanted...

What am I doing wrong, and how should I have done this?


/Thomas Bellman

Michal Zubáč

unread,
Dec 31, 2013, 7:15:37 AM12/31/13
to puppet...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages