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.

Methods

External Aliases

engine_initialized -> engine_initialized?

Attributes

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.

Public Class 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.

[Source]

    # 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?(:to_int)  ; @line = arg.to_int
45:         when arg.respond_to?(:to_hash) ; @options = arg.to_hash.dup
46:         when arg.respond_to?(:path)    ; @file = arg.path
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| read_template_file }
69:       @data = @reader.call(self)
70: 
71:       if @data.respond_to?(:force_encoding)
72:         @data.force_encoding(default_encoding) if default_encoding
73: 
74:         if !@data.valid_encoding?
75:           raise Encoding::InvalidByteSequenceError, "#{eval_file} is not valid #{@data.encoding}"
76:         end
77:       end
78: 
79:       prepare
80:     end

Public Instance methods

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.

[Source]

     # File lib/tilt/template.rb, line 126
126:     def allows_script?
127:       true
128:     end

The basename of the template file.

[Source]

     # File lib/tilt/template.rb, line 107
107:     def basename(suffix='')
108:       File.basename(file, suffix) if file
109:     end

The encoding of the source data. Defaults to the default_encoding-option if present. You may override this method in your template class if you have a better hint of the data‘s encoding.

[Source]

    # File lib/tilt/template.rb, line 86
86:     def default_encoding
87:       @default_encoding
88:     end

The filename used in backtraces to describe the template.

[Source]

     # File lib/tilt/template.rb, line 117
117:     def eval_file
118:       file || '(__TEMPLATE__)'
119:     end

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

[Source]

     # File lib/tilt/template.rb, line 112
112:     def name
113:       basename.split('.', 2).first if basename
114:     end

[Source]

    # File lib/tilt/template.rb, line 90
90:     def read_template_file
91:       data = File.open(file, 'rb') { |io| io.read }
92:       if data.respond_to?(:force_encoding)
93:         # Set it to the default external (without verifying)
94:         data.force_encoding(Encoding.default_external) if Encoding.default_external
95:       end
96:       data
97:     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/tilt/template.rb, line 102
102:     def render(scope=Object.new, locals={}, &block)
103:       evaluate scope, locals || {}, &block
104:     end

Protected Instance methods

The compiled method for the locals keys provided.

[Source]

     # File lib/tilt/template.rb, line 238
238:     def compiled_method(locals_keys)
239:       @compiled_method[locals_keys] ||=
240:         compile_template_method(locals_keys)
241:     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.

[Source]

     # File lib/tilt/template.rb, line 168
168:     def evaluate(scope, locals, &block)
169:       method = compiled_method(locals.keys)
170:       method.bind(scope).call(locals, &block)
171:     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/tilt/template.rb, line 134
134:     def initialize_engine
135:     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.

[Source]

     # File lib/tilt/template.rb, line 182
182:     def precompiled(locals)
183:       preamble = precompiled_preamble(locals)
184:       template = precompiled_template(locals)
185:       postamble = precompiled_postamble(locals)
186:       source = ''
187: 
188:       # Ensure that our generated source code has the same encoding as the
189:       # the source code generated by the template engine.
190:       if source.respond_to?(:force_encoding)
191:         template_encoding = extract_encoding(template)
192: 
193:         source.force_encoding(template_encoding)
194:         template.force_encoding(template_encoding)
195:       end
196: 
197:       # https://github.com/rtomayko/tilt/issues/193
198:       warn "precompiled_preamble should return String (not Array)" if preamble.is_a?(Array)
199:       warn "precompiled_postamble should return String (not Array)" if postamble.is_a?(Array)
200:       source << [preamble, template, postamble].join("\n")
201: 
202:       [source, preamble.count("\n")+1]
203:     end

Generates postamble code for the precompiled template source. The string returned from this method is appended to the precompiled template source.

[Source]

     # File lib/tilt/template.rb, line 233
233:     def precompiled_postamble(locals)
234:       ''
235:     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.

[Source]

     # File lib/tilt/template.rb, line 220
220:     def precompiled_preamble(locals)
221:       locals.map do |k,v|
222:         if k.to_s =~ /\A[a-z_][a-zA-Z_0-9]*\z/
223:           "#{k} = locals[#{k.inspect}]"
224:         else
225:           raise "invalid locals key: #{k.inspect} (keys must be variable names)"
226:         end
227:       end.join("\n")
228:     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, proper encoding, and support for template compilation.

[Source]

     # File lib/tilt/template.rb, line 211
211:     def precompiled_template(locals)
212:       raise NotImplementedError
213:     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.

[Source]

     # File lib/tilt/template.rb, line 152
152:     def prepare
153:       if respond_to?(:compile!)
154:         # backward compat with tilt < 0.6; just in case
155:         warn 'Tilt::Template#compile! is deprecated; implement #prepare instead.'
156:         compile!
157:       else
158:         raise NotImplementedError
159:       end
160:     end

Like Kernel#require but issues a warning urging a manual require when running under a threaded environment.

[Source]

     # File lib/tilt/template.rb, line 139
139:     def require_template_library(name)
140:       if Thread.list.size > 1
141:         warn "WARN: tilt autoloading '#{name}' in a non thread-safe way; " +
142:              "explicit require '#{name}' suggested."
143:       end
144:       require name
145:     end

[Validate]