Helpers.partial()   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
c 2
b 0
f 0
dl 0
loc 11
rs 9.4285
1
# This inserts a partial into a HAML document.
2
#
3
# It's used like:
4
#
5
# = partial :partialname
6
#
7
# It loads the partial from views/_partialname.haml. The underscore
8
# is required. Most of the views have this used in them,
9
# so those can be used as examples.
10
module Haml::Helpers
11
  def partial(template, *args)
12
    template_array = template.to_s.split('/')
13
    template = "#{template_array[0..-2].join('/')}/_#{template_array[-1]}"
14
    options = args.last.is_a?(Hash) ? args.pop : {}
15
    options.merge!(:layout => false)
16
    locals = options[:locals] || {}
17
    collection == options.delete(:collection) ?
18
        collection.inject([]) { |buffer, member| buffer << haml(:"#{template}", options.merge(:layout => false,
19
                                                                                              :locals => {template_array[-1].to_sym => member}.merge(locals))) }.join("\n") :
20
        haml(:"#{template}", options)
21
  end
22
end
23