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



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

#blocksObject



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

def blocks
  @blocks ||= Hash.new
end

#contextObject

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

Since:

  • 0.0.2



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

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



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

def path
  @path
end

#scopeObject

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

Since:

  • 0.0.2



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

def scope
  @scope
end

#supertemplateObject

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

Since:

  • 0.0.2



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

def supertemplate
  @supertemplate
end

Class Method Details

.pathsObject



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

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

Instance Method Details

#adapterObject



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

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

#extensionObject

haml, erb …



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

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

#fullpathObject

Since:

  • 0.0.2



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_supertemplateObject



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

Raises:

Since:

  • 0.0.2



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(options = Hash.new)
  @template ||= Tilt.new(self.fullpath, nil, options)
end