Skip to content

Instantly share code, notes, and snippets.

@harmesy
Created April 9, 2012 22:46
Show Gist options
  • Save harmesy/2347133 to your computer and use it in GitHub Desktop.
Save harmesy/2347133 to your computer and use it in GitHub Desktop.
Simple mustache support for the cuba microframework (cuba.is)
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
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
@harmesy
Copy link
Author

harmesy commented Apr 9, 2012

MustacheOnCuba::mustache is obviously incomplete, this is just a simple example of how to marry the two together.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment