Class Tilt::Template
In: lib/sinatra/tilt.rb
Parent: Object
Template BuilderTemplate RDiscountTemplate LiquidTemplate RDocTemplate MustacheTemplate StringTemplate RedClothTemplate HamlTemplate ERBTemplate SassTemplate ErubisTemplate Cache lib/sinatra/tilt.rb Tilt dot/m_2_0.png

Base class for template implementations. Subclasses must implement the compile! method and one of the evaluate or template_source methods.

Methods

Attributes

data  [R]  Template source; loaded from a file or given directly.
engine_initialized  [RW] 
file  [R]  The name of the file where the template data was loaded from.
line  [R]  The line number in file where template data was loaded from.
options  [R]  A Hash of template engine specific options. This is passed directly to the underlying engine and is not used by the generic template interface.

Public Class methods

Create a new template with the file, line, and options specified. By default, template data is read from the file specified. When a block is given, it should read template data and return as a String. When file is nil, a block is required.

The initialize_engine method is called if this is the very first time this template subclass has been initialized.

[Source]

    # File lib/sinatra/tilt.rb, line 73
73:     def initialize(file=nil, line=1, options={}, &block)
74:       raise ArgumentError, "file or block required" if file.nil? && block.nil?
75:       options, line = line, 1 if line.is_a?(Hash)
76:       @file = file
77:       @line = line || 1
78:       @options = options || {}
79:       @reader = block || lambda { |t| File.read(file) }
80: 
81:       if !self.class.engine_initialized
82:         initialize_engine
83:         self.class.engine_initialized = true
84:       end
85:     end

Public Instance methods

The basename of the template file.

[Source]

     # File lib/sinatra/tilt.rb, line 115
115:     def basename(suffix='')
116:       File.basename(file, suffix) if file
117:     end

Load template source and compile the template. The template is loaded and compiled the first time this method is called; subsequent calls are no-ops.

[Source]

     # File lib/sinatra/tilt.rb, line 99
 99:     def compile
100:       if @data.nil?
101:         @data = @reader.call(self)
102:         compile!
103:       end
104:     end

The filename used in backtraces to describe the template.

[Source]

     # File lib/sinatra/tilt.rb, line 125
125:     def eval_file
126:       file || '(__TEMPLATE__)'
127:     end

Called once and only once for each template subclass the first time the template class is initialized. This should be used to require the underlying template library and perform any initial setup.

[Source]

    # File lib/sinatra/tilt.rb, line 90
90:     def initialize_engine
91:     end

The template file‘s basename with all extensions chomped off.

[Source]

     # File lib/sinatra/tilt.rb, line 120
120:     def name
121:       basename.split('.', 2).first if basename
122:     end

Render the template in the given scope with the locals specified. If a block is given, it is typically available within the template via yield.

[Source]

     # File lib/sinatra/tilt.rb, line 109
109:     def render(scope=Object.new, locals={}, &block)
110:       compile
111:       evaluate scope, locals || {}, &block
112:     end

Protected Instance methods

Do whatever preparation is necessary to "compile" the template. Called immediately after template data is loaded. Instance variables set in this method are available when evaluate is called.

Subclasses must provide an implementation of this method.

[Source]

     # File lib/sinatra/tilt.rb, line 135
135:     def compile!
136:       raise NotImplementedError
137:     end

Process the template and return the result. Subclasses should override this method unless they implement the template_source.

[Source]

     # File lib/sinatra/tilt.rb, line 141
141:     def evaluate(scope, locals, &block)
142:       source, offset = local_assignment_code(locals)
143:       source = [source, template_source].join("\n")
144:       scope.instance_eval source, eval_file, line - offset
145:     end

Return a string containing the (Ruby) source code for the template. The default Template#evaluate implementation requires this method be defined.

[Source]

     # File lib/sinatra/tilt.rb, line 150
150:     def template_source
151:       raise NotImplementedError
152:     end

Private Instance methods

[Source]

     # File lib/sinatra/tilt.rb, line 155
155:     def local_assignment_code(locals)
156:       return ['', 1] if locals.empty?
157:       source = locals.collect { |k,v| "#{k} = locals[:#{k}]" }
158:       [source.join("\n"), source.length]
159:     end

[Source]

     # File lib/sinatra/tilt.rb, line 161
161:     def require_template_library(name)
162:       if Thread.list.size > 1
163:         warn "WARN: tilt autoloading '#{name}' in a non thread-safe way; " +
164:              "explicit require '#{name}' suggested."
165:       end
166:       require name
167:     end

[Validate]