Created
December 7, 2011 23:07
-
-
Save jbarnette/1445203 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Render a template file using Tilt. Templates can | |
# have multiple file extensions, which will be rendered one at a | |
# time. For example, "foo.md.erb" will first be run through ERB, | |
# then through Markdown. | |
def self.render template, context = nil, options = {} | |
context ||= Object.new | |
options.merge! default_encoding: Encoding::UTF_8 | |
file = Dir["app/templates/#{template}.*"].first | |
unless file and File.file? file | |
raise "Can't find [#{template}] template." | |
end | |
input = File.read file | |
steps = file.split(".").reverse | |
output = steps.inject(input) do |input, step| | |
renderer = Tilt[step] | |
renderer ? renderer.new(file) { input }.render(context, options) : input | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment