Module: Sinatra::Partials

Defined in:
lib/sinatra/partials.rb

Overview

Instance Method Summary collapse

Instance Method Details

#partial(template, *args) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sinatra/partials.rb', line 5

def partial(template, *args)
  key="partial_#{template}_#{args.to_s}"

  template_array = template.to_s.split('/')
  template = template_array[0..-2].join('/') + "/_#{template_array[-1]}"
  options = args.last.is_a?(Hash) ? args.pop : {}
  cache_option = options.delete(:cache)
  options.merge!(:layout => false, :escape_html=>false)
  lambda_func=lambda {
    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}))
      end.join("\n")
    else
      haml(:"#{template}", options)
    end
  }
  #$log.info("#{template},  #{options}")
  if false and cache_option
    if $cache.exists?(key)
      $cache.get(key)
    else
      out=lambda_func.call()
      $cache.put(key, out)
      out
    end
  else
    lambda_func.call()
  end
end