class Tilt::BaseMapping

  1. lib/tilt/mapping.rb
Superclass: Object

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

Methods

Public Instance

  1. []
  2. new
  3. templates_for

Public Instance Aliases

template_for -> []

Public Instance methods

[](file)

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]

[show source]
   # File lib/tilt/mapping.rb
32 def [](file)
33   _, ext = split(file)
34   ext && lookup(ext)
35 end
new(file, line=nil, options={}, &block)

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

[show source]
   # 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
templates_for(file)

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>]

[show source]
   # 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