add layouts to renderer
The renderer is now able to render templates in the context of their layouts.
This commit is contained in:
parent
5a65a3dc40
commit
6c1cb50af0
|
@ -3,6 +3,25 @@ class Renderer
|
|||
DEFAULT_TYPE = :default_type
|
||||
|
||||
def self.call(session)
|
||||
template = do_checks(session)
|
||||
return nil unless template
|
||||
type = get_type(session, template)
|
||||
session.response.body = render(template, type, session.options)
|
||||
session.response.content_type = type_map[type]
|
||||
nil
|
||||
end
|
||||
|
||||
def self.render(template, type, object)
|
||||
layout = templates['layout']
|
||||
if layout && layout.has_key?(type)
|
||||
return layout[type].render(object) do
|
||||
template[type].render(object)
|
||||
end
|
||||
end
|
||||
template[type].render(object)
|
||||
end
|
||||
|
||||
def self.do_checks(session)
|
||||
unless session.options.has_key? :render
|
||||
session.response.body = ':render not set!'
|
||||
return nil
|
||||
|
@ -12,31 +31,27 @@ class Renderer
|
|||
session.response.body = 'Template does not exist!'
|
||||
return nil
|
||||
end
|
||||
template = templates[to_render]
|
||||
type = get_preferred_type(session, template)
|
||||
type = template.first[0] if type == DEFAULT_TYPE
|
||||
session.response.body = Tilt.new(template[type]).render(session.options)
|
||||
session.response.content_type = type_map[type]
|
||||
nil
|
||||
return templates[to_render]
|
||||
end
|
||||
|
||||
# initialize the template cache
|
||||
def self.find_templates(path)
|
||||
@@templates = {}
|
||||
templates = {}
|
||||
Dir[path + '/**/*'].each do |file|
|
||||
next unless File.file?(file)
|
||||
parts = file.gsub(path, '').match(COMPONENT_MATCHER)
|
||||
@@templates[parts[:template]] ||= {}
|
||||
@@templates[parts[:template]].merge!({parts[:type].to_sym => file})
|
||||
next unless File.file?(file) && parts
|
||||
templates[parts[:template]] ||= {}
|
||||
templates[parts[:template]].merge!({parts[:type].to_sym => Tilt.new(file)})
|
||||
end
|
||||
templates
|
||||
end
|
||||
|
||||
def self.templates
|
||||
find_templates 'templates' unless @@templates
|
||||
@@templates
|
||||
@@templates ||= find_templates 'templates'
|
||||
end
|
||||
|
||||
def self.set_types(types)
|
||||
@@type_map = types.invert
|
||||
@@types = types
|
||||
end
|
||||
|
||||
|
@ -50,11 +65,17 @@ class Renderer
|
|||
end
|
||||
|
||||
def self.type_map
|
||||
@type_map ||= types.invert
|
||||
@@type_map ||= types.invert
|
||||
end
|
||||
|
||||
def self.get_preferred_type(session, template)
|
||||
session.request.accept.types.each do |type|
|
||||
def self.get_type(session, template)
|
||||
type = get_preferred_type(session.request, template)
|
||||
return template.first[0] if type == DEFAULT_TYPE
|
||||
type
|
||||
end
|
||||
|
||||
def self.get_preferred_type(request, template)
|
||||
request.accept.types.each do |type|
|
||||
next unless types.has_key? type
|
||||
return types[type] if template.has_key? types[type]
|
||||
return DEFAULT_TYPE if types[type] == DEFAULT_TYPE
|
||||
|
|
Loading…
Reference in New Issue