Module: Tilt

Defined in:
lib/sinatra/tilt.rb

Defined Under Namespace

Classes: BuilderTemplate, Cache, ERBTemplate, ErubisTemplate, HamlTemplate, LiquidTemplate, MustacheTemplate, RDiscountTemplate, RDocTemplate, RedClothTemplate, SassTemplate, StringTemplate, Template

Constant Summary collapse

VERSION =
'0.4'

Class Method Summary collapse

Class Method Details

.[](file) ⇒ Object

Lookup a template class given for the given filename or file extension. Return nil when no implementation is found.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sinatra/tilt.rb', line 30

def self.[](file)
  if @template_mappings.key?(pattern = file.to_s.downcase)
    @template_mappings[pattern]
  elsif @template_mappings.key?(pattern = File.basename(pattern))
    @template_mappings[pattern]
  else
    while !pattern.empty?
      if @template_mappings.key?(pattern)
        return @template_mappings[pattern]
      else
        pattern = pattern.sub(/^[^.]*\.?/, '')
      end
    end
    nil
  end
end

.mappingsObject

Hash of template path pattern => template implementation class mappings.



8
9
10
# File 'lib/sinatra/tilt.rb', line 8

def self.mappings
  @template_mappings
end

.new(file, line = nil, options = {}, &block) ⇒ Object

Create a new template for the given file using the file’s extension to determine the the template mapping.



20
21
22
23
24
25
26
# File 'lib/sinatra/tilt.rb', line 20

def self.new(file, line=nil, options={}, &block)
  if template_class = self[file]
    template_class.new(file, line, options, &block)
  else
    fail "No template engine registered for #{File.basename(file)}"
  end
end

.register(ext, template_class) ⇒ Object

Register a template implementation by file extension.



13
14
15
16
# File 'lib/sinatra/tilt.rb', line 13

def self.register(ext, template_class)
  ext = ext.to_s.sub(/^\./, '')
  mappings[ext.downcase] = template_class
end