Skip to content

Instantly share code, notes, and snippets.

@sax
Forked from lenary/partials.rb
Created April 6, 2010 16:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sax/357790 to your computer and use it in GitHub Desktop.
Save sax/357790 to your computer and use it in GitHub Desktop.
partials for sinatra
# 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
require 'sinatra/base'
module Sinatra
module 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)
if collection = options.delete(:collection) then
collection.inject([]) do |buffer, member|
buffer << erb(:"#{template}", options.merge(:layout =>
false, :locals => {template_array[-1].to_sym => member}))
end.join("\n")
else
erb(:"#{template}", options)
end
end
end
helpers Partials
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment