abloom (owner)

Revisions

gist: 116233 Download_button fork
public
Public Clone URL: git://gist.github.com/116233.git
Embed All Files: show embed
override_helper.rb #
1
2
3
4
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
module OverrideHelper
  # Call this method to setup a default content block by name. This content will be rendered into the page
  # unless a content_block with the same name also exists somewhere else in the rendered response.
  #
  # === Examples
  # Using a block for complex, or lengthy content
  # <% content_default(:navigation) do %>
  # <ul>
  # <li><%= link "Home" %></li>
  # <li><%= link "Product Info", "product-info" %></li>
  # </ul>
  # <% end %>
  #
  # Passing content as an argument
  # <title><%= content_default(:title, "Default Title") %></title>
  def content_default(name, content = nil, &block)
    if override_content[name].blank?
      # default
      content = capture(&block) if block_given?
    else
      # override
      content = override_content[name]
    end
          
    if block_given?
      concat(content)
    else
      return content
    end
  end
  
  # Call this method to override a call to content_default. If no call to content_default with
  # a matching name exists then this block will not get rendered into the response.
  #
  # === Examples
  # Using a block for complex, or lengthy content
  # <% content_block(:navigation) do %>
  # <ul>
  # <li><%= link "Super Special Home" %></li>
  # <li><%= link "Different Product Info", "product-info" %></li>
  # </ul>
  # <% end %>
  #
  # Passing content as an argument
  # <title><%= content_block(:title, "Custom Title") %></title>
  def content_block(name, content = nil, &block)
    content = capture(&block) if block_given?
    override_content[name] = content
    nil
  end
 
  private
    def override_content # :nodoc:
      @override_content ||= {}
    end
end