Class: Sinatra::JstPages::Engine
- Inherits:
-
Object
- Object
- Sinatra::JstPages::Engine
- Defined in:
- lib/sinatra/jstpages.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
### file [attribute] The string path of the template file.
-
#name ⇒ Object
readonly
### name [attribute] The name of the template.
Instance Method Summary collapse
-
#compile! ⇒ Object
### function [method] The JavaScript function to invoke on the precompile’d object.
-
#contents ⇒ Object
### contents [method] Returns the contents of the template file as a string.
-
#initialize(name, file) ⇒ Engine
constructor
A new instance of Engine.
- #wrap_jst(name) ⇒ Object
Constructor Details
#initialize(name, file) ⇒ Engine
Returns a new instance of Engine.
185 186 187 188 |
# File 'lib/sinatra/jstpages.rb', line 185 def initialize(name, file) @name = name @file = file end |
Instance Attribute Details
#file ⇒ Object (readonly)
### file [attribute] The string path of the template file.
183 184 185 |
# File 'lib/sinatra/jstpages.rb', line 183 def file @file end |
#name ⇒ Object (readonly)
### name [attribute] The name of the template.
179 180 181 |
# File 'lib/sinatra/jstpages.rb', line 179 def name @name end |
Instance Method Details
#compile! ⇒ Object
### function [method] The JavaScript function to invoke on the precompile’d object.
What this returns should, in JavaScript, return a function that can be called with an object hash of the params to be passed onto the template.
213 214 215 216 217 |
# File 'lib/sinatra/jstpages.rb', line 213 def compile! wrap_jst(name) do "_.template(#{contents.inspect})" end end |
#contents ⇒ Object
### contents [method] Returns the contents of the template file as a string.
192 193 194 |
# File 'lib/sinatra/jstpages.rb', line 192 def contents File.read(@file) end |
#wrap_jst(name) ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/sinatra/jstpages.rb', line 196 def wrap_jst(name) compiled_contents = contents compiled_contents = yield if block_given? <<JS.strip.gsub(/^ {12}/, '') JST[#{name.inspect}] = function() { if (!c[#{name.inspect}]) c[#{name.inspect}] = (#{compiled_contents}); return c[#{name.inspect}].apply(this, arguments); }; JS end |