Module: Sinatra::Partials

Defined in:
lib/partials.rb

Overview

Instance Method Summary collapse

Instance Method Details

#partial(template, *args) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/partials.rb', line 3

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 << erb(:"#{template}", options.merge(:layout =>
      false, :locals => {template_array[-1].to_sym => member}.merge(locals)))
    end.join("\n")
  else
    erb(:"#{template}", options)
  end
end