| Class | Tilt::Template |
| In: |
lib/sinatra/tilt.rb
|
| Parent: | Object |
Base class for template implementations. Subclasses must implement the compile! method and one of the evaluate or template_source methods.
| 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. |
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.
# 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
The filename used in backtraces to describe the template.
# 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.
# File lib/sinatra/tilt.rb, line 90
90: def initialize_engine
91: 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.
# File lib/sinatra/tilt.rb, line 109
109: def render(scope=Object.new, locals={}, &block)
110: compile
111: evaluate scope, locals || {}, &block
112: end
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.
# 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.
# 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.
# File lib/sinatra/tilt.rb, line 150
150: def template_source
151: raise NotImplementedError
152: end
# 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