From 9fef844f060991d743e62b020b8ccc1e1fac6b45 Mon Sep 17 00:00:00 2001 From: Jordan Owens Date: Fri, 30 Nov 2018 12:54:52 -0500 Subject: [PATCH 1/7] Update yield_content to append default to ERB template buffer --- sinatra-contrib/lib/sinatra/content_for.rb | 14 ++++++++++---- sinatra-contrib/spec/content_for/layout.erb | 3 ++- sinatra-contrib/spec/content_for/layout.erubis | 3 ++- sinatra-contrib/spec/content_for/layout.haml | 1 + sinatra-contrib/spec/content_for/layout.slim | 1 + sinatra-contrib/spec/content_for/yield_block.erb | 1 + .../spec/content_for/yield_block.erubis | 1 + sinatra-contrib/spec/content_for/yield_block.haml | 2 ++ sinatra-contrib/spec/content_for/yield_block.slim | 2 ++ sinatra-contrib/spec/content_for_spec.rb | 5 +++++ 10 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 sinatra-contrib/spec/content_for/yield_block.erb create mode 100644 sinatra-contrib/spec/content_for/yield_block.erubis create mode 100644 sinatra-contrib/spec/content_for/yield_block.haml create mode 100644 sinatra-contrib/spec/content_for/yield_block.slim diff --git a/sinatra-contrib/lib/sinatra/content_for.rb b/sinatra-contrib/lib/sinatra/content_for.rb index 5fb88559..c165c5d2 100644 --- a/sinatra-contrib/lib/sinatra/content_for.rb +++ b/sinatra-contrib/lib/sinatra/content_for.rb @@ -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 %> # ... # <% end %> # @@ -171,9 +171,15 @@ module Sinatra # # Would pass 1 and 2 to all the blocks registered # for :head. - 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| + @_out_buf << c if block_given? && erb? + end + end end private diff --git a/sinatra-contrib/spec/content_for/layout.erb b/sinatra-contrib/spec/content_for/layout.erb index 22cdc056..d6317b7e 100644 --- a/sinatra-contrib/spec/content_for/layout.erb +++ b/sinatra-contrib/spec/content_for/layout.erb @@ -1 +1,2 @@ -<%= yield_content :foo %> \ No newline at end of file +<%= yield if block_given? %> +<%= yield_content :foo %> diff --git a/sinatra-contrib/spec/content_for/layout.erubis b/sinatra-contrib/spec/content_for/layout.erubis index 22cdc056..d6317b7e 100644 --- a/sinatra-contrib/spec/content_for/layout.erubis +++ b/sinatra-contrib/spec/content_for/layout.erubis @@ -1 +1,2 @@ -<%= yield_content :foo %> \ No newline at end of file +<%= yield if block_given? %> +<%= yield_content :foo %> diff --git a/sinatra-contrib/spec/content_for/layout.haml b/sinatra-contrib/spec/content_for/layout.haml index aabdfaae..fc064a96 100644 --- a/sinatra-contrib/spec/content_for/layout.haml +++ b/sinatra-contrib/spec/content_for/layout.haml @@ -1 +1,2 @@ += yield if block_given? = yield_content :foo diff --git a/sinatra-contrib/spec/content_for/layout.slim b/sinatra-contrib/spec/content_for/layout.slim index aabdfaae..807b6326 100644 --- a/sinatra-contrib/spec/content_for/layout.slim +++ b/sinatra-contrib/spec/content_for/layout.slim @@ -1 +1,2 @@ +== yield if block_given? = yield_content :foo diff --git a/sinatra-contrib/spec/content_for/yield_block.erb b/sinatra-contrib/spec/content_for/yield_block.erb new file mode 100644 index 00000000..38efd67b --- /dev/null +++ b/sinatra-contrib/spec/content_for/yield_block.erb @@ -0,0 +1 @@ +<% yield_content :bar do %>bar<% end %> diff --git a/sinatra-contrib/spec/content_for/yield_block.erubis b/sinatra-contrib/spec/content_for/yield_block.erubis new file mode 100644 index 00000000..38efd67b --- /dev/null +++ b/sinatra-contrib/spec/content_for/yield_block.erubis @@ -0,0 +1 @@ +<% yield_content :bar do %>bar<% end %> diff --git a/sinatra-contrib/spec/content_for/yield_block.haml b/sinatra-contrib/spec/content_for/yield_block.haml new file mode 100644 index 00000000..d573b340 --- /dev/null +++ b/sinatra-contrib/spec/content_for/yield_block.haml @@ -0,0 +1,2 @@ += yield_content :bar do + bar diff --git a/sinatra-contrib/spec/content_for/yield_block.slim b/sinatra-contrib/spec/content_for/yield_block.slim new file mode 100644 index 00000000..d6482dc4 --- /dev/null +++ b/sinatra-contrib/spec/content_for/yield_block.slim @@ -0,0 +1,2 @@ += yield_content :bar do + | bar diff --git a/sinatra-contrib/spec/content_for_spec.rb b/sinatra-contrib/spec/content_for_spec.rb index 684f4a76..f53235d4 100644 --- a/sinatra-contrib/spec/content_for_spec.rb +++ b/sinatra-contrib/spec/content_for_spec.rb @@ -205,6 +205,11 @@ describe Sinatra::ContentFor do end end + it 'renders default content' do + expect(get('/yield_block')).to be_ok + expect(body).to eq("bar") + 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") From 4396f1e6a88fcb861010bcea1af7e97c8c3789ed Mon Sep 17 00:00:00 2001 From: Jordan Owens Date: Sat, 1 Dec 2018 23:50:51 -0500 Subject: [PATCH 2/7] Fix issue setting content_for value with method param --- sinatra-contrib/lib/sinatra/capture.rb | 3 +-- sinatra-contrib/spec/content_for/parameter_value.erb | 1 + sinatra-contrib/spec/content_for/parameter_value.erubis | 1 + sinatra-contrib/spec/content_for/parameter_value.haml | 1 + sinatra-contrib/spec/content_for/parameter_value.slim | 1 + sinatra-contrib/spec/content_for_spec.rb | 5 +++++ 6 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 sinatra-contrib/spec/content_for/parameter_value.erb create mode 100644 sinatra-contrib/spec/content_for/parameter_value.erubis create mode 100644 sinatra-contrib/spec/content_for/parameter_value.haml create mode 100644 sinatra-contrib/spec/content_for/parameter_value.slim diff --git a/sinatra-contrib/lib/sinatra/capture.rb b/sinatra-contrib/lib/sinatra/capture.rb index 0bbe7a17..10a00733 100644 --- a/sinatra-contrib/lib/sinatra/capture.rb +++ b/sinatra-contrib/lib/sinatra/capture.rb @@ -97,8 +97,7 @@ 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) + result = block.call(*args) @_out_buf = _buf_was else buffer = eval '_buf if defined?(_buf)', block.binding diff --git a/sinatra-contrib/spec/content_for/parameter_value.erb b/sinatra-contrib/spec/content_for/parameter_value.erb new file mode 100644 index 00000000..b22350c1 --- /dev/null +++ b/sinatra-contrib/spec/content_for/parameter_value.erb @@ -0,0 +1 @@ +<% content_for :foo, 'foo' %> diff --git a/sinatra-contrib/spec/content_for/parameter_value.erubis b/sinatra-contrib/spec/content_for/parameter_value.erubis new file mode 100644 index 00000000..b22350c1 --- /dev/null +++ b/sinatra-contrib/spec/content_for/parameter_value.erubis @@ -0,0 +1 @@ +<% content_for :foo, 'foo' %> diff --git a/sinatra-contrib/spec/content_for/parameter_value.haml b/sinatra-contrib/spec/content_for/parameter_value.haml new file mode 100644 index 00000000..6f492b28 --- /dev/null +++ b/sinatra-contrib/spec/content_for/parameter_value.haml @@ -0,0 +1 @@ +- content_for :foo, 'foo' diff --git a/sinatra-contrib/spec/content_for/parameter_value.slim b/sinatra-contrib/spec/content_for/parameter_value.slim new file mode 100644 index 00000000..6f492b28 --- /dev/null +++ b/sinatra-contrib/spec/content_for/parameter_value.slim @@ -0,0 +1 @@ +- content_for :foo, 'foo' diff --git a/sinatra-contrib/spec/content_for_spec.rb b/sinatra-contrib/spec/content_for_spec.rb index f53235d4..3cc28102 100644 --- a/sinatra-contrib/spec/content_for_spec.rb +++ b/sinatra-contrib/spec/content_for_spec.rb @@ -210,6 +210,11 @@ describe Sinatra::ContentFor do expect(body).to eq("bar") 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") From 5913bec66e92f2c126deafc4b64839eb3527c93a Mon Sep 17 00:00:00 2001 From: Jordan Owens Date: Sat, 8 Dec 2018 14:01:48 -0500 Subject: [PATCH 3/7] Fix content_for specs --- sinatra-contrib/spec/content_for/layout.erb | 1 - sinatra-contrib/spec/content_for/layout.erubis | 1 - sinatra-contrib/spec/content_for/layout.haml | 1 - sinatra-contrib/spec/content_for/layout.slim | 1 - sinatra-contrib/spec/content_for/yield_block.erb | 2 +- sinatra-contrib/spec/content_for/yield_block.erubis | 2 +- sinatra-contrib/spec/content_for/yield_block.haml | 2 +- sinatra-contrib/spec/content_for/yield_block.slim | 2 +- sinatra-contrib/spec/content_for_spec.rb | 9 +++++++-- 9 files changed, 11 insertions(+), 10 deletions(-) diff --git a/sinatra-contrib/spec/content_for/layout.erb b/sinatra-contrib/spec/content_for/layout.erb index d6317b7e..a385d0cf 100644 --- a/sinatra-contrib/spec/content_for/layout.erb +++ b/sinatra-contrib/spec/content_for/layout.erb @@ -1,2 +1 @@ -<%= yield if block_given? %> <%= yield_content :foo %> diff --git a/sinatra-contrib/spec/content_for/layout.erubis b/sinatra-contrib/spec/content_for/layout.erubis index d6317b7e..a385d0cf 100644 --- a/sinatra-contrib/spec/content_for/layout.erubis +++ b/sinatra-contrib/spec/content_for/layout.erubis @@ -1,2 +1 @@ -<%= yield if block_given? %> <%= yield_content :foo %> diff --git a/sinatra-contrib/spec/content_for/layout.haml b/sinatra-contrib/spec/content_for/layout.haml index fc064a96..aabdfaae 100644 --- a/sinatra-contrib/spec/content_for/layout.haml +++ b/sinatra-contrib/spec/content_for/layout.haml @@ -1,2 +1 @@ -= yield if block_given? = yield_content :foo diff --git a/sinatra-contrib/spec/content_for/layout.slim b/sinatra-contrib/spec/content_for/layout.slim index 807b6326..aabdfaae 100644 --- a/sinatra-contrib/spec/content_for/layout.slim +++ b/sinatra-contrib/spec/content_for/layout.slim @@ -1,2 +1 @@ -== yield if block_given? = yield_content :foo diff --git a/sinatra-contrib/spec/content_for/yield_block.erb b/sinatra-contrib/spec/content_for/yield_block.erb index 38efd67b..a68e7784 100644 --- a/sinatra-contrib/spec/content_for/yield_block.erb +++ b/sinatra-contrib/spec/content_for/yield_block.erb @@ -1 +1 @@ -<% yield_content :bar do %>bar<% end %> +<% yield_content :foo do %>bar<% end %> diff --git a/sinatra-contrib/spec/content_for/yield_block.erubis b/sinatra-contrib/spec/content_for/yield_block.erubis index 38efd67b..a68e7784 100644 --- a/sinatra-contrib/spec/content_for/yield_block.erubis +++ b/sinatra-contrib/spec/content_for/yield_block.erubis @@ -1 +1 @@ -<% yield_content :bar do %>bar<% end %> +<% yield_content :foo do %>bar<% end %> diff --git a/sinatra-contrib/spec/content_for/yield_block.haml b/sinatra-contrib/spec/content_for/yield_block.haml index d573b340..5ec45851 100644 --- a/sinatra-contrib/spec/content_for/yield_block.haml +++ b/sinatra-contrib/spec/content_for/yield_block.haml @@ -1,2 +1,2 @@ -= yield_content :bar do += yield_content :foo do bar diff --git a/sinatra-contrib/spec/content_for/yield_block.slim b/sinatra-contrib/spec/content_for/yield_block.slim index d6482dc4..e4f5b1d2 100644 --- a/sinatra-contrib/spec/content_for/yield_block.slim +++ b/sinatra-contrib/spec/content_for/yield_block.slim @@ -1,2 +1,2 @@ -= yield_content :bar do += yield_content :foo do | bar diff --git a/sinatra-contrib/spec/content_for_spec.rb b/sinatra-contrib/spec/content_for_spec.rb index 3cc28102..0b208437 100644 --- a/sinatra-contrib/spec/content_for_spec.rb +++ b/sinatra-contrib/spec/content_for_spec.rb @@ -205,11 +205,16 @@ describe Sinatra::ContentFor do end end - it 'renders default content' do - expect(get('/yield_block')).to be_ok + it 'renders content' do + expect(get('/yield_block/different_key')).to be_ok expect(body).to eq("bar") end + it 'renders default content' do + expect(get('/yield_block/same_key')).to be_ok + expect(body).to eq("foo") + end + it 'renders content set as parameter' do expect(get('/parameter_value')).to be_ok expect(body).to eq("foo") From a3339e0f192c9751ff89babe1c95d8f998be0c5d Mon Sep 17 00:00:00 2001 From: Jordan Owens Date: Sat, 8 Dec 2018 14:02:44 -0500 Subject: [PATCH 4/7] Update capture per review --- sinatra-contrib/lib/sinatra/capture.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sinatra-contrib/lib/sinatra/capture.rb b/sinatra-contrib/lib/sinatra/capture.rb index 10a00733..fb1cd106 100644 --- a/sinatra-contrib/lib/sinatra/capture.rb +++ b/sinatra-contrib/lib/sinatra/capture.rb @@ -97,7 +97,9 @@ module Sinatra result = block[*args] elsif current_engine == :erb || current_engine == :slim @_out_buf, _buf_was = '', @_out_buf - result = block.call(*args) + 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 From ac36e38bbd31bb024e6b2dd5213666055cb8565a Mon Sep 17 00:00:00 2001 From: Jordan Owens Date: Sat, 8 Dec 2018 14:53:33 -0500 Subject: [PATCH 5/7] Write to template buffer for erubi and erubis --- sinatra-contrib/lib/sinatra/content_for.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sinatra-contrib/lib/sinatra/content_for.rb b/sinatra-contrib/lib/sinatra/content_for.rb index c165c5d2..d052713d 100644 --- a/sinatra-contrib/lib/sinatra/content_for.rb +++ b/sinatra-contrib/lib/sinatra/content_for.rb @@ -177,7 +177,9 @@ module Sinatra else content = content_blocks[key.to_sym].map { |b| capture(*args, &b) } content.join.tap do |c| - @_out_buf << c if block_given? && erb? + if block_given? && (erb? || erubi? || erubis?) + @_out_buf << c + end end end end From bf2cb85e8694c34a17515ecac1ef120ecc2b69e1 Mon Sep 17 00:00:00 2001 From: Jordan Owens Date: Sat, 8 Dec 2018 15:11:42 -0500 Subject: [PATCH 6/7] Improve specs for yield_content with default block --- .../spec/content_for/yield_block.erb | 2 +- .../spec/content_for/yield_block.erubis | 2 +- .../spec/content_for/yield_block.haml | 2 +- .../spec/content_for/yield_block.slim | 2 +- sinatra-contrib/spec/content_for_spec.rb | 20 ++++++++++++------- 5 files changed, 17 insertions(+), 11 deletions(-) diff --git a/sinatra-contrib/spec/content_for/yield_block.erb b/sinatra-contrib/spec/content_for/yield_block.erb index a68e7784..d1d98ad3 100644 --- a/sinatra-contrib/spec/content_for/yield_block.erb +++ b/sinatra-contrib/spec/content_for/yield_block.erb @@ -1 +1 @@ -<% yield_content :foo do %>bar<% end %> +<% yield_content :foo do %>baz<% end %> diff --git a/sinatra-contrib/spec/content_for/yield_block.erubis b/sinatra-contrib/spec/content_for/yield_block.erubis index a68e7784..d1d98ad3 100644 --- a/sinatra-contrib/spec/content_for/yield_block.erubis +++ b/sinatra-contrib/spec/content_for/yield_block.erubis @@ -1 +1 @@ -<% yield_content :foo do %>bar<% end %> +<% yield_content :foo do %>baz<% end %> diff --git a/sinatra-contrib/spec/content_for/yield_block.haml b/sinatra-contrib/spec/content_for/yield_block.haml index 5ec45851..1abbec77 100644 --- a/sinatra-contrib/spec/content_for/yield_block.haml +++ b/sinatra-contrib/spec/content_for/yield_block.haml @@ -1,2 +1,2 @@ = yield_content :foo do - bar + baz diff --git a/sinatra-contrib/spec/content_for/yield_block.slim b/sinatra-contrib/spec/content_for/yield_block.slim index e4f5b1d2..805c0b21 100644 --- a/sinatra-contrib/spec/content_for/yield_block.slim +++ b/sinatra-contrib/spec/content_for/yield_block.slim @@ -1,2 +1,2 @@ = yield_content :foo do - | bar + | baz diff --git a/sinatra-contrib/spec/content_for_spec.rb b/sinatra-contrib/spec/content_for_spec.rb index 0b208437..0d8785d6 100644 --- a/sinatra-contrib/spec/content_for_spec.rb +++ b/sinatra-contrib/spec/content_for_spec.rb @@ -205,14 +205,20 @@ describe Sinatra::ContentFor do end end - it 'renders content' do - expect(get('/yield_block/different_key')).to be_ok - expect(body).to eq("bar") - 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 - it 'renders default content' do - expect(get('/yield_block/same_key')).to be_ok - expect(body).to eq("foo") + 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 From 9a3106721a98952e042b5ca85ec5da7d7c573bf4 Mon Sep 17 00:00:00 2001 From: Jordan Owens Date: Sun, 9 Dec 2018 01:15:52 -0500 Subject: [PATCH 7/7] Update capture to handle empty blocks --- sinatra-contrib/lib/sinatra/capture.rb | 2 +- sinatra-contrib/spec/capture_spec.rb | 6 ++++++ sinatra-contrib/spec/content_for_spec.rb | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/sinatra-contrib/lib/sinatra/capture.rb b/sinatra-contrib/lib/sinatra/capture.rb index fb1cd106..b2716c5c 100644 --- a/sinatra-contrib/lib/sinatra/capture.rb +++ b/sinatra-contrib/lib/sinatra/capture.rb @@ -110,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 diff --git a/sinatra-contrib/spec/capture_spec.rb b/sinatra-contrib/spec/capture_spec.rb index bb94f1fc..563205b6 100644 --- a/sinatra-contrib/spec/capture_spec.rb +++ b/sinatra-contrib/spec/capture_spec.rb @@ -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__ diff --git a/sinatra-contrib/spec/content_for_spec.rb b/sinatra-contrib/spec/content_for_spec.rb index 0d8785d6..9309a4d6 100644 --- a/sinatra-contrib/spec/content_for_spec.rb +++ b/sinatra-contrib/spec/content_for_spec.rb @@ -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