Class: SmtRails::Tilt

Inherits:
Tilt::Template
  • Object
show all
Defined in:
lib/smt_rails/tilt.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#namespaceObject (readonly)

Returns the value of attribute namespace.



11
12
13
# File 'lib/smt_rails/tilt.rb', line 11

def namespace
  @namespace
end

Instance Method Details

#evaluate(scope, locals, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/smt_rails/tilt.rb', line 13

def evaluate(scope, locals, &block)
  template_key = path_to_key scope
  <<-MustacheTemplate
  (function() {
  #{namespace} || (#{namespace} = {});
  #{namespace}Cache || (#{namespace}Cache = {});
  #{namespace}Cache[#{template_key.inspect}] = Mustache.compile(#{data.inspect});
  Mustache.compilePartial(#{template_key.inspect}, #{data.inspect});

  #{namespace}[#{template_key.inspect}] = function(object) {
if (!object){ object = {}; }
return #{SmtRails.template_namespace}Cache[#{template_key.inspect}](object);
  };
  }).call(this);
  MustacheTemplate
end

#path_to_key(scope) ⇒ Object



30
31
32
33
34
# File 'lib/smt_rails/tilt.rb', line 30

def path_to_key(scope)
  path = scope.logical_path.to_s.split('/')
  path.last.gsub!(/^_/, '')
  path.join('/')
end

#prepareObject



7
8
9
# File 'lib/smt_rails/tilt.rb', line 7

def prepare
  @namespace = "this.#{SmtRails.template_namespace}"
end