Skip to content

Commit

Permalink
Refactor yield_content method
Browse files Browse the repository at this point in the history
  • Loading branch information
jkowens committed Dec 9, 2018
1 parent bf2cb85 commit 5643293
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
1 change: 1 addition & 0 deletions sinatra-contrib/lib/sinatra/capture.rb
Expand Up @@ -110,6 +110,7 @@ def capture(*args, &block)
buffer.clear unless buffer.nil?
result = render(current_engine, dummy, options, &block)
end
result ||= ''
result.strip.empty? && @capture ? @capture : result
ensure
buffer.replace(old_buffer) unless buffer.nil?
Expand Down
12 changes: 6 additions & 6 deletions sinatra-contrib/lib/sinatra/content_for.rb
Expand Up @@ -173,13 +173,13 @@ def clear_content_for(key)
# for <tt>:head</tt>.
def yield_content(key, *args, &block)
if block_given? && !content_for?(key)
haml? ? capture_haml(*args, &block) : yield(*args)
capture(*args, &block)
else
content = content_blocks[key.to_sym].map { |b| capture(*args, &b) }
content.join.tap do |c|
if block_given? && (erb? || erubi? || erubis?)
@_out_buf << c
end
content_blocks[key.to_sym].map { |b| capture(*args, &b) }.join
end.tap do |content|
if block_given? && (erb? || erubi? || erubis?)
# Concat to template buffer
@_out_buf << content
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion sinatra-contrib/spec/content_for_spec.rb
Expand Up @@ -33,7 +33,7 @@ def render(engine, template)
end

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

Expand Down

0 comments on commit 5643293

Please sign in to comment.