Class: ActionView::Template::Handlers::Mustache

Inherits:
Object
  • Object
show all
Defined in:
lib/action_view/template/handlers/mustache.rb

Overview

Public: Mustache template compiler.

Requiring this file should automatically register the template handler, so there should be no need to reference the class directly.

Class Method Summary collapse

Class Method Details

.call(template, source) ⇒ Object

Public: Compile template into Ruby.

template - ActionView::Template object

Returns String of Ruby code to be evaled.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/action_view/template/handlers/mustache.rb', line 24

def self.call(template, source)
  # Use standard mustache parser to generate tokens
  tokens = ::Mustache::Parser.new.compile(source)

  # Use custom generator to generate the compiled ruby
  src = ActionView::Mustache::Generator.new.compile(tokens)

  digest = Digest::MD5.hexdigest(source)[0, 10]
  set_cache_key = "merge({:template_cache_key => \"#{digest}\"})"

  <<-RUBY
    ctx = mustache_view.context; begin; ctx.push(local_assigns.#{set_cache_key}); #{src}; ensure; ctx.pop; end
  RUBY
end