Forked from jeroenvandijk/mustache_with_haml_rails3.rb
Created
November 1, 2011 14:10
-
-
Save gustin/1330583 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# # 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