Class: TemplateInheritance::Template
- Inherits:
-
Object
- Object
- TemplateInheritance::Template
- Defined in:
- lib/template-inheritance.rb,
lib/template-inheritance/adapters/padrino.rb
Instance Attribute Summary collapse
- #blocks ⇒ Object
-
#context ⇒ Object
template -> supertemplate is the same relationship as class -> superclass.
-
#padrino_views_directory ⇒ Object
Returns the value of attribute padrino_views_directory.
-
#path ⇒ Object
template -> supertemplate is the same relationship as class -> superclass.
-
#scope ⇒ Object
template -> supertemplate is the same relationship as class -> superclass.
-
#supertemplate ⇒ Object
template -> supertemplate is the same relationship as class -> superclass.
Class Method Summary collapse
Instance Method Summary collapse
- #adapter ⇒ Object
-
#extension ⇒ Object
haml, erb …
- #fullpath ⇒ Object
-
#initialize(path, scope = Object.new) ⇒ Template
constructor
A new instance of Template.
- #instantiate_supertemplate ⇒ Object
- #render(context = Hash.new) ⇒ Object
- #template(options = Hash.new) ⇒ Object
Constructor Details
#initialize(path, scope = Object.new) ⇒ Template
Returns a new instance of Template.
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/template-inheritance.rb', line 56 def initialize(path, scope = Object.new) self.path = path#[scope.class.template_prefix.chomp("/"), template].join("/") self.scope = scope self.scope.extend(TemplateHelpers) # this enables template caching unless TemplateInheritance.development? self.scope.extend(Tilt::CompileSite) end self.scope.template = self end |
Instance Attribute Details
#blocks ⇒ Object
51 52 53 |
# File 'lib/template-inheritance.rb', line 51 def blocks @blocks ||= Hash.new end |
#context ⇒ Object
template -> supertemplate is the same relationship as class -> superclass
47 48 49 |
# File 'lib/template-inheritance.rb', line 47 def context @context end |
#padrino_views_directory ⇒ Object
Returns the value of attribute padrino_views_directory.
9 10 11 |
# File 'lib/template-inheritance/adapters/padrino.rb', line 9 def padrino_views_directory @padrino_views_directory end |
#path ⇒ Object
template -> supertemplate is the same relationship as class -> superclass
47 48 49 |
# File 'lib/template-inheritance.rb', line 47 def path @path end |
#scope ⇒ Object
template -> supertemplate is the same relationship as class -> superclass
47 48 49 |
# File 'lib/template-inheritance.rb', line 47 def scope @scope end |
#supertemplate ⇒ Object
template -> supertemplate is the same relationship as class -> superclass
47 48 49 |
# File 'lib/template-inheritance.rb', line 47 def supertemplate @supertemplate end |
Class Method Details
.paths ⇒ Object
41 42 43 |
# File 'lib/template-inheritance.rb', line 41 def self.paths @@paths ||= Array.new end |
Instance Method Details
#adapter ⇒ Object
78 79 80 |
# File 'lib/template-inheritance.rb', line 78 def adapter snake_case(self.template.class.name.split("::").last).sub("_template", "") end |
#extension ⇒ Object
haml, erb …
82 83 84 |
# File 'lib/template-inheritance.rb', line 82 def extension # haml, erb ... File.extname(path)[1..-1] end |
#fullpath ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/template-inheritance.rb', line 68 def fullpath @fullpath ||= begin if self.path.match(/^(\/|\.)/) # /foo or ./foo find_file(self.path, "#{self.path}.*") else self.find_in_paths end end end |
#instantiate_supertemplate ⇒ Object
112 113 114 115 116 |
# File 'lib/template-inheritance.rb', line 112 def instantiate_supertemplate supertemplate = self.class.new(self.supertemplate, self.scope) supertemplate.blocks = self.blocks supertemplate end |
#render(context = Hash.new) ⇒ Object
91 92 93 94 95 96 97 98 |
# File 'lib/template-inheritance.rb', line 91 def render(context = Hash.new) raise TemplateNotFound.new(self.path) if self.fullpath.nil? TemplateInheritance.logger.info("Rendering template #{self.path} with context keys #{context.keys.inspect}") self.scope.context = self.context = context # so we can access context in the scope object as well value = self.template.render(self.scope, context) TemplateInheritance.logger.debug("Available blocks: #{self.blocks.keys.inspect}") self.supertemplate ? render_supertemplate : value end |
#template(options = Hash.new) ⇒ Object
86 87 88 |
# File 'lib/template-inheritance.rb', line 86 def template( = Hash.new) @template ||= Tilt.new(self.fullpath, nil, ) end |