├── lib ├── generators │ ├── mustache │ │ ├── controller │ │ │ ├── templates │ │ │ │ ├── view.html.mustache.erb │ │ │ │ └── view.rb.erb │ │ │ └── controller_generator.rb │ │ ├── scaffold │ │ │ ├── templates │ │ │ │ ├── _form.html.mustache.erb │ │ │ │ ├── edit.html.mustache.erb │ │ │ │ ├── new.html.mustache.erb │ │ │ │ ├── show.html.mustache.erb │ │ │ │ ├── index.html.mustache.erb │ │ │ │ ├── show.rb.erb │ │ │ │ ├── index.rb.erb │ │ │ │ ├── new.rb.erb │ │ │ │ └── edit.rb.erb │ │ │ └── scaffold_generator.rb │ │ ├── install │ │ │ ├── templates │ │ │ │ ├── config │ │ │ │ │ └── initializers │ │ │ │ │ │ └── mustache.rb │ │ │ │ └── lib │ │ │ │ │ └── mustache_rails.rb │ │ │ └── install_generator.rb │ │ └── README.md │ └── mustache.rb └── mustache_rails.rb ├── config └── initializers │ └── mustache.rb ├── LICENSE ├── mustache_rails3.gemspec └── readme.md /lib/generators/mustache/controller/templates/view.html.mustache.erb: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /lib/generators/mustache/controller/templates/view.rb.erb: -------------------------------------------------------------------------------- 1 | class <%= singular_name.camelize %>::<%= @action.camelize %> < Mustache::Rails 2 | 3 | end -------------------------------------------------------------------------------- /lib/generators/mustache/scaffold/templates/_form.html.mustache.erb: -------------------------------------------------------------------------------- 1 |
<% for attribute in attributes -%>
2 | {{{<%= attribute.name %>_label}}}: {{{<%= attribute.name %>_text_field}}}
3 | <% end -%>
Editing <%= singular_name.capitalize %>
2 | 3 | {{{errors_display_div}}} 4 | 5 |New <%= singular_name.capitalize %>
2 | 3 | {{{errors_display_div}}} 4 | 5 |Displaying <%= singular_name.capitalize %>
2 | 3 | <% for attribute in attributes -%> 4 |<%= attribute.human_name %>: {{<%= attribute.name %>}}
5 | <% end -%> 6 | 7 | Edit | Back 8 | -------------------------------------------------------------------------------- /lib/generators/mustache/install/install_generator.rb: -------------------------------------------------------------------------------- 1 | require 'generators/mustache' 2 | 3 | class Mustache 4 | module Generators 5 | class InstallGenerator < ::Rails::Generators::Base 6 | extend TemplatePath 7 | 8 | def copy_initializer_files 9 | copy_file "config/initializers/mustache.rb", "config/initializers/mustache.rb" 10 | end 11 | 12 | end 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /lib/generators/mustache/scaffold/templates/index.html.mustache.erb: -------------------------------------------------------------------------------- 1 |Listing <%= plural_name.capitalize %>
2 | 3 | {{#listing}} 4 | 5 |6 | <% attributes.collect do |attribute| -%> 7 | <%= attribute.human_name %>: {{<%= attribute.name %>}} 8 | <% end.join(" | ") -%> 9 | | Details
10 | 11 | {{/listing}} 12 | 13 | 14 | -------------------------------------------------------------------------------- /lib/generators/mustache/scaffold/templates/show.rb.erb: -------------------------------------------------------------------------------- 1 | class <%= plural_name.camelize %>::Show < Mustache::Rails 2 | 3 | <% for attribute in attributes -%> 4 | def <%= attribute.name %> 5 | <%= singular_name %>.<%= attribute.name %> 6 | end 7 | 8 | <% end -%> 9 | 10 | def edit_path 11 | edit_<%= singular_name %>_path(@<%= singular_name %>) 12 | end 13 | 14 | def index_path 15 | <%= plural_name %>_path 16 | end 17 | 18 | end -------------------------------------------------------------------------------- /lib/generators/mustache/scaffold/templates/index.rb.erb: -------------------------------------------------------------------------------- 1 | class <%= plural_name.camelize %>::Index < Mustache::Rails 2 | 3 | def new_path 4 | new_<%= singular_name %>_path() 5 | end 6 | 7 | def listing 8 | <%= plural_name %>.collect do |record| 9 | { 10 | <% for attribute in attributes -%> 11 | :<%= attribute.name %> => record.<%= attribute.name %>, 12 | <% end -%> 13 | :show_path => <%= singular_name %>_path(record) 14 | } 15 | end 16 | end 17 | 18 | end -------------------------------------------------------------------------------- /lib/generators/mustache/README.md: -------------------------------------------------------------------------------- 1 | ## Rails 3 Generators for Mustache 2 | 3 | The generators provided are experimental and incomplete. [Mustache](http://github.com/defunkt/mustache) support for [Rails 3](http://weblog.rubyonrails.org/2010/6/8/rails-3-0-beta-4-now-rc-in-days) is [still developing](http://github.com/defunkt/mustache/issues/#issue/3), and we will be updating these generators as that support comes into focus. 4 | 5 | I'm assuming that the Mustache template engine that gets the official blessing will expect to find files where Chris W. and others have suggested the files should go. I.e. the files for widget views would go in: 6 | 7 | * app/views/widgets/action.rb for view class definitions 8 | * app/templates/widgets/action.html.mustache for templates 9 | 10 | ### To Do 11 | 12 | * Layout use 13 | 14 | ### Thanks 15 | 16 | Thanks to Louis T. for running the umbrella Rails 3 Generators project and giving me pointers on writing generators. 17 | Thanks also to José Valim, Paul Barry, and Jeremy McAnally for good information. 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2010 Michael Harrison mh@michaelharrison.ws 4 | Paul Barry 5 | Martin Gamsjaeger 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. -------------------------------------------------------------------------------- /lib/generators/mustache/scaffold/scaffold_generator.rb: -------------------------------------------------------------------------------- 1 | require 'generators/mustache' 2 | require 'rails/generators/erb/scaffold/scaffold_generator' 3 | 4 | class Mustache 5 | module Generators 6 | class ScaffoldGenerator < Erb::Generators::ScaffoldGenerator 7 | extend TemplatePath 8 | 9 | # TODO Layout files? snusnu claims his template engine supports layouts: 10 | # http://github.com/defunkt/mustache/issues/#issue/3/comment/263445 11 | 12 | def copy_view_files 13 | views = available_views 14 | views.delete("index") if options[:singleton] 15 | 16 | views.each do |view| 17 | template "#{view}.rb.erb", 18 | File.join("app/views", controller_file_path, "#{view}.rb") 19 | template "#{view}.html.mustache.erb", 20 | File.join("app/templates", 21 | controller_file_path, 22 | "#{view}.html.mustache") 23 | end 24 | template "_form.html.mustache.erb", 25 | File.join("app/templates", 26 | controller_file_path, 27 | "_form.html.mustache") 28 | end 29 | 30 | private 31 | 32 | def available_views 33 | %w(index edit show new) 34 | end 35 | 36 | end 37 | end 38 | end 39 | -------------------------------------------------------------------------------- /lib/generators/mustache/controller/controller_generator.rb: -------------------------------------------------------------------------------- 1 | require 'generators/mustache' 2 | require 'rails/generators/named_base' 3 | 4 | class Mustache 5 | module Generators 6 | class ControllerGenerator < ::Rails::Generators::NamedBase 7 | extend TemplatePath 8 | 9 | argument :actions, :type => :array, :default => [], :banner => "action action" 10 | 11 | def create_view_files 12 | model_path = File.join(class_path, file_name) 13 | 14 | base_mustache_view_path = File.join("app/views", model_path) 15 | empty_directory base_mustache_view_path 16 | 17 | base_mustache_template_path = File.join("app/templates", model_path) 18 | empty_directory base_mustache_template_path 19 | 20 | actions.each do |action| 21 | @action = action 22 | mustache_view_path = File.join(base_mustache_view_path, 23 | "#{action}.rb") 24 | mustache_template_path = File.join(base_mustache_template_path, 25 | "#{action}.html.mustache") 26 | 27 | template "view.rb.erb", mustache_view_path 28 | template "view.html.mustache.erb", mustache_template_path 29 | end 30 | end 31 | 32 | protected 33 | 34 | # Methods not to be executed go here 35 | 36 | end 37 | end 38 | end -------------------------------------------------------------------------------- /lib/generators/mustache/scaffold/templates/new.rb.erb: -------------------------------------------------------------------------------- 1 | class <%= plural_name.camelize %>::New < Mustache::Rails 2 | 3 | <%# TODO: extract errors_display settings to to module methods -%> 4 | def errors_display_div 5 | return "" unless <%= singular_name %>.errors.any? 6 | content_tag("div", :id=>"errorExplanation", :class=>"errorExplanation") do 7 | content_tag("h2", error_header) + content_tag("ul") do 8 | <%= singular_name %>.errors.full_messages.inject("") do |memo,msg| 9 | memo += content_tag("li", msg) 10 | end 11 | end 12 | end 13 | end 14 | 15 | def <%= singular_name %>_form_tag 16 | form_tag(create_path, :class => "<%= singular_name %>_form", :id => "edit_<%= singular_name %>_#{<%= singular_name %>.id}_form") 17 | end 18 | 19 | <% for attribute in attributes -%> 20 | def <%= attribute.name %>_label 21 | label :<%= singular_name %>, :<%= attribute.name %> 22 | end 23 | <%# TODO: Different fields for different attribute types -%> 24 | 25 | def <%= attribute.name %>_text_field 26 | text_field(:<%= singular_name %>, :<%= attribute.name %>, :id => "<%= attribute.name %>_text_field") 27 | end 28 | 29 | <% end -%> 30 | 31 | def form_submit 32 | submit_tag "Create" 33 | end 34 | 35 | 36 | def index_path 37 | <%= plural_name %>_path 38 | end 39 | 40 | private 41 | 42 | def create_path 43 | <%= plural_name %>_path 44 | end 45 | 46 | def error_header 47 | "u r dong it rong" 48 | end 49 | 50 | end -------------------------------------------------------------------------------- /lib/generators/mustache/scaffold/templates/edit.rb.erb: -------------------------------------------------------------------------------- 1 | class <%= plural_name.camelize %>::Edit < Mustache::Rails 2 | 3 | <%# TODO: extract errors_display settings to to module methods -%> 4 | def errors_display_div 5 | return "" unless <%= singular_name %>.errors.any? 6 | content_tag("div", :id=>"errorExplanation", :class=>"errorExplanation") do 7 | content_tag("h2", error_header) + content_tag("ul") do 8 | <%= singular_name %>.errors.full_messages.inject("") do |memo,msg| 9 | memo += content_tag("li", msg) 10 | end 11 | end 12 | end 13 | end 14 | 15 | def <%= singular_name %>_form_tag 16 | form_tag(update_path, :class => "<%= singular_name %>_form", :method => :put, :id => "edit_<%= singular_name %>_#{<%= singular_name %>.id}_form") 17 | end 18 | 19 | <% for attribute in attributes -%> 20 | def <%= attribute.name %>_label 21 | label :<%= singular_name %>, :<%= attribute.name %> 22 | end 23 | <%# TODO: Different fields for different attribute types -%> 24 | 25 | def <%= attribute.name %>_text_field 26 | text_field(:<%= singular_name %>, :<%= attribute.name %>, :id => "<%= attribute.name %>_text_field") 27 | end 28 | 29 | <% end -%> 30 | 31 | def form_submit 32 | submit_tag "Update" 33 | end 34 | 35 | def show_path 36 | <%= singular_name %>_path(<%= singular_name %>) 37 | end 38 | 39 | def index_path 40 | <%= plural_name %>_path 41 | end 42 | 43 | private 44 | 45 | def update_path 46 | <%= singular_name %>_path(<%= singular_name %>) 47 | end 48 | 49 | def error_header 50 | "u r dong it rong" 51 | end 52 | 53 | end -------------------------------------------------------------------------------- /mustache_rails3.gemspec: -------------------------------------------------------------------------------- 1 | Gem::Specification.new do |s| 2 | s.name = %q{mustache_rails3} 3 | s.version = "0.1.2.1" 4 | s.date = %q{2010-08-01} 5 | s.authors = ["Michael Harrison"] 6 | s.email = %q{mh@michaelharrison.ws} 7 | s.summary = %q{Mustache_rails3 provides a template handler and generators for Rails 3.} 8 | s.homepage = %q{http://github.com/goodmike/mustache_rails3} 9 | s.description = %q{Mustache_rails3 is intended to add Rails 3 support to the existing mustache templating system for Ruby. It provides a template handler for Rails 3 and generators. I strive to make the gem work with the latest beta or release candidate of Rails 3. The source code is maintained at http://github.com/goodmike/mustache_rails3, and I welcome comments and forks. I thank Jens Bissinger for his Rails 3 RC 1 patch.} 10 | s.files = [ "readme.md", "LICENSE", "config/initializers/mustache.rb", 11 | "lib/generators/mustache.rb", 12 | "lib/generators/mustache/controller/controller_generator.rb", 13 | "lib/generators/mustache/controller/templates/view.html.mustache.erb", 14 | "lib/generators/mustache/controller/templates/view.rb.erb", 15 | "lib/generators/mustache/install/install_generator.rb", 16 | "lib/generators/mustache/install/templates/config/initializers/mustache.rb", 17 | "lib/generators/mustache/scaffold/scaffold_generator.rb", 18 | "lib/generators/mustache/scaffold/templates/_form.html.mustache.erb", 19 | "lib/generators/mustache/scaffold/templates/edit.html.mustache.erb", 20 | "lib/generators/mustache/scaffold/templates/edit.rb.erb", 21 | "lib/generators/mustache/scaffold/templates/index.html.mustache.erb", 22 | "lib/generators/mustache/scaffold/templates/index.rb.erb", 23 | "lib/generators/mustache/scaffold/templates/new.html.mustache.erb", 24 | "lib/generators/mustache/scaffold/templates/new.rb.erb", 25 | "lib/generators/mustache/scaffold/templates/show.html.mustache.erb", 26 | "lib/generators/mustache/scaffold/templates/show.rb.erb", 27 | "lib/mustache_rails.rb" 28 | ] 29 | end 30 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | ## Mustache support for Rails 3 2 | 3 | This generator and template handler for Mustache in Rails 3 is based on the 4 | work of Paul Barry, Louis T., and Martin Gamsjaeger. I am indebted to them for allowing me to stand on their shoulders. 5 | 6 | This is also available as a [rubygem](http://rubygems.org/gems/mustache_rails3). 7 | 8 | I'm just getting started. This really is a low-numbered prerelease. :-) I have asked for comments on [the mustache project's Rails Support issue ticket](http://github.com/defunkt/mustache/issues/#issue/3/comment/294928). Please leave feedback there, and thanks. 9 | 10 | ### Views & Templates 11 | 12 | For your view files, subclass Mustache::Rails as (:controller)::(:action) in 13 | app/views/:controller/:action.rb 14 | 15 |#app/views/home/index.rb
16 |
17 | class Home::Index < Mustache::Rails
18 | def world
19 | 'New Caprica'
20 | end
21 | end
22 |
23 |
24 | Mustache::Rails registers a TemplateHandler for ".rb" files. Templates go in
25 | app/templates/:controller/:action.format.mustache
26 |
27 | #app/templates/home/index.html.mustache
28 |
29 | Hello {{world}}!
30 |
31 |
32 | ### Layouts
33 |
34 | Layouts work much the same way, using a similar naming convention. Subclass Mustache::Rails as Layouts::(:layout) in app/views/layouts/:layout.rb
35 |
36 | #app/views/layouts/main.rb
37 |
38 | class Layouts::Main < Mustache::Rails
39 | def default_title
40 | 'A Cylon fleet has jumped into orbit!'
41 | end
42 | end
43 |
44 |
45 | Place the template for your layout in app/templates/layouts/:layout.format.mustache
46 |
47 | #app/templates/layouts/main.html.mustache
48 |
49 | <h1>{{default_title}}</h1>
50 | {{{yield}}}
51 |
52 |
53 | ### Instructions
54 |
55 | A Rails 3 reminder: be sure to add
56 | gem 'mustache'
57 | to your project's Gemfile before running any generators or starting the server.
58 |
59 | If you're using the mustache_rails3 gem, be sure to also add
60 | gem 'mustache_rails3'
61 |
62 | You can enable the mustache template handler by running
63 | rails g mustache:install
64 | in your project directory.
65 |
66 | ### TODO:
67 |
68 | * Add controller-retrofit generator to build default mustache views for existing controllers
69 | * Generate different fields for different attribute types
70 | * Add support for easy conversion of Rails::Mustache objects to JSON representations
71 | * Think about allowing to overwrite layout methods in subclassing views:
72 | http://github.com/defunkt/mustache/blob/master/lib/mustache/sinatra.rb#L79-82
73 | http://github.com/defunkt/mustache/blob/master/lib/mustache/sinatra.rb#L96-102
74 |
--------------------------------------------------------------------------------
/lib/generators/mustache/install/templates/lib/mustache_rails.rb:
--------------------------------------------------------------------------------
1 | require 'action_view'
2 | require 'active_support'
3 | require 'mustache'
4 |
5 | class Mustache
6 |
7 | # Remember to use {{{yield}}} (3 mustaches) to skip escaping HTML
8 | # Using {{{tag}}} will skip escaping HTML so if your mustache methods return
9 | # HTML, be sure to interpolate them using 3 mustaches.
10 |
11 | class Rails < Mustache
12 | attr_accessor :view
13 |
14 | def method_missing(method, *args, &block)
15 | view.send(method, *args, &block)
16 | end
17 |
18 | def respond_to?(method, include_private=false)
19 | super(method, include_private) || view.respond_to?(method, include_private)
20 | end
21 |
22 | # Redefine where Mustache::Rails templates locate their partials:
23 | #
24 | # (1) in the same directory as the current template file.
25 | # (2) in the shared templates path (can be configured via Config.shared_path=(value))
26 | #
27 | def partial(name)
28 | partial_name = "_#{name}.#{Config.template_extension}"
29 | template_dir = Pathname.new(self.class.template_file).dirname
30 | partial_path = File.expand_path("#{template_dir}/#{partial_name}")
31 | unless File.file?(partial_path)
32 | partial_path = "#{Config.shared_path}/#{partial_name}"
33 | end
34 | File.read(partial_path)
35 | end
36 |
37 | # You can change these defaults in, say, a Rails initializer or
38 | # environment.rb, e.g.:
39 | #
40 | # Mustache::Rails::Config.template_base_path = Rails.root.join('app', 'templates')
41 | module Config
42 | def self.template_base_path
43 | @template_base_path ||= ::Rails.root.join('app', 'templates')
44 | end
45 |
46 | def self.template_base_path=(value)
47 | @template_base_path = value
48 | end
49 |
50 | def self.template_extension
51 | @template_extension ||= 'html.mustache'
52 | end
53 |
54 | def self.template_extension=(value)
55 | @template_extension = value
56 | end
57 |
58 | def self.shared_path
59 | @shared_path ||= ::Rails.root.join('app', 'templates', 'shared')
60 | end
61 |
62 | def self.shared_path=(value)
63 | @shared_path = value
64 | end
65 | end
66 |
67 | class TemplateHandler < ActionView::Template::Handler
68 |
69 | include ActionView::Template::Handlers::Compilable
70 |
71 | self.default_format = :mustache
72 |
73 | # @return [String] its evaled in the context of the action view
74 | # hence the hack below
75 | #
76 | # @param [ActionView::Template]
77 | def compile(template)
78 | mustache_class = mustache_class_from_template(template)
79 | mustache_class.template_file = mustache_template_file(template)
80 |
81 | <<-MUSTACHE
82 | mustache = ::#{mustache_class}.new
83 | mustache.view = self
84 | mustache[:yield] = content_for(:layout)
85 | mustache.context.update(local_assigns)
86 | variables = controller.instance_variable_names
87 | variables -= %w[@template]
88 |
89 | if controller.respond_to?(:protected_instance_variables)
90 | variables -= controller.protected_instance_variables
91 | end
92 |
93 | variables.each do |name|
94 | mustache.instance_variable_set(name, controller.instance_variable_get(name))
95 | end
96 |
97 | # Declaring an +attr_reader+ for each instance variable in the
98 | # Mustache::Rails subclass makes them available to your templates.
99 | mustache.class.class_eval do
100 | attr_reader *variables.map { |name| name.sub(/^@/, '').to_sym }
101 | end
102 |
103 | mustache.render
104 | MUSTACHE
105 | end
106 |
107 | private
108 |
109 | def mustache_class_from_template(template)
110 | const_name = ActiveSupport::Inflector.camelize(template.virtual_path.to_s)
111 | defined?(const_name) ? const_name.constantize : Mustache
112 | end
113 |
114 | def mustache_template_file(template)
115 | "#{Config.template_base_path}/#{template.virtual_path}.#{Config.template_extension}"
116 | end
117 |
118 | end
119 | end
120 | end
121 |
122 | ::ActiveSupport::Dependencies.load_paths << Rails.root.join("app", "views")
123 | ::ActionView::Template.register_template_handler(:rb, Mustache::Rails::TemplateHandler)
124 |
--------------------------------------------------------------------------------
/lib/mustache_rails.rb:
--------------------------------------------------------------------------------
1 | require 'action_view'
2 | require 'active_support'
3 | require 'mustache'
4 |
5 | class Mustache
6 |
7 | # Remember to use {{{yield}}} (3 mustaches) to skip escaping HTML
8 | # Using {{{tag}}} will skip escaping HTML so if your mustache methods return
9 | # HTML, be sure to interpolate them using 3 mustaches.
10 |
11 | # Override Mustache's default HTML escaper to only escape strings that
12 | # aren't marked `html_safe?`
13 | def escapeHTML(str)
14 | str.html_safe? ? str : CGI.escapeHTML(str)
15 | end
16 |
17 | class Rails < Mustache
18 | attr_accessor :view
19 |
20 | def method_missing(method, *args, &block)
21 | view.send(method, *args, &block)
22 | end
23 |
24 | def respond_to?(method, include_private=false)
25 | super(method, include_private) || view.respond_to?(method, include_private)
26 | end
27 |
28 | # Redefine where Mustache::Rails templates locate their partials:
29 | #
30 | # (1) in the same directory as the current template file.
31 | # (2) in the shared templates path (can be configured via Config.shared_path=(value))
32 | #
33 | def partial(name)
34 | partial_name = "_#{name}.#{Config.template_extension}"
35 | template_dir = Pathname.new(self.class.template_file).dirname
36 | partial_path = File.expand_path("#{template_dir}/#{partial_name}")
37 | unless File.file?(partial_path)
38 | partial_path = "#{Config.shared_path}/#{partial_name}"
39 | end
40 | File.read(partial_path)
41 | end
42 |
43 | # You can change these defaults in, say, a Rails initializer or
44 | # environment.rb, e.g.:
45 | #
46 | # Mustache::Rails::Config.template_base_path = Rails.root.join('app', 'templates')
47 | module Config
48 | def self.template_base_path
49 | @template_base_path ||= ::Rails.root.join('app', 'templates')
50 | end
51 |
52 | def self.template_base_path=(value)
53 | @template_base_path = value
54 | end
55 |
56 | def self.template_extension
57 | @template_extension ||= 'html.mustache'
58 | end
59 |
60 | def self.template_extension=(value)
61 | @template_extension = value
62 | end
63 |
64 | def self.shared_path
65 | @shared_path ||= ::Rails.root.join('app', 'templates', 'shared')
66 | end
67 |
68 | def self.shared_path=(value)
69 | @shared_path = value
70 | end
71 | end
72 |
73 | class TemplateHandler < ActionView::Template::Handler
74 |
75 | include ActionView::Template::Handlers::Compilable
76 |
77 | self.default_format = :mustache
78 |
79 | # @return [String] its evaled in the context of the action view
80 | # hence the hack below
81 | #
82 | # @param [ActionView::Template]
83 | def compile(template)
84 | mustache_class = mustache_class_from_template(template)
85 | template_file = mustache_template_file(template)
86 |
87 | <<-MUSTACHE
88 | mustache = ::#{mustache_class}.new
89 | mustache.template_file = #{template_file.inspect}
90 | mustache.view = self
91 | mustache[:yield] = content_for(:layout)
92 | mustache.context.update(local_assigns)
93 | variables = controller.instance_variable_names
94 | variables -= %w[@template]
95 |
96 | if controller.respond_to?(:protected_instance_variables)
97 | variables -= controller.protected_instance_variables
98 | end
99 |
100 | variables.each do |name|
101 | mustache.instance_variable_set(name, controller.instance_variable_get(name))
102 | end
103 |
104 | # Declaring an +attr_reader+ for each instance variable in the
105 | # Mustache::Rails subclass makes them available to your templates.
106 | mustache.class.class_eval do
107 | attr_reader *variables.map { |name| name.sub(/^@/, '').to_sym }
108 | end
109 |
110 | mustache.render
111 | MUSTACHE
112 | end
113 |
114 | # In Rails 3.1+, #call takes the place of #compile
115 | def self.call(template)
116 | new.compile(template)
117 | end
118 |
119 | private
120 |
121 | def mustache_class_from_template(template)
122 | const_name = ActiveSupport::Inflector.camelize(template.virtual_path.to_s)
123 | defined?(const_name) ? const_name.constantize : Mustache
124 | end
125 |
126 | def mustache_template_file(template)
127 | "#{Config.template_base_path}/#{template.virtual_path}.#{Config.template_extension}"
128 | end
129 |
130 | end
131 | end
132 | end
133 |
134 | ::ActiveSupport::Dependencies.autoload_paths << Rails.root.join("app", "views")
135 | ::ActionView::Template.register_template_handler(:rb, Mustache::Rails::TemplateHandler)
136 |
--------------------------------------------------------------------------------