Class: Deas::TemplateSource
- Inherits:
-
Object
- Object
- Deas::TemplateSource
- Defined in:
- lib/deas/template_source.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#engines ⇒ Object
readonly
Returns the value of attribute engines.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #engine(input_ext, engine_class, registered_opts = nil) ⇒ Object
- #engine_for?(ext) ⇒ Boolean
-
#initialize(path, logger = nil) ⇒ TemplateSource
constructor
A new instance of TemplateSource.
- #partial(template_name, locals, &content) ⇒ Object
- #render(template_name, view_handler, locals, &content) ⇒ Object
Constructor Details
#initialize(path, logger = nil) ⇒ TemplateSource
Returns a new instance of TemplateSource.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/deas/template_source.rb', line 10 def initialize(path, logger = nil) @path = path.to_s @default_engine_opts = { 'source_path' => @path, 'logger' => logger || Deas::NullLogger.new, 'default_template_source' => self } @engines = Hash.new do |hash, ext| # cache null template exts so we don't repeatedly call this block for # known null template exts hash[ext.to_s] = Deas::NullTemplateEngine.new(@default_engine_opts) end @engine_exts = [] @ext_lists = Hash.new do |hash, template_name| # An ext list is an array of non-template-name extensions that have engines # configured. The first ext in the list is the most precedent. Its engine # is used to do the initial render from the named template file. Any # further exts are used to compile rendered content from upsteam engines. hash[template_name] = parse_ext_list(template_name) end end |
Instance Attribute Details
#engines ⇒ Object (readonly)
Returns the value of attribute engines.
8 9 10 |
# File 'lib/deas/template_source.rb', line 8 def engines @engines end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
8 9 10 |
# File 'lib/deas/template_source.rb', line 8 def path @path end |
Instance Method Details
#engine(input_ext, engine_class, registered_opts = nil) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/deas/template_source.rb', line 32 def engine(input_ext, engine_class, registered_opts = nil) @engine_exts << input_ext.to_s engine_opts = @default_engine_opts.merge(registered_opts || {}) engine_opts['ext'] = input_ext.to_s @engines[input_ext.to_s] = engine_class.new(engine_opts) end |
#engine_for?(ext) ⇒ Boolean
40 41 42 |
# File 'lib/deas/template_source.rb', line 40 def engine_for?(ext) @engine_exts.include?(ext.to_s) end |
#partial(template_name, locals, &content) ⇒ Object
56 57 58 59 60 |
# File 'lib/deas/template_source.rb', line 56 def partial(template_name, locals, &content) compile(template_name) do |engine| engine.partial(template_name, locals, &content) end end |
#render(template_name, view_handler, locals, &content) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/deas/template_source.rb', line 44 def render(template_name, view_handler, locals, &content) [ view_handler.layouts, template_name ].flatten.reverse.inject(content) do |render_proc, name| proc do compile(name) do |engine| engine.render(name, view_handler, locals, &render_proc) end end end.call end |