Module: ActionView::Helpers::CaptureHelper

Defined in:
lib/dry_views/rails3_two.rb,
lib/dry_views/rails3_zero.rb

Instance Method Summary collapse

Instance Method Details

#content_for_with_default(name, *args, &block) ⇒ Object

Reference: api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html

Custom method (not overriding anything in Rails)

In a template/layout

= content_for_with_default :my_default_content, :partial => 'layouts/my_default_content'

Override in a nested layout/template/partial:

= content_for :my_default_content do
  = render :partial => 'layouts/my_custom_content', :collection => my_stuff, :as => :stuff

All params after the first will be used as options for the default render

= content_for_with_default :my_default_content, :partial => 'some/partial', :locals => {:variable => 'Some Text'}


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/dry_views/rails3_two.rb', line 22

def content_for_with_default(name, *args, &block)
  if content_check = content_for?(name) # returns true, false, or :empty
    content_for_without_empty_check(name) unless content_check == :empty # when content_check is :empty, does nothing
  elsif block_given?
    content_for_without_empty_check(name, yield) # No need to recheck for empty here, as initial condition handles the :empty case
  else
    options = args.extract_options!
    # Supports the default API of with content as the second param, content_for(name, content = nil, &block)
    if options.empty?
      content_for_without_empty_check(name, args.first) # No need to recheck for empty here, as initial condition handles the :empty case
    else
      render options
    end
  end
end

#content_for_with_empty_check(name, content = nil, &block) ⇒ Object

Override file actionpack/lib/action_view/helpers/capture_helper.rb, line 136 If the empty check has already been performed (as content_for_with_default), you can call content_for_without_empty_check to bypass a duplicate check



40
41
42
43
44
45
46
47
# File 'lib/dry_views/rails3_two.rb', line 40

def content_for_with_empty_check(name, content = nil, &block)
  if content_for?(name) == :empty
    # Some preceding layout or template has already specified that there is to be no content for this name, and that is FINAL.
    return nil
  else
    content_for_without_empty_check(name, content, &block)
  end
end

#content_for_with_empty_check?(name) ⇒ Boolean

Override file actionpack/lib/action_view/helpers/capture_helper.rb, line 175

Returns:

  • (Boolean)


51
52
53
54
55
56
57
58
59
60
# File 'lib/dry_views/rails3_two.rb', line 51

def content_for_with_empty_check?(name)
  if @view_flow.get(name) == DryViews::EMPTY_CONTENT
    #add a check for empty, and return :empty, so we can switch on it for empty content
    return :empty
  elsif content_for_without_empty_check?(name)
    return true
  else
    return false
  end
end

#no_content_for(name) ⇒ Object

Custom method (not overriding anything in Rails)



64
65
66
# File 'lib/dry_views/rails3_two.rb', line 64

def no_content_for(name)
  @view_flow.append!(name, DryViews::EMPTY_CONTENT)
end