diff options
author | Gibheer <gibheer@gmail.com> | 2012-11-08 21:32:59 +0100 |
---|---|---|
committer | Gibheer <gibheer@gmail.com> | 2012-11-08 21:36:56 +0100 |
commit | 84eaf7f763d4dcbf0b9cbdefe9f860df9043d2f4 (patch) | |
tree | 6e3f84c53c8d0c3ef966f1c703ef149e62598a14 | |
parent | 24eba118e6a5cdef01a6f73a32dcfc7f57184ac1 (diff) |
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.
-rw-r--r-- | lib/zero/renderer.rb | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/zero/renderer.rb b/lib/zero/renderer.rb new file mode 100644 index 0000000..bdf1334 --- /dev/null +++ b/lib/zero/renderer.rb @@ -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 |