Class: Sinatra::JstPages::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/sinatra/jstpages.rb

Direct Known Subclasses

EcoEngine, HamlCoffeeEngine, HamlEngine, JadeEngine

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#fileObject (readonly)

### file [attribute] The string path of the template file.



183
184
185
# File 'lib/sinatra/jstpages.rb', line 183

def file
  @file
end

#nameObject (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

#contentsObject

### 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