Skip to content

Instantly share code, notes, and snippets.

@1gor
Forked from harmesy/app.rb
Created May 22, 2016 13:17
Show Gist options
  • Save 1gor/ebd73bc8a20d342a13b9b4a6f1e8d5dc to your computer and use it in GitHub Desktop.
Save 1gor/ebd73bc8a20d342a13b9b4a6f1e8d5dc 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment