From 84eaf7f763d4dcbf0b9cbdefe9f860df9043d2f4 Mon Sep 17 00:00:00 2001 From: Gibheer Date: Thu, 8 Nov 2012 21:32:59 +0100 Subject: 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. --- lib/zero/renderer.rb | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 lib/zero/renderer.rb 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 -- cgit v1.2.3-70-g09d2