Module: Padrino::Helpers::RenderHelpers

Defined in:
lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/render_helpers.rb

Overview

Helpers related to rendering within templates (i.e partials).

Instance Method Summary collapse

Instance Method Details

#partial(template, options = {}) ⇒ String Also known as: render_partial

Note:

If using this from Sinatra, pass explicit :engine option

Render a partials with collections support

Examples:

partial 'photo/item', :object => @photo
partial 'photo/item', :collection => @photos
partial 'photo/item', :locals => { :foo => :bar }
partial 'photo/item', :engine => :erb

Parameters:

  • template (String)

    Relative path to partial template.

  • options (Hash) (defaults to: {})

    Options hash for rendering options.

Options Hash (options):

  • :object (Object)

    Object rendered in partial.

  • :collection (Array<Object>)

    Partial is rendered for each object in this collection.

  • :locals (Hash) — default: {}

    Local variables accessible in the partial.

  • :engine (Symbol)

    Explicit rendering engine to use for this partial

Returns:

  • (String)

    The html generated from this partial.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/render_helpers.rb', line 34

def partial(template, options={})
  options.reverse_merge!(:locals => {}, :layout => false)
  path            = template.to_s.split(File::SEPARATOR)
  object_name     = path[-1].to_sym
  path[-1]        = "_#{path[-1]}"
  explicit_engine = options.delete(:engine)
  template_path   = File.join(path).to_sym
  raise 'Partial collection specified but is nil' if options.has_key?(:collection) && options[:collection].nil?
  if collection = options.delete(:collection)
    options.delete(:object)
    counter = 0
    collection.map { |member|
      counter += 1
      options[:locals].merge!(object_name => member, "#{object_name}_counter".to_sym => counter)
      render(explicit_engine, template_path, options.dup)
    }.join("\n").html_safe
  else
    if member = options.delete(:object)
      options[:locals].merge!(object_name => member)
    end
    render(explicit_engine, template_path, options.dup).html_safe
  end
end