Skip to content
This repository has been archived by the owner on May 16, 2021. It is now read-only.

Adding default content for yield_content method #121

Merged
merged 2 commits into from
May 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/sinatra/content_for.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ module Sinatra
# # layout.erb
# <%= yield_content :some_key %>
#
# If you have provided +yield_content+ with a block and no content for the
# specified key is found, it will render the results of the block provided
# to yield_content.
#
# # layout.erb
# <%= yield_content :some_key_with_no_content do %>
# <chunk of="default html">...</chunk>
# <% end %>
#
# === Classic Application
#
# To use the helpers in a classic application all you need to do is require
Expand Down Expand Up @@ -111,6 +120,7 @@ def content_for?(key)
# Would pass <tt>1</tt> and <tt>2</tt> to all the blocks registered
# for <tt>:head</tt>.
def yield_content(key, *args)
return yield(*args) if block_given? && content_blocks[key.to_sym].empty?
content_blocks[key.to_sym].map { |b| capture(*args, &b) }.join
end

Expand Down
10 changes: 10 additions & 0 deletions spec/content_for_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ def render(engine, template)
yield_content(:foo).should be_empty
end

it 'renders default content if no block matches the key and a default block is specified' do
content_for(:bar) { "bar" }
yield_content(:foo) { "foo" }.should == "foo"
end

it 'renders multiple blocks with the same key' do
content_for(:foo) { "foo" }
content_for(:foo) { "bar" }
Expand Down Expand Up @@ -85,6 +90,11 @@ def render(engine, template)
yield_content(:foo).should be_empty
end

it 'renders default content if no block matches the key and a default block is specified' do
render inner, :different_key
yield_content(:foo) { "foo" }.should == "foo"
end

it 'renders multiple blocks with the same key' do
render inner, :multiple_blocks
yield_content(:foo).gsub(/\s/, '').should == "foobarbaz"
Expand Down