lib/angelo/tilt/erb.rb in angelo-0.4.1 vs lib/angelo/tilt/erb.rb in angelo-0.5.0

- old
+ new

@@ -1,119 +1,34 @@ -require 'erb' -require 'tilt' +require 'tilt/erb' module Angelo module Tilt module ERB - DEFAULT_LAYOUT = 'layout.%s.erb' + DEFAULT_LAYOUT = 'layout.%s' DEFAULT_TYPE = :html LAYOUTS_DIR = 'layouts' ACCEPT_ALL = '*/*' - # hrm, sneaky - # - def self.included base - base.extend ClassMethods - end + def erb view, opts = {} + type = opts.delete(:type) || template_type + content_type type - module ClassMethods - - @reload_templates = false - - def view_glob *glob - File.join views_dir, *glob + if view.is_a? Symbol + view = :"#{view}.#{type}" end - def templatify *glob - Dir[view_glob *glob].reduce({}) do |h,v| - sym = v.gsub views_dir + File::SEPARATOR, '' - return h if (block_given? && yield(v)) - sym.gsub! File::SEPARATOR, UNDERSCORE - sym.gsub! /\.\w+?\.erb$/, EMPTY_STRING - sym.gsub! /^#{LAYOUTS_DIR}#{UNDERSCORE}/, EMPTY_STRING - h[sym.to_sym] = ::Tilt::ERBTemplate.new v - h - end - end - - def templates type = DEFAULT_TYPE - @templates ||= {} - if reload_templates? - @templates[type] = load_templates(type) + layout = + case opts[:layout] + when false + false + when Symbol + :"#{LAYOUTS_DIR}/#{layout}.#{type}" else - @templates[type] ||= load_templates(type) + :"#{DEFAULT_LAYOUT % type}" end - end - def load_templates type = DEFAULT_TYPE - templatify('**', "*.#{type}.erb") do |v| - v =~ /^#{LAYOUTS_DIR}#{File::SEPARATOR}/ - end - end - - def layout_templates type = DEFAULT_TYPE - if reload_templates? - @layout_templates = load_layout_templates(type) - else - @layout_templates ||= load_layout_templates(type) - end - end - - def load_layout_templates type = DEFAULT_TYPE - templatify LAYOUTS_DIR, "*.#{type}.erb" - end - - def default_layout type = DEFAULT_TYPE - @default_layout ||= {} - if reload_templates? - @default_layout[type] = load_default_layout(type) - else - @default_layout[type] ||= load_default_layout(type) - end - end - - def load_default_layout type = DEFAULT_TYPE - l = view_glob(DEFAULT_LAYOUT % type) - ::Tilt::ERBTemplate.new l if File.exist? l - end - - def reload_templates! on = true - @reload_templates = on - end - - def reload_templates? - @reload_templates - end - - end - - def erb view, opts = {locals: {}} - type = opts[:type] || template_type - content_type type - locals = Hash === opts[:locals] ? opts[:locals] : {} - render = case view - when String - ->{ view } - when Symbol - ->{self.class.templates(type)[view].render self, locals} - end - case opts[:layout] - when false - render[] - when Symbol - if lt = self.class.layout_templates(type)[opts[:layout]] - lt.render self, locals, &render - else - raise ArgumentError.new "unknown layout - :#{opts[:layout]}" - end - else - if self.class.default_layout(type) - self.class.default_layout(type).render self, locals, &render - else - render[] - end - end + _erb view, layout: layout, locals: opts[:locals] end def template_type accept = request.headers[ACCEPT_REQUEST_HEADER_KEY] mt = if accept.nil? or accept == ACCEPT_ALL