Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flush option for Sinatra::ContentFor flush content #1225

Merged
merged 2 commits into from
Dec 21, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Merge branch 'master' into flush-content-for
  • Loading branch information
namusyaka authored Dec 21, 2018
commit d75c2428bcb35973d8630074c24bda9c4ae34e23
15 changes: 8 additions & 7 deletions sinatra-contrib/lib/sinatra/capture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,14 @@ def capture(*args, &block)
buffer = Haml::Buffer.new(nil, Haml::Options.new.for_buffer)
with_haml_buffer(buffer) { capture_haml(*args, &block) }
else
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 } }

buffer.try :clear
result = render(current_engine, dummy, options, &block)
@_out_buf, _buf_was = '', @_out_buf
begin
raw = block[*args]
captured = block.binding.eval('@_out_buf')
captured.empty? ? raw : captured
ensure
@_out_buf = _buf_was
end
end
end

Expand Down
15 changes: 11 additions & 4 deletions sinatra-contrib/lib/sinatra/content_for.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,18 @@ def clear_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)
def yield_content(key, *args, &block)
key = key.to_sym
return yield(*args) if block_given? && content_blocks[key].empty?

content_blocks[key].map { |b| capture(*args, &b) }.join
if block_given? && !content_for?(key)
haml? ? capture_haml(*args, &block) : yield(*args)
else
content = content_blocks[key].map { |b| capture(*args, &b) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change doesn't seem necessary to me. What do you think?

content.join.tap do |c|
if block_given? && (erb? || erubi? || erubis?)
@_out_buf << c
end
end
end
end

private
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.