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:
commit
8a7da30a84
14 changed files with 56 additions and 10 deletions
|
@ -97,8 +97,9 @@ module Sinatra
|
|||
result = block[*args]
|
||||
elsif current_engine == :erb || current_engine == :slim
|
||||
@_out_buf, _buf_was = '', @_out_buf
|
||||
block[*args]
|
||||
result = eval('@_out_buf', block.binding)
|
||||
raw = block.call(*args)
|
||||
captured = block.binding.eval('@_out_buf')
|
||||
result = captured.empty? ? raw : captured
|
||||
@_out_buf = _buf_was
|
||||
else
|
||||
buffer = eval '_buf if defined?(_buf)', block.binding
|
||||
|
@ -109,7 +110,7 @@ module Sinatra
|
|||
buffer.clear unless buffer.nil?
|
||||
result = render(current_engine, dummy, options, &block)
|
||||
end
|
||||
result.strip.empty? && @capture ? @capture : result
|
||||
result && result.strip.empty? && @capture ? @capture : result
|
||||
ensure
|
||||
buffer.replace(old_buffer) unless buffer.nil?
|
||||
end
|
||||
|
|
|
@ -32,7 +32,7 @@ module Sinatra
|
|||
# to yield_content.
|
||||
#
|
||||
# # layout.erb
|
||||
# <%= yield_content :some_key_with_no_content do %>
|
||||
# <% yield_content :some_key_with_no_content do %>
|
||||
# <chunk of="default html">...</chunk>
|
||||
# <% end %>
|
||||
#
|
||||
|
@ -171,9 +171,17 @@ module Sinatra
|
|||
#
|
||||
# Would pass <tt>1</tt> and <tt>2</tt> to all the blocks registered
|
||||
# for <tt>:head</tt>.
|
||||
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
|
||||
def yield_content(key, *args, &block)
|
||||
if block_given? && !content_for?(key)
|
||||
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
|
||||
|
||||
private
|
||||
|
|
|
@ -50,6 +50,12 @@ describe Sinatra::Capture do
|
|||
expect(render(:erb, "iso_8859_1")).to eq("ISO-8859-1 -")
|
||||
end
|
||||
end
|
||||
|
||||
describe 'without templates' do
|
||||
it 'captures empty blocks' do
|
||||
expect(capture {}).to be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
__END__
|
||||
|
|
1
sinatra-contrib/spec/content_for/parameter_value.erb
Normal file
1
sinatra-contrib/spec/content_for/parameter_value.erb
Normal file
|
@ -0,0 +1 @@
|
|||
<% content_for :foo, 'foo' %>
|
1
sinatra-contrib/spec/content_for/parameter_value.erubis
Normal file
1
sinatra-contrib/spec/content_for/parameter_value.erubis
Normal file
|
@ -0,0 +1 @@
|
|||
<% content_for :foo, 'foo' %>
|
1
sinatra-contrib/spec/content_for/parameter_value.haml
Normal file
1
sinatra-contrib/spec/content_for/parameter_value.haml
Normal file
|
@ -0,0 +1 @@
|
|||
- content_for :foo, 'foo'
|
1
sinatra-contrib/spec/content_for/parameter_value.slim
Normal file
1
sinatra-contrib/spec/content_for/parameter_value.slim
Normal file
|
@ -0,0 +1 @@
|
|||
- content_for :foo, 'foo'
|
1
sinatra-contrib/spec/content_for/yield_block.erb
Normal file
1
sinatra-contrib/spec/content_for/yield_block.erb
Normal file
|
@ -0,0 +1 @@
|
|||
<% yield_content :foo do %>baz<% end %>
|
1
sinatra-contrib/spec/content_for/yield_block.erubis
Normal file
1
sinatra-contrib/spec/content_for/yield_block.erubis
Normal file
|
@ -0,0 +1 @@
|
|||
<% yield_content :foo do %>baz<% end %>
|
2
sinatra-contrib/spec/content_for/yield_block.haml
Normal file
2
sinatra-contrib/spec/content_for/yield_block.haml
Normal file
|
@ -0,0 +1,2 @@
|
|||
= yield_content :foo do
|
||||
baz
|
2
sinatra-contrib/spec/content_for/yield_block.slim
Normal file
2
sinatra-contrib/spec/content_for/yield_block.slim
Normal file
|
@ -0,0 +1,2 @@
|
|||
= yield_content :foo do
|
||||
| baz
|
|
@ -33,7 +33,7 @@ describe Sinatra::ContentFor do
|
|||
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_nil
|
||||
expect(yield_content(:foo) { "foo" }).to eq("foo")
|
||||
end
|
||||
|
||||
|
@ -205,6 +205,27 @@ describe Sinatra::ContentFor do
|
|||
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
|
||||
expect(get('/same_key')).to be_ok
|
||||
expect(body).to eq("foo")
|
||||
|
|
Loading…
Reference in a new issue