Skip to content

Instantly share code, notes, and snippets.

@matiskay
Forked from lenary/partials.rb
Created December 29, 2011 21:24
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matiskay/1536267 to your computer and use it in GitHub Desktop.
Save matiskay/1536267 to your computer and use it in GitHub Desktop.
Partials on Sinatra + Haml

Partials on Sinatra + Haml

This is the haml version. If you want to use the same with erb just change haml to erb.

How to use it

  • Create you partial form example _form.haml (The underscore at the beginning is really important).
  • In you .haml file just write = partial("form").

Info

# stolen from http://github.com/cschneid/irclogger/blob/master/lib/partials.rb
# and made a lot more robust by me
# this implementation uses erb by default. if you want to use any other template mechanism
# then replace `erb` on line 13 and line 17 with `haml` or whatever
module Sinatra::Partials
def partial(template, *args)
template_array = template.to_s.split('/')
template = template_array[0..-2].join('/') + "/_#{template_array[-1]}"
options = args.last.is_a?(Hash) ? args.pop : {}
options.merge!(:layout => false)
locals = options[:locals] || {}
if collection = options.delete(:collection) then
collection.inject([]) do |buffer, member|
buffer << haml(:"#{template}", options.merge(:layout =>
false, :locals => {template_array[-1].to_sym => member}.merge(locals)))
end.join("\n")
else
haml(:"#{template}", options)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment