Created
April 9, 2012 22:46
-
-
Save harmesy/2347133 to your computer and use it in GitHub Desktop.
Simple mustache support for the cuba microframework (cuba.is)
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
require 'cuba' | |
require 'mustache' | |
require 'mustache_on_cuba' | |
Cuba.use Rack::Session::Cookie | |
Cuba.plugin MustacheOnCuba | |
Cuba.settings[:mustache] = { | |
views: File.join(File.dirname(__FILE__), "views"), | |
templates: File.join(File.dirname(__FILE__), "templates") | |
} | |
Cuba.define do | |
on get do | |
on root do | |
@title = "Simple" | |
res.write mustache :index | |
end | |
end | |
end |
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
module MustacheOnCuba | |
def mustache(template, options={}, locals={}) | |
locals.update(options.delete(:locals) || {}) | |
options.update(settings[:mustache]) if settings.has_key? :mustache | |
klass = template if template.is_a?(Class) && template < Mustache | |
unless klass | |
factory = Class.new(Mustache) do | |
self.view_path = options[:views] | |
end | |
klass = factory.view_class(template) | |
klass.template_path = options[:templates] | |
end | |
klass.template_name = template.to_s | |
instance = klass.new | |
instance_variables.each do |name| | |
instance.instance_variable_set(name, instance_variable_get(name)) | |
end | |
instance.render(locals) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
MustacheOnCuba::mustache
is obviously incomplete, this is just a simple example of how to marry the two together.