Class: MHaml::MHamlTemplate

Inherits:
Tilt::Template
  • Object
show all
Defined in:
lib/mhaml/mhaml_template.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#namespaceObject (readonly)

Returns the value of attribute namespace.



17
18
19
# File 'lib/mhaml/mhaml_template.rb', line 17

def namespace
  @namespace
end

Class Method Details

.default_mime_typeObject



5
6
7
# File 'lib/mhaml/mhaml_template.rb', line 5

def self.default_mime_type
  'application/javascript'
end

.engine_initialized?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/mhaml/mhaml_template.rb', line 9

def self.engine_initialized?
  true
end

Instance Method Details

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



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/mhaml/mhaml_template.rb', line 19

def evaluate(scope, locals, &block)
  template_key = path_to_key scope
  code = Haml::Engine.new(data, Haml::Template.options.merge(:escape_attrs => false)).render(scope, locals).to_json
  <<-MustacheTemplate
    (function() {
      #{namespace} || (#{namespace} = {});
      #{namespace}[#{template_key.inspect}] = function(object, partials) {
        if (partials == null) {
          partials = {};
        }
        var template = #{code};
        if (object == null){
          return template;
        } else {
          return Mustache.render(template, object, partials);
        }
      };
    }).call(this);
  MustacheTemplate
end

#path_to_key(scope) ⇒ Object



40
41
42
43
44
# File 'lib/mhaml/mhaml_template.rb', line 40

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

#prepareObject



13
14
15
# File 'lib/mhaml/mhaml_template.rb', line 13

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