From 0c91aba8ffe92876650202d5fdace30259a3ca4b Mon Sep 17 00:00:00 2001 From: Micah Redding Date: Tue, 11 Feb 2014 17:41:59 -0600 Subject: [PATCH 1/2] Adding ability for the yield_content method in the content_for package to define default content in the absence of content for a given key --- sinatra-contrib/lib/sinatra/content_for.rb | 1 + sinatra-contrib/spec/content_for_spec.rb | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/sinatra-contrib/lib/sinatra/content_for.rb b/sinatra-contrib/lib/sinatra/content_for.rb index 311a88d8..19397625 100644 --- a/sinatra-contrib/lib/sinatra/content_for.rb +++ b/sinatra-contrib/lib/sinatra/content_for.rb @@ -111,6 +111,7 @@ module Sinatra # Would pass 1 and 2 to all the blocks registered # for :head. 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 diff --git a/sinatra-contrib/spec/content_for_spec.rb b/sinatra-contrib/spec/content_for_spec.rb index ce29b909..a17d417b 100644 --- a/sinatra-contrib/spec/content_for_spec.rb +++ b/sinatra-contrib/spec/content_for_spec.rb @@ -33,6 +33,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" } @@ -85,6 +90,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" From ddfeb50835a43ea2354baa8235bb7c2e4571ddd6 Mon Sep 17 00:00:00 2001 From: Micah Redding Date: Tue, 11 Feb 2014 17:46:34 -0600 Subject: [PATCH 2/2] Adding documentation --- sinatra-contrib/lib/sinatra/content_for.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/sinatra-contrib/lib/sinatra/content_for.rb b/sinatra-contrib/lib/sinatra/content_for.rb index 19397625..778a1ac8 100644 --- a/sinatra-contrib/lib/sinatra/content_for.rb +++ b/sinatra-contrib/lib/sinatra/content_for.rb @@ -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 %> + # ... + # <% end %> + # # === Classic Application # # To use the helpers in a classic application all you need to do is require