Skip to content

Commit

Permalink
Update yield_content to append default to ERB template buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
jkowens committed Nov 30, 2018
1 parent ba63ae8 commit 64f33b9
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 6 deletions.
23 changes: 19 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,16 +171,31 @@ 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|
template_buffer << c if block_given? && erb?
end
end
end

private

def content_blocks
@content_blocks ||= Hash.new { |h, k| h[k] = [] }
end

def template_buffer
@template_buffer ||= begin
if t = Tilt.current_template
v = t.options[:outvar]
instance_variable_get(v) if v
end
end
end
end

helpers ContentFor
Expand Down
3 changes: 2 additions & 1 deletion sinatra-contrib/spec/content_for/layout.erb
@@ -1 +1,2 @@
<%= yield_content :foo %>
<%= yield if block_given? %>
<%= yield_content :foo %>
3 changes: 2 additions & 1 deletion sinatra-contrib/spec/content_for/layout.erubis
@@ -1 +1,2 @@
<%= yield_content :foo %>
<%= yield if block_given? %>
<%= yield_content :foo %>
1 change: 1 addition & 0 deletions sinatra-contrib/spec/content_for/layout.haml
@@ -1 +1,2 @@
= yield if block_given?
= yield_content :foo
1 change: 1 addition & 0 deletions sinatra-contrib/spec/content_for/layout.slim
@@ -1 +1,2 @@
== yield if block_given?
= yield_content :foo
1 change: 1 addition & 0 deletions sinatra-contrib/spec/content_for/yield_block.erb
@@ -0,0 +1 @@
<% yield_content :bar do %>bar<% end %>
1 change: 1 addition & 0 deletions sinatra-contrib/spec/content_for/yield_block.erubis
@@ -0,0 +1 @@
<% yield_content :bar do %>bar<% end %>
2 changes: 2 additions & 0 deletions sinatra-contrib/spec/content_for/yield_block.haml
@@ -0,0 +1,2 @@
= yield_content :bar do
bar
2 changes: 2 additions & 0 deletions sinatra-contrib/spec/content_for/yield_block.slim
@@ -0,0 +1,2 @@
= yield_content :bar do
| bar
5 changes: 5 additions & 0 deletions sinatra-contrib/spec/content_for_spec.rb
Expand Up @@ -205,6 +205,11 @@ def body
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")
Expand Down

0 comments on commit 64f33b9

Please sign in to comment.