class Tilt::BaseMapping

Private internal base class for both Mapping and FinalizedMapping, for the shared methods.

Public Instance Methods

[](file) click to toggle source

Looks up a template class based on file name and/or extension.

@example

mapping['views/hello.erb'] # => Tilt::ERBTemplate
mapping['hello.erb']       # => Tilt::ERBTemplate
mapping['erb']             # => Tilt::ERBTemplate

@return [template class]

   # File lib/tilt/mapping.rb
32 def [](file)
33   _, ext = split(file)
34   ext && lookup(ext)
35 end
Also aliased as: template_for
new(file, line=nil, options={}, &block) click to toggle source

Instantiates a new template class based on the file.

@raise [RuntimeError] if there is no template class registered for the

file name.

@example

mapping.new('index.mt') # => instance of MyEngine::Template

@see Tilt::Template.new

   # File lib/tilt/mapping.rb
16 def new(file, line=nil, options={}, &block)
17   if template_class = self[file]
18     template_class.new(file, line, options, &block)
19   else
20     fail "No template engine registered for #{File.basename(file)}"
21   end
22 end
template_for(file)
Alias for: []
templates_for(file) click to toggle source

Looks up a list of template classes based on file name. If the file name has multiple extensions, it will return all template classes matching the extensions from the end.

@example

mapping.templates_for('views/index.haml.erb')
# => [Tilt::ERBTemplate, Tilt::HamlTemplate]

@return [Array<template class>]

   # File lib/tilt/mapping.rb
48 def templates_for(file)
49   templates = []
50 
51   while true
52     prefix, ext = split(file)
53     break unless ext
54     templates << lookup(ext)
55     file = prefix
56   end
57 
58   templates
59 end

Private Instance Methods

split(file) click to toggle source
   # File lib/tilt/mapping.rb
63 def split(file)
64   pattern = file.to_s.downcase
65   full_pattern = pattern.dup
66 
67   until registered?(pattern)
68     return if pattern.empty?
69     pattern = File.basename(pattern)
70     pattern.sub!(/\A[^.]*\.?/, '')
71   end
72 
73   prefix_size = full_pattern.size - pattern.size
74   [full_pattern[0,prefix_size-1], pattern]
75 end