Class: Rango::Templates::Template

  • Object
    • Rango::Templates::Template

Attributes

Instance Attributes

context [RW] public

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

locals [RW] public

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

supertemplate [RW] public

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

template [RW] public

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

Constructor Summary

public initialize(template, context, locals = Hash.new)

Meta Tags

Parameters:

Since:

0.0.2

[View source]


20
21
22
23
24
# File 'lib/rango/templates/template.rb', line 20

def initialize(template, context, locals = Hash.new)
  self.template = template#[context.class.template_prefix.chomp("/"), template].join("/")
  self.context  = context
  self.locals   = locals
end

Public Visibility

Public Instance Method Summary

#engine
#extend_context(context)
#find(template)
#render

Public Instance Methods Inherited from Object

define_instance_method, not_nil?, try

Public Instance Method Details

engine

public engine

Meta Tags

Parameters:

Raises:

[Error406]

Since:

0.0.2

[View source]


56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rango/templates/template.rb', line 56

def engine
  # TODO: test if Project.settings.template_engine nil => useful message
  # TODO: maybe more template engines?
  engine_name = Project.settings.template_engine
  Rango.import("templates/adapters/#{engine_name}.rb")
  engine_class = Rango::Templates.engine(engine_name)
  engine_class.new
rescue LoadError
  Rango.logger.fatal("Template engine #{engine_name} can't be loaded.")
  raise Error406.new(self.params)
end

extend_context

public extend_context(context)

Meta Tags

Parameters:

Since:

0.0.2

[View source]


46
47
48
49
50
51
52
53
# File 'lib/rango/templates/template.rb', line 46

def extend_context(context)
  class << context
    include TemplateHelpers
    attr_accessor :_template
  end
  context._template = self
  return context
end

find

public find(template)

Meta Tags

Parameters:

Since:

0.0.2

[View source]


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rango/templates/template.rb', line 69

def find(template)
  if template.match(/^\//)
    if File.exist?(template)
      return template
    elsif template = Dir["#{template}.*"].first
      return template
    end
  else
    Project.settings.template_dirs.each do |directory|
      path = File.join(directory, template)
      if File.exist?(path)
        return path
      elsif first = Dir["#{path}.*"].first
        return first
      end
    end
    return nil
  end
end

render

public render

Meta Tags

Parameters:

Raises:

[TemplateNotFound]

Since:

0.0.2

[View source]


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rango/templates/template.rb', line 27

def render
  self.context = extend_context(self.context) unless self.partial
  path = self.find(self.template)
  raise TemplateNotFound.new(template, Project.settings.template_dirs) if path.nil?
  file = File.new(path)
  value = self.engine.render(file, context, self.locals)
  STDOUT.puts
  Rango.logger.info("Rendering template #{self.template}")
  # Rango.logger.inspect(self.blocks)
  if self.supertemplate
    Rango.logger.debug("Extends call: #{self.supertemplate}")
    supertemplate = self.class.new(self.supertemplate, self.context, self.locals)
    supertemplate.blocks = self.blocks
    return supertemplate.render
  end
  return value
end