Base class for template implementations. Subclasses must implement the prepare method and one of the evaluate or template_source methods.
Create a new template with the file, line, and options specified. By default, template data is read from the file. When a block is given, it should read template data and return as a String. When file is nil, a block is required.
All arguments are optional.
# File lib/sinatra/tilt.rb, line 96 def initialize(file=nil, line=1, options={}, &block) @file, @line, @options = nil, 1, {} [options, line, file].compact.each do |arg| case when arg.respond_to?(:to_str) ; @file = arg.to_str when arg.respond_to?(:to_int) ; @line = arg.to_int when arg.respond_to?(:to_hash) ; @options = arg.to_hash else raise TypeError end end raise ArgumentError, "file or block required" if (@file || block).nil? # call the initialize_engine method if this is the very first time # an instance of this class has been created. if !self.class.engine_initialized? initialize_engine self.class.engine_initialized = true end # used to generate unique method names for template compilation @stamp = (Time.now.to_f * 10000).to_i @compiled_method_names = {} # load template data and prepare @reader = block || lambda { |t| File.read(@file) } @data = @reader.call(self) prepare end
The basename of the template file.
# File lib/sinatra/tilt.rb, line 135 def basename(suffix='') File.basename(file, suffix) if file end
The filename used in backtraces to describe the template.
# File lib/sinatra/tilt.rb, line 145 def eval_file file || '(__TEMPLATE__)' end
The template file’s basename with all extensions chomped off.
# File lib/sinatra/tilt.rb, line 140 def name basename.split('.', 2).first if basename 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 130 def render(scope=Object.new, locals={}, &block) evaluate scope, locals || {}, &block end
The unique compiled method name for the locals keys provided.
# File lib/sinatra/tilt.rb, line 248 def compiled_method_name(locals_keys) @compiled_method_names[locals_keys] ||= generate_compiled_method_name(locals_keys) end
Process the template and return the result. When the scope mixes in the Tilt::CompileSite module, the template is compiled to a method and reused given identical locals keys. When the scope object does not mix in the CompileSite module, the template source is evaluated with instance_eval. In any case, template executation is guaranteed to be performed in the scope object with the locals specified and with support for yielding to the block.
# File lib/sinatra/tilt.rb, line 188 def evaluate(scope, locals, &block) if scope.respond_to?(:__tilt__) method_name = compiled_method_name(locals.keys) if scope.respond_to?(method_name) scope.send(method_name, locals, &block) else compile_template_method(method_name, locals) scope.send(method_name, locals, &block) end else evaluate_source(scope, locals, &block) end 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 153 def initialize_engine end
Generates all template source by combining the preamble, template, and postamble and returns a two-tuple of the form: [source, offset], where source is the string containing (Ruby) source code for the template and offset is the integer line offset where line reporting should begin.
Template subclasses may override this method when they need complete control over source generation or want to adjust the default line offset. In most cases, overriding the precompiled_template method is easier and more appropriate.
# File lib/sinatra/tilt.rb, line 211 def precompiled(locals) preamble = precompiled_preamble(locals) parts = [ preamble, precompiled_template(locals), precompiled_postamble(locals) ] [parts.join("\n"), preamble.count("\n") + 1] end
Generates postamble code for the precompiled template source. The string returned from this method is appended to the precompiled template source.
# File lib/sinatra/tilt.rb, line 243 def precompiled_postamble(locals) '' end
Generates preamble code for initializing template state, and performing locals assignment. The default implementation performs locals assignment only. Lines included in the preamble are subtracted from the source line offset, so adding code to the preamble does not effect line reporting in Kernel::caller and backtraces.
# File lib/sinatra/tilt.rb, line 236 def precompiled_preamble(locals) locals.map { |k,v| "#{k} = locals[:#{k}]" }.join("\n") end
A string containing the (Ruby) source code for the template. The default Template#evaluate implementation requires either this method or the precompiled method be overridden. When defined, the base Template guarantees correct file/line handling, locals support, custom scopes, and support for template compilation when the scope object allows it.
# File lib/sinatra/tilt.rb, line 227 def precompiled_template(locals) raise NotImplementedError end
Do whatever preparation is necessary to setup the underlying template engine. 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 171 def prepare if respond_to?(:compile!) # backward compat with tilt < 0.6; just in case warn 'Tilt::Template#compile! is deprecated; implement #prepare instead.' compile! else raise NotImplementedError end end
Like Kernel::require but issues a warning urging a manual require when running under a threaded environment.
# File lib/sinatra/tilt.rb, line 158 def require_template_library(name) if Thread.list.size > 1 warn "WARN: tilt autoloading '#{name}' in a non thread-safe way; " + "explicit require '#{name}' suggested." end require name end
Generated with the Darkfish Rdoc Generator 2.