Sha256: 877a5f442c3e0d1b980be63cf7b32d0429922d02e7427bce3239c0a2f5f3a60b
Contents?: true
Size: 1.95 KB
Versions: 1
Compression:
Stored size: 1.95 KB
Contents
require 'deas/logger' require 'deas/template_engine' module Deas class TemplateSource DISALLOWED_ENGINE_EXTS = [ 'rb' ] DisallowedEngineExtError = Class.new(ArgumentError) attr_reader :path, :engines def initialize(path, logger = nil) @path = path.to_s @default_opts = { 'source_path' => @path, 'logger' => logger || Deas::NullLogger.new, 'deas_template_source' => self } @engines = Hash.new{ |h,k| Deas::NullTemplateEngine.new(@default_opts) } end def engine(input_ext, engine_class, registered_opts = nil) if DISALLOWED_ENGINE_EXTS.include?(input_ext) raise DisallowedEngineExtError, "`#{input_ext}` is disallowed as an"\ " engine extension." end engine_opts = @default_opts.merge(registered_opts || {}) @engines[input_ext.to_s] = engine_class.new(engine_opts) end def engine_for?(template_name) @engines.keys.include?(get_template_ext(template_name)) end def render(template_name, view_handler, locals, &content) [ view_handler.class.layouts, template_name ].flatten.reverse.inject(content) do |render_proc, name| proc{ get_engine(name).render(name, view_handler, locals, &render_proc) } end.call end def partial(template_name, locals, &content) get_engine(template_name).partial(template_name, locals, &content) end private def get_engine(template_name) @engines[get_template_ext(template_name)] end def get_template_ext(template_name) files = Dir.glob("#{File.join(@path, template_name.to_s)}.*") files = files.reject{ |p| !@engines.keys.include?(parse_ext(p)) } parse_ext(files.first.to_s || '') end def parse_ext(template_name) File.extname(template_name)[1..-1] end end class NullTemplateSource < TemplateSource def initialize super('') end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
deas-0.29.0 | lib/deas/template_source.rb |