1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00

Merge pull request #121 from centresource/master

Adding default content for yield_content method
This commit is contained in:
Zachary Scott 2016-05-03 17:16:06 +09:00
commit 57373804f8
2 changed files with 20 additions and 0 deletions

View file

@ -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
@ -111,6 +120,7 @@ module Sinatra
# 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

View file

@ -32,6 +32,11 @@ describe Sinatra::ContentFor do
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" }
@ -84,6 +89,11 @@ describe Sinatra::ContentFor do
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"