Class: Mustache::Rails::TemplateHandler

Inherits:
ActionView::Template::Handler
  • Object
show all
Includes:
ActionView::Template::Handlers::Compilable
Defined in:
lib/generators/mustache/install/templates/lib/mustache_rails.rb

Instance Method Summary collapse

Instance Method Details

#compile(template) ⇒ String

hence the hack below

Parameters:

  • (ActionView::Template)

Returns:

  • (String)

    its evaled in the context of the action view



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/generators/mustache/install/templates/lib/mustache_rails.rb', line 75

def compile(template)
  mustache_class = mustache_class_from_template(template)
  mustache_class.template_file = mustache_template_file(template)
  
  <<-MUSTACHE
    mustache = ::#{mustache_class}.new
    mustache.view = self
    mustache[:yield] = content_for(:layout)
    mustache.context.update(local_assigns)
    variables = controller.instance_variable_names
    variables -= %w[@template]

    if controller.respond_to?(:protected_instance_variables)
      variables -= controller.protected_instance_variables
    end

    variables.each do |name|
      mustache.instance_variable_set(name, controller.instance_variable_get(name))
    end

    # Declaring an +attr_reader+ for each instance variable in the
    # Mustache::Rails subclass makes them available to your templates.
    mustache.class.class_eval do
      attr_reader *variables.map { |name| name.sub(/^@/, '').to_sym }
    end

    mustache.render
  MUSTACHE
end