Skip to content

Instantly share code, notes, and snippets.

@blueheadpublishing
Created December 6, 2011 19:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save blueheadpublishing/1439435 to your computer and use it in GitHub Desktop.
Save blueheadpublishing/1439435 to your computer and use it in GitHub Desktop.
ERB Subtemplating Script: Import ERB From Within ERB
# This script will take the file book.html.erb and parse it. When it encounters
# <%= import('your_file.html.erb') %> it will parse the file and stream
# it into the parent erb file.
require "erb"
class ErbImportSubtemplate
def self.import(file)
# Parse the source erb file
ERB.new(File.read(file)).result(binding).gsub(/\n$/,'')
end
# this is where you should specify your master .erb file
erb = import('book.html.erb')
# write the contents to an HTML file
File.open("book.html", 'a') do |f|
f << erb
end
end
# book.html.erb
#
# <html><body>
# <p>This is a test</p>
# <%= import("chapter.html.erb") %>
# </body></html>
# chapter.html.erb
#
# <p>Chapter One</p>
# script will output html
# book.html
#
# <html><body>
# <p>This is a test</p>
# <p>Chapter One</p>
# </body></html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment