Class: TemplateInheritance::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/template-inheritance.rb,
lib/template-inheritance/adapters/padrino.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, scope = Object.new) ⇒ Template

Returns a new instance of Template.

Since:

  • 0.0.2



55
56
57
58
59
60
61
62
63
64
# File 'lib/template-inheritance.rb', line 55

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

#blocksObject



50
51
52
# File 'lib/template-inheritance.rb', line 50

def blocks
  @blocks ||= Hash.new
end

#contextObject

template -> supertemplate is the same relationship as class -> superclass

Since:

  • 0.0.2



46
47
48
# File 'lib/template-inheritance.rb', line 46

def context
  @context
end

#padrino_views_directoryObject

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

#pathObject

template -> supertemplate is the same relationship as class -> superclass

Since:

  • 0.0.2



46
47
48
# File 'lib/template-inheritance.rb', line 46

def path
  @path
end

#scopeObject

template -> supertemplate is the same relationship as class -> superclass

Since:

  • 0.0.2



46
47
48
# File 'lib/template-inheritance.rb', line 46

def scope
  @scope
end

#supertemplateObject

template -> supertemplate is the same relationship as class -> superclass

Since:

  • 0.0.2



46
47
48
# File 'lib/template-inheritance.rb', line 46

def supertemplate
  @supertemplate
end

Class Method Details

.pathsObject



40
41
42
# File 'lib/template-inheritance.rb', line 40

def self.paths
  @@paths ||= Array.new
end

Instance Method Details

#adapterObject



77
78
79
# File 'lib/template-inheritance.rb', line 77

def adapter
  snake_case(self.template.class.name.split("::").last).sub("_template", "")
end

#extensionObject

haml, erb …



81
82
83
# File 'lib/template-inheritance.rb', line 81

def extension # haml, erb ...
  File.extname(path)[1..-1]
end

#fullpathObject

Since:

  • 0.0.2



67
68
69
70
71
72
73
74
75
# File 'lib/template-inheritance.rb', line 67

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

#render(context = Hash.new) ⇒ Object

Raises:

Since:

  • 0.0.2



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/template-inheritance.rb', line 90

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}")
  if self.supertemplate
    TemplateInheritance.logger.debug("Extends call: #{self.supertemplate}")
    supertemplate = self.class.new(self.supertemplate, self.scope)
    supertemplate.blocks = self.blocks
    return supertemplate.render(context)
  end
  value
end

#template(options = Hash.new) ⇒ Object



85
86
87
# File 'lib/template-inheritance.rb', line 85

def template(options = Hash.new)
  @template ||= Tilt.new(self.fullpath, nil, options)
end