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_class}.new\n    mustache.view = self\n    mustache[:yield] = content_for(:layout)\n    mustache.context.update(local_assigns)\n    variables = controller.instance_variable_names\n    variables -= %w[@template]\n\n    if controller.respond_to?(:protected_instance_variables)\n      variables -= controller.protected_instance_variables\n    end\n\n    variables.each do |name|\n      mustache.instance_variable_set(name, controller.instance_variable_get(name))\n    end\n\n    # Declaring an +attr_reader+ for each instance variable in the\n    # Mustache::Rails subclass makes them available to your templates.\n    mustache.class.class_eval do\n      attr_reader *variables.map { |name| name.sub(/^@/, '').to_sym }\n    end\n\n    mustache.render\n  MUSTACHE\nend\n"