From cb45e46fdb51c683f431a6fbc4558379f5b1689c Mon Sep 17 00:00:00 2001 From: Shota Iguchi Date: Fri, 30 Dec 2016 15:17:47 +0900 Subject: [PATCH] Add flush option to content_for --- sinatra-contrib/lib/sinatra/capture.rb | 4 ++-- sinatra-contrib/lib/sinatra/content_for.rb | 13 +++++++++---- sinatra-contrib/spec/content_for_spec.rb | 16 ++++++++++++++++ 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/sinatra-contrib/lib/sinatra/capture.rb b/sinatra-contrib/lib/sinatra/capture.rb index e219a9315b..285503ae1e 100644 --- a/sinatra-contrib/lib/sinatra/capture.rb +++ b/sinatra-contrib/lib/sinatra/capture.rb @@ -104,7 +104,7 @@ def capture(*args, &block) buffer = eval '_buf if defined?(_buf)', block.binding old_buffer = buffer.dup if buffer dummy = DUMMIES.fetch(current_engine) - options = { :layout => false, :locals => {:args => args, :block => block }} + options = { :layout => false, :locals => {:args => args, :block => block } } buffer.try :clear result = render(current_engine, dummy, options, &block) @@ -116,7 +116,7 @@ def capture(*args, &block) def capture_later(&block) engine = current_engine - proc { |*a| with_engine(engine) { @capture = capture(*a, &block) }} + proc { |*a| with_engine(engine) { @capture = capture(*a, &block) } } end end diff --git a/sinatra-contrib/lib/sinatra/content_for.rb b/sinatra-contrib/lib/sinatra/content_for.rb index c61d8191ed..ce28e19b73 100644 --- a/sinatra-contrib/lib/sinatra/content_for.rb +++ b/sinatra-contrib/lib/sinatra/content_for.rb @@ -128,9 +128,12 @@ module ContentFor # # Your blocks can also receive values, which are passed to them # by yield_content - def content_for(key, value = nil, &block) + def content_for(key, value = nil, options = {}, &block) + key = key.to_sym + block ||= proc { |*| value } - content_blocks[key.to_sym] << capture_later(&block) + clear_content_for(key) if options[:flush] + content_blocks[key] << capture_later(&block) end # Check if a block of content with the given key was defined. For @@ -172,8 +175,10 @@ def clear_content_for(key) # 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 + key = key.to_sym + return yield(*args) if block_given? && content_blocks[key].empty? + + content_blocks[key].map { |b| capture(*args, &b) }.join end private diff --git a/sinatra-contrib/spec/content_for_spec.rb b/sinatra-contrib/spec/content_for_spec.rb index d09028896d..77248bd24a 100644 --- a/sinatra-contrib/spec/content_for_spec.rb +++ b/sinatra-contrib/spec/content_for_spec.rb @@ -70,6 +70,22 @@ def render(engine, template) content_for(:foo, "foo") expect(yield_content(:foo)).to eq("foo") end + + context 'when flush option was disabled' do + it 'append content' do + content_for(:foo, "foo") + content_for(:foo, "bar") + expect(yield_content(:foo)).to eq("foobar") + end + end + + context 'when flush option was enabled' do + it 'flush first content' do + content_for(:foo, "foo") + content_for(:foo, "bar", flush: true) + expect(yield_content(:foo)).to eq("bar") + end + end end # TODO: liquid radius markaby builder nokogiri