0
0
Fork 0

added a small renderer

This is a renderer which currently just looks for fitting templates
based on the name and accept type. The accept type can be mapped in the
accept class to make it easier to work with.
This commit is contained in:
Gibheer 2012-11-08 21:32:59 +01:00
parent 24eba118e6
commit 84eaf7f763
1 changed files with 31 additions and 0 deletions

31
lib/zero/renderer.rb Normal file
View File

@ -0,0 +1,31 @@
module Zero
class FileNotFoundError < IOError; end
class Renderer
# set the path to the template base directory
def self.template_path(path)
@@path = path + '/'
end
# take the path and render the template within the context
def initialize(path, context, accept_types)
accept_types ||= Request::Accept.new('text/html')
@path = find_template(path, accept_types)
@context = context
end
# check if the template does exist
def find_template(template_path, types)
types.each do |type|
Dir[@@path + template_path + '.' + type + '.*'].each do |file|
return file
end
end
raise FileNotFoundError.new("Template '#{template_path}' not found!")
end
# render the template within the context
def render
Tilt.new(@path).render(@context)
end
end
end