Skip to content

Commit

Permalink
[padrino-helpers] Adds create "content_for?" helper (Thanks @mlightner)
Browse files Browse the repository at this point in the history
  • Loading branch information
nesquena committed Sep 10, 2011
1 parent 2dafe2e commit c7e4043
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGES.rdoc
Expand Up @@ -5,6 +5,7 @@
* FIX issue with generator runner for https templates (@xavierRiley)
* NEW #657 improve docs and conform to YARD format (@postmodern)
* NEW #658 remove shoulda and switch to minitest (plus aliases)
* NEW #662 create "content_for?" helper (@mlightner)

== 0.10.2

Expand Down
3 changes: 2 additions & 1 deletion padrino-helpers/README.rdoc
Expand Up @@ -34,7 +34,8 @@ Added to a template, this will capture the includes from the block and allow the
...

This will automatically insert the contents of the block (in this case a stylesheet include) into the
location the content is yielded within the layout.
location the content is yielded within the layout. You can also check if content exists for a block using
<tt>content_for?(true)</tt> which returns true if content exists.

The capture_html and the concat_content methods allow content to be manipulated and stored for use in building
additional helpers accepting blocks or displaying information in a template. One example is the use of
Expand Down
35 changes: 17 additions & 18 deletions padrino-helpers/lib/padrino-helpers/output_helpers.rb
Expand Up @@ -106,6 +106,22 @@ def content_for(key, content = nil, &block)
content_blocks[key.to_sym] << (block_given? ? block : Proc.new { content })
end

##
# Is there a content block for a given key?
#
# @param [Symbol] key
# Name of content to yield
#
# @return [TrueClass,FalseClass] Result html for the given +key+
#
# @example
# content_for? :header => true
#
# @api public
def content_for?(key)
content_blocks[key.to_sym].present?
end

##
# Render the captured content blocks for a given key.
# You can also pass values to the content blocks by passing them
Expand All @@ -129,24 +145,7 @@ def yield_content(key, *args)
return nil if blocks.empty?
blocks.map { |content| capture_html(*args, &content) }.join
end

##
# Is there a content block for a given key?
#
# @param [Symbol] key
# Name of content to yield
#
# @return [TrueClass,FalseClass] Result html for the given +key+
#
# @example
# content_for? :header
#
# @api public
def content_for?(key)
blocks = content_blocks[key.to_sym]
!(blocks.nil? || blocks.empty?)
end


protected
##
# Retrieves content_blocks stored by content_for or within yield_content
Expand Down
Expand Up @@ -9,3 +9,6 @@
<% end %>

<div class='demo2'><%= yield_content :demo2, "Johnny", "Smith" %></div>

<div class="demo_has_content"><%= content_for?(:demo).to_s %>
<div class="fake_has_content"><%= content_for?(:fake).to_s %>
Expand Up @@ -7,3 +7,6 @@
%h1 This is content yielded with name #{fname + " " + lname}

.demo2= yield_content :demo2, "Johnny", "Smith"

.demo_has_content= content_for?(:demo)
.fake_has_content= content_for?(:fake)
Expand Up @@ -7,3 +7,6 @@
h1 This is content yielded with name #{fname + " " + lname}

.demo2== yield_content :demo2, "Johnny", "Smith"

.demo_has_content== content_for?(:demo)
.fake_has_content== content_for?(:fake)
22 changes: 21 additions & 1 deletion padrino-helpers/test/test_output_helpers.rb
Expand Up @@ -24,7 +24,27 @@ def app
assert_have_selector '.demo h1', :content => "This is content yielded from a content_for"
assert_have_selector '.demo2 h1', :content => "This is content yielded with name Johnny Smith"
end
end
end # content_for

context "for #content_for? method" do
should 'work for erb templates' do
visit '/erb/content_for'
assert_have_selector '.demo_has_content', :content => "true"
assert_have_selector '.fake_has_content', :content => "false"
end

should "work for haml templates" do
visit '/haml/content_for'
assert_have_selector '.demo_has_content', :content => "true"
assert_have_selector '.fake_has_content', :content => "false"
end

should "work for slim templates" do
visit '/slim/content_for'
assert_have_selector '.demo_has_content', :content => "true"
assert_have_selector '.fake_has_content', :content => "false"
end
end # content_for?

context 'for #capture_html method' do
should "work for erb templates" do
Expand Down

0 comments on commit c7e4043

Please sign in to comment.