Class | Tilt::Template |
In: |
lib/tilt/template.rb
|
Parent: | Object |
Base class for template implementations. Subclasses must implement the prepare method and one of the evaluate or precompiled_template methods.
engine_initialized | -> | engine_initialized? |
data | [R] | Template source; loaded from a file or given directly. |
default_mime_type | [RW] | |
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. 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/tilt/template.rb, line 38 38: def initialize(file=nil, line=1, options={}, &block) 39: @file, @line, @options = nil, 1, {} 40: 41: [options, line, file].compact.each do |arg| 42: case 43: when arg.respond_to?(:to_str) ; @file = arg.to_str 44: when arg.respond_to?(:path) ; @file = arg.path 45: when arg.respond_to?(:to_int) ; @line = arg.to_int 46: when arg.respond_to?(:to_hash) ; @options = arg.to_hash.dup 47: else raise TypeError 48: end 49: end 50: 51: raise ArgumentError, "file or block required" if (@file || block).nil? 52: 53: # call the initialize_engine method if this is the very first time 54: # an instance of this class has been created. 55: if !self.class.engine_initialized? 56: initialize_engine 57: self.class.engine_initialized = true 58: end 59: 60: # used to hold compiled template methods 61: @compiled_method = {} 62: 63: # used on 1.9 to set the encoding if it is not set elsewhere (like a magic comment) 64: # currently only used if template compiles to ruby 65: @default_encoding = @options.delete :default_encoding 66: 67: # load template data and prepare (uses binread to avoid encoding issues) 68: @reader = block || lambda { |t| File.respond_to?(:binread) ? File.binread(@file) : File.read(@file) } 69: @data = @reader.call(self) 70: prepare 71: end
Whether or not this template engine allows executing Ruby script within the template. If this is false, scope and locals will generally not be used, nor will the provided block be avaiable via yield. This should be overridden by template subclasses.
# File lib/tilt/template.rb, line 100 100: def allows_script? 101: true 102: end
The filename used in backtraces to describe the template.
# File lib/tilt/template.rb, line 91 91: def eval_file 92: file || '(__TEMPLATE__)' 93: 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/tilt/template.rb, line 76 76: def render(scope=Object.new, locals={}, &block) 77: evaluate scope, locals || {}, &block 78: end
The compiled method for the locals keys provided.
# File lib/tilt/template.rb, line 206 206: def compiled_method(locals_keys) 207: @compiled_method[locals_keys] ||= 208: compile_template_method(locals_keys) 209: end
Execute the compiled template and return the result string. Template evaluation is guaranteed to be performed in the scope object with the locals specified and with support for yielding to the block.
This method is only used by source generating templates. Subclasses that override render() may not support all features.
# File lib/tilt/template.rb, line 142 142: def evaluate(scope, locals, &block) 143: method = compiled_method(locals.keys) 144: method.bind(scope).call(locals, &block) 145: 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/tilt/template.rb, line 108 108: def initialize_engine 109: 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/tilt/template.rb, line 156 156: def precompiled(locals) 157: preamble = precompiled_preamble(locals) 158: template = precompiled_template(locals) 159: magic_comment = extract_magic_comment(template) 160: if magic_comment 161: # Magic comment e.g. "# coding: utf-8" has to be in the first line. 162: # So we copy the magic comment to the first line. 163: preamble = magic_comment + "\n" + preamble 164: end 165: parts = [ 166: preamble, 167: template, 168: precompiled_postamble(locals) 169: ] 170: [parts.join("\n"), preamble.count("\n") + 1] 171: end
Generates postamble code for the precompiled template source. The string returned from this method is appended to the precompiled template source.
# File lib/tilt/template.rb, line 201 201: def precompiled_postamble(locals) 202: '' 203: 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/tilt/template.rb, line 188 188: def precompiled_preamble(locals) 189: locals.map do |k,v| 190: if k.to_s =~ /\A[a-z_][a-zA-Z_0-9]*\z/ 191: "#{k} = locals[#{k.inspect}]" 192: else 193: raise "invalid locals key: #{k.inspect} (keys must be variable names)" 194: end 195: end.join("\n") 196: 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/tilt/template.rb, line 179 179: def precompiled_template(locals) 180: raise NotImplementedError 181: 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/tilt/template.rb, line 126 126: def prepare 127: if respond_to?(:compile!) 128: # backward compat with tilt < 0.6; just in case 129: warn 'Tilt::Template#compile! is deprecated; implement #prepare instead.' 130: compile! 131: else 132: raise NotImplementedError 133: end 134: end
Like Kernel#require but issues a warning urging a manual require when running under a threaded environment.
# File lib/tilt/template.rb, line 113 113: def require_template_library(name) 114: if Thread.list.size > 1 115: warn "WARN: tilt autoloading '#{name}' in a non thread-safe way; " + 116: "explicit require '#{name}' suggested." 117: end 118: require name 119: end