Created
December 6, 2011 19:00
-
-
Save blueheadpublishing/1439435 to your computer and use it in GitHub Desktop.
ERB Subtemplating Script: Import ERB From Within ERB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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