Skip to content

Instantly share code, notes, and snippets.

@jeroenvandijk
Created July 24, 2010 16:11
Show Gist options
  • Save jeroenvandijk/488785 to your computer and use it in GitHub Desktop.
Save jeroenvandijk/488785 to your computer and use it in GitHub Desktop.
# # Be sure to install mustache gem and include mustache gem in project Gemfile.
#
# # Template Handler
require 'mustache_rails'
# Generator
Rails.application.config.generators.template_engine :mustache
module MustacheRailsPatch
# Overwrite render to have it render haml first when a template with .haml extension exist
def render(*args)
haml_template = self.class.template_file + ".haml"
if args.empty? && File.exist?(haml_template)
super(Haml::Engine.new(File.read(haml_template)).render)
else
super
end
end
# Overwrite render to have it render haml first when a template with .haml extension exist
def partial(name)
partial_name = "_#{name}.#{Mustache::Rails::Config.template_extension}"
template_dir = Pathname.new(self.class.template_file).dirname
partial_path = File.expand_path("#{template_dir}/#{partial_name}")
haml_template = "#{partial_path}.haml"
if File.exist?(haml_template)
Haml::Engine.new(File.read(haml_template)).render
else
super
end
end
end
# Really hacky way to get what we want, not sure why it had to be this way
class Mustache::Rails
def self.inherited(klass)
klass.send(:include, MustacheRailsPatch)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment