Skip to content

Instantly share code, notes, and snippets.

@jbarnette
Created December 7, 2011 23:07
Show Gist options
  • Save jbarnette/1445203 to your computer and use it in GitHub Desktop.
Save jbarnette/1445203 to your computer and use it in GitHub Desktop.
# 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