Class: Tilt::Template
- Inherits:
-
Object
- Object
- Tilt::Template
- Defined in:
- lib/frank/tilt.rb
Overview
Base class for template implementations. Subclasses must implement the #prepare method and one of the #evaluate or #template_source methods.
Direct Known Subclasses
BuilderTemplate, CoffeeTemplate, ERBTemplate, HamlTemplate, LessTemplate, LiquidTemplate, MustacheTemplate, RDiscountTemplate, RDocTemplate, RadiusTemplate, RedClothTemplate, SassTemplate, StringTemplate
Class Attribute Summary collapse
-
.engine_initialized ⇒ Object
(also: engine_initialized?)
Returns the value of attribute engine_initialized.
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Template source; loaded from a file or given directly.
-
#file ⇒ Object
readonly
The name of the file where the template data was loaded from.
-
#line ⇒ Object
readonly
The line number in #file where template data was loaded from.
-
#options ⇒ Object
readonly
A Hash of template engine specific options.
Instance Method Summary collapse
-
#basename(suffix = '') ⇒ Object
The basename of the template file.
-
#eval_file ⇒ Object
The filename used in backtraces to describe the template.
-
#initialize(file = nil, line = 1, options = {}, &block) ⇒ Template
constructor
Create a new template with the file, line, and options specified.
-
#name ⇒ Object
The template file’s basename with all extensions chomped off.
-
#render(scope = Object.new, locals = {}, &block) ⇒ Object
Render the template in the given scope with the locals specified.
Constructor Details
#initialize(file = nil, line = 1, options = {}, &block) ⇒ Template
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.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/frank/tilt.rb', line 93 def initialize(file=nil, line=1, ={}, &block) @file, @line, @options = nil, 1, {} [, 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.dup 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 if @file.match(/^[^\n]+$/) && File.exist?(@file) @reader = block || lambda { |t| File.read(@file) } else @reader = block || lambda { |t| @file } end @data = @reader.call(self) prepare end |
Class Attribute Details
.engine_initialized ⇒ Object Also known as: engine_initialized?
Returns the value of attribute engine_initialized.
83 84 85 |
# File 'lib/frank/tilt.rb', line 83 def engine_initialized @engine_initialized end |
Instance Attribute Details
#data ⇒ Object (readonly)
Template source; loaded from a file or given directly.
66 67 68 |
# File 'lib/frank/tilt.rb', line 66 def data @data end |
#file ⇒ Object (readonly)
The name of the file where the template data was loaded from.
69 70 71 |
# File 'lib/frank/tilt.rb', line 69 def file @file end |
#line ⇒ Object (readonly)
The line number in #file where template data was loaded from.
72 73 74 |
# File 'lib/frank/tilt.rb', line 72 def line @line end |
#options ⇒ Object (readonly)
A Hash of template engine specific options. This is passed directly to the underlying engine and is not used by the generic template interface.
77 78 79 |
# File 'lib/frank/tilt.rb', line 77 def @options end |
Instance Method Details
#basename(suffix = '') ⇒ Object
The basename of the template file.
137 138 139 |
# File 'lib/frank/tilt.rb', line 137 def basename(suffix='') File.basename(file, suffix) if file end |
#eval_file ⇒ Object
The filename used in backtraces to describe the template.
147 148 149 |
# File 'lib/frank/tilt.rb', line 147 def eval_file file || '(__TEMPLATE__)' end |
#name ⇒ Object
The template file’s basename with all extensions chomped off.
142 143 144 |
# File 'lib/frank/tilt.rb', line 142 def name basename.split('.', 2).first if basename end |
#render(scope = Object.new, locals = {}, &block) ⇒ Object
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
.
132 133 134 |
# File 'lib/frank/tilt.rb', line 132 def render(scope=Object.new, locals={}, &block) evaluate scope, locals || {}, &block end |