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

Merge pull request #1500 from jkowens/fix-1480

Update yield_content to append default to ERB template buffer
This commit is contained in:
namusyaka 2018-12-09 20:28:51 +09:00 committed by GitHub
commit 8a7da30a84
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 56 additions and 10 deletions

View file

@ -97,8 +97,9 @@ module Sinatra
result = block[*args] result = block[*args]
elsif current_engine == :erb || current_engine == :slim elsif current_engine == :erb || current_engine == :slim
@_out_buf, _buf_was = '', @_out_buf @_out_buf, _buf_was = '', @_out_buf
block[*args] raw = block.call(*args)
result = eval('@_out_buf', block.binding) captured = block.binding.eval('@_out_buf')
result = captured.empty? ? raw : captured
@_out_buf = _buf_was @_out_buf = _buf_was
else else
buffer = eval '_buf if defined?(_buf)', block.binding buffer = eval '_buf if defined?(_buf)', block.binding
@ -109,7 +110,7 @@ module Sinatra
buffer.clear unless buffer.nil? buffer.clear unless buffer.nil?
result = render(current_engine, dummy, options, &block) result = render(current_engine, dummy, options, &block)
end end
result.strip.empty? && @capture ? @capture : result result && result.strip.empty? && @capture ? @capture : result
ensure ensure
buffer.replace(old_buffer) unless buffer.nil? buffer.replace(old_buffer) unless buffer.nil?
end end

View file

@ -32,7 +32,7 @@ module Sinatra
# to yield_content. # to yield_content.
# #
# # layout.erb # # layout.erb
# <%= yield_content :some_key_with_no_content do %> # <% yield_content :some_key_with_no_content do %>
# <chunk of="default html">...</chunk> # <chunk of="default html">...</chunk>
# <% end %> # <% end %>
# #
@ -171,9 +171,17 @@ module Sinatra
# #
# Would pass <tt>1</tt> and <tt>2</tt> to all the blocks registered # Would pass <tt>1</tt> and <tt>2</tt> to all the blocks registered
# for <tt>:head</tt>. # for <tt>:head</tt>.
def yield_content(key, *args) def yield_content(key, *args, &block)
return yield(*args) if block_given? && content_blocks[key.to_sym].empty? if block_given? && !content_for?(key)
content_blocks[key.to_sym].map { |b| capture(*args, &b) }.join haml? ? capture_haml(*args, &block) : yield(*args)
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
end
end
end end
private private

View file

@ -50,6 +50,12 @@ describe Sinatra::Capture do
expect(render(:erb, "iso_8859_1")).to eq("ISO-8859-1 -") expect(render(:erb, "iso_8859_1")).to eq("ISO-8859-1 -")
end end
end end
describe 'without templates' do
it 'captures empty blocks' do
expect(capture {}).to be_nil
end
end
end end
__END__ __END__

View file

@ -0,0 +1 @@
<% content_for :foo, 'foo' %>

View file

@ -0,0 +1 @@
<% content_for :foo, 'foo' %>

View file

@ -0,0 +1 @@
- content_for :foo, 'foo'

View file

@ -0,0 +1 @@
- content_for :foo, 'foo'

View file

@ -0,0 +1 @@
<% yield_content :foo do %>baz<% end %>

View file

@ -0,0 +1 @@
<% yield_content :foo do %>baz<% end %>

View file

@ -0,0 +1,2 @@
= yield_content :foo do
baz

View file

@ -0,0 +1,2 @@
= yield_content :foo do
| baz

View file

@ -33,7 +33,7 @@ describe Sinatra::ContentFor do
end end
it 'renders default content if no block matches the key and a default block is specified' do 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_nil
expect(yield_content(:foo) { "foo" }).to eq("foo") expect(yield_content(:foo) { "foo" }).to eq("foo")
end end
@ -205,6 +205,27 @@ describe Sinatra::ContentFor do
end end
end end
describe 'with a default content block' do
describe 'when content_for key exists' do
it 'ignores default content and renders content' do
expect(get('/yield_block/same_key')).to be_ok
expect(body).to eq("foo")
end
end
describe 'when content_for key is missing' do
it 'renders default content block' do
expect(get('/yield_block/different_key')).to be_ok
expect(body).to eq("baz")
end
end
end
it 'renders content set as parameter' do
expect(get('/parameter_value')).to be_ok
expect(body).to eq("foo")
end
it 'renders blocks declared with the same key you use when rendering' do it 'renders blocks declared with the same key you use when rendering' do
expect(get('/same_key')).to be_ok expect(get('/same_key')).to be_ok
expect(body).to eq("foo") expect(body).to eq("foo")