aboutsummaryrefslogtreecommitdiff
path: root/lib/sinatra-partials.rb
diff options
context:
space:
mode:
authorPatrick J Cherry <patrick@bytemark.co.uk>2011-04-13 17:03:16 +0100
committerPatrick J Cherry <patrick@bytemark.co.uk>2011-04-13 17:03:16 +0100
commit89a67770e66d11740948e90a41db6cee0482cf8e (patch)
treebe858515fb789a89d68f94975690ab019813726c /lib/sinatra-partials.rb
new version.
Diffstat (limited to 'lib/sinatra-partials.rb')
-rw-r--r--lib/sinatra-partials.rb25
1 files changed, 25 insertions, 0 deletions
diff --git a/lib/sinatra-partials.rb b/lib/sinatra-partials.rb
new file mode 100644
index 0000000..e16313c
--- /dev/null
+++ b/lib/sinatra-partials.rb
@@ -0,0 +1,25 @@
+#
+# 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)
+ 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
+ haml(:"#{template}", options)
+ end
+ end
+end
+