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

Update yield_content to append default to ERB template buffer #1500

Merged
merged 7 commits into from Dec 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions sinatra-contrib/lib/sinatra/capture.rb
Expand Up @@ -97,8 +97,9 @@ def capture(*args, &block)
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
Expand All @@ -109,7 +110,7 @@ def capture(*args, &block)
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
Expand Down
16 changes: 12 additions & 4 deletions sinatra-contrib/lib/sinatra/content_for.rb
Expand Up @@ -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 %>
#
Expand Down Expand Up @@ -171,9 +171,17 @@ 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)
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
namusyaka marked this conversation as resolved.
Show resolved Hide resolved
end
end

private
Expand Down
6 changes: 6 additions & 0 deletions sinatra-contrib/spec/capture_spec.rb
Expand Up @@ -50,6 +50,12 @@ def render(engine, template)
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__
Expand Down
2 changes: 1 addition & 1 deletion sinatra-contrib/spec/content_for/layout.erb
@@ -1 +1 @@
<%= yield_content :foo %>
<%= yield_content :foo %>
2 changes: 1 addition & 1 deletion sinatra-contrib/spec/content_for/layout.erubis
@@ -1 +1 @@
<%= yield_content :foo %>
<%= yield_content :foo %>
1 change: 1 addition & 0 deletions sinatra-contrib/spec/content_for/parameter_value.erb
@@ -0,0 +1 @@
<% content_for :foo, 'foo' %>
1 change: 1 addition & 0 deletions sinatra-contrib/spec/content_for/parameter_value.erubis
@@ -0,0 +1 @@
<% content_for :foo, 'foo' %>
1 change: 1 addition & 0 deletions sinatra-contrib/spec/content_for/parameter_value.haml
@@ -0,0 +1 @@
- content_for :foo, 'foo'
1 change: 1 addition & 0 deletions sinatra-contrib/spec/content_for/parameter_value.slim
@@ -0,0 +1 @@
- content_for :foo, 'foo'
1 change: 1 addition & 0 deletions sinatra-contrib/spec/content_for/yield_block.erb
@@ -0,0 +1 @@
<% yield_content :foo do %>baz<% end %>
1 change: 1 addition & 0 deletions sinatra-contrib/spec/content_for/yield_block.erubis
@@ -0,0 +1 @@
<% yield_content :foo do %>baz<% end %>
2 changes: 2 additions & 0 deletions sinatra-contrib/spec/content_for/yield_block.haml
@@ -0,0 +1,2 @@
= yield_content :foo do
baz
2 changes: 2 additions & 0 deletions sinatra-contrib/spec/content_for/yield_block.slim
@@ -0,0 +1,2 @@
= yield_content :foo do
| baz
23 changes: 22 additions & 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_nil
expect(yield_content(:foo) { "foo" }).to eq("foo")
end

Expand Down Expand Up @@ -205,6 +205,27 @@ def body
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")
Expand Down