Class: Tilt::Template
- Inherits:
-
Object
- Object
- Tilt::Template
- Defined in:
- lib/sinatra/tilt.rb
Overview
Base class for template implementations. Subclasses must implement the #compile! method and one of the #evaluate or #template_source methods.
Direct Known Subclasses
BuilderTemplate, ERBTemplate, HamlTemplate, LiquidTemplate, MustacheTemplate, RDiscountTemplate, RDocTemplate, RedClothTemplate, SassTemplate, StringTemplate
Class Attribute Summary collapse
-
.engine_initialized ⇒ Object
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.
-
#compile ⇒ Object
Load template source and compile the template.
-
#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.
-
#initialize_engine ⇒ Object
Called once and only once for each template subclass the first time the template class is initialized.
-
#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 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.
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/sinatra/tilt.rb', line 73 def initialize(file=nil, line=1, ={}, &block) raise ArgumentError, "file or block required" if file.nil? && block.nil? , line = line, 1 if line.is_a?(Hash) @file = file @line = line || 1 @options = || {} @reader = block || lambda { |t| File.read(file) } if !self.class.engine_initialized initialize_engine self.class.engine_initialized = true end end |
Class Attribute Details
.engine_initialized ⇒ Object
Returns the value of attribute engine_initialized.
93 94 95 |
# File 'lib/sinatra/tilt.rb', line 93 def engine_initialized @engine_initialized end |
Instance Attribute Details
#data ⇒ Object (readonly)
Template source; loaded from a file or given directly.
53 54 55 |
# File 'lib/sinatra/tilt.rb', line 53 def data @data end |
#file ⇒ Object (readonly)
The name of the file where the template data was loaded from.
56 57 58 |
# File 'lib/sinatra/tilt.rb', line 56 def file @file end |
#line ⇒ Object (readonly)
The line number in #file where template data was loaded from.
59 60 61 |
# File 'lib/sinatra/tilt.rb', line 59 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.
64 65 66 |
# File 'lib/sinatra/tilt.rb', line 64 def @options end |
Instance Method Details
#basename(suffix = '') ⇒ Object
The basename of the template file.
115 116 117 |
# File 'lib/sinatra/tilt.rb', line 115 def basename(suffix='') File.basename(file, suffix) if file end |
#compile ⇒ Object
Load template source and compile the template. The template is loaded and compiled the first time this method is called; subsequent calls are no-ops.
99 100 101 102 103 104 |
# File 'lib/sinatra/tilt.rb', line 99 def compile if @data.nil? @data = @reader.call(self) compile! end end |
#eval_file ⇒ Object
The filename used in backtraces to describe the template.
125 126 127 |
# File 'lib/sinatra/tilt.rb', line 125 def eval_file file || '(__TEMPLATE__)' end |
#initialize_engine ⇒ Object
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.
90 91 |
# File 'lib/sinatra/tilt.rb', line 90 def initialize_engine end |
#name ⇒ Object
The template file’s basename with all extensions chomped off.
120 121 122 |
# File 'lib/sinatra/tilt.rb', line 120 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
.
109 110 111 112 |
# File 'lib/sinatra/tilt.rb', line 109 def render(scope=Object.new, locals={}, &block) compile evaluate scope, locals || {}, &block end |