-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Add flush option for Sinatra::ContentFor flush content #1225
Conversation
@iguchi1124 I don't think that the way you've implemented :flush quite lines up with the Rails behavior. This is how I would expect flush to behave:
So instead of just rendering the content one time, it renders only the last content with |
@@ -128,9 +129,25 @@ module ContentFor | |||
# | |||
# Your blocks can also receive values, which are passed to them | |||
# by <tt>yield_content</tt> | |||
def content_for(key, value = nil, &block) | |||
def content_for(key, *args, &block) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rather see the method signature:
def content_for(key, value = nil, options = {}, &block)
It would avoid the need to jump through the hoops below.
end | ||
|
||
private | ||
|
||
def content_blocks | ||
@content_blocks ||= Hash.new { |h, k| h[k] = [] } | ||
def contents |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kind of like the method name content_blocks, I feel like it is a bit more descriptive of what the hash represents.
block ||= proc { |*| value } | ||
content_blocks[key.to_sym] << capture_later(&block) | ||
flush_key_set << key if options[:flush] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this could be simplified to just something like:
clear_content_for(key) if options[:flush]
I'm almost certain that would take care of mimicking the Rails behavior.
@iguchi1124 thanks for getting started on this feature! I think you're close, just a few tweaks should do it 👍 |
I'll try to fix the review points soon, thanks for reviews! |
Thank you for updating your patch @iguchi1124! I will postpone this until after 2.0.0 release is made. |
Hello! Is this finally making it into Sinatra? @zzak |
Hi there, do you have any progress regarding this releases or reviews? |
@namusyaka if this can be rebased, can we schedule this for the 2.1 release? |
block ||= proc { |*| value } | ||
content_blocks[key.to_sym] << capture_later(&block) | ||
clear_content_for(key) if options[:flush] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@iguchi1124 this is the only line that needs added in this method. Can you rebase with master and limit changes to just this line and the added tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just tried to resolve the conflicts by web GUI.
I don't have to wait until 2.1 release because the public interface seems not to be broken by this patch. The internal key symbolization isn't problem, I think. |
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 = content_blocks[key].map { |b| capture(*args, &b) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change doesn't seem necessary to me. What do you think?
block ||= proc { |*| value } | ||
content_blocks[key.to_sym] << capture_later(&block) | ||
clear_content_for(key) if options[:flush] | ||
content_blocks[key] << capture_later(&block) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rather leave this as:
content_blocks[key.to_sym] << capture_later(&block)
and delete the assignment key = key.to_sym
above.
@jkowens Your reviews should be reflected after merging this. |
### sinatra-contrib * Add `flush` option to `content_for` [#1225](sinatra/sinatra#1225) by Shota Iguchi * Drop activesupport dependency from sinatra-contrib [#1448](sinatra/sinatra#1448) * Update `yield_content` to append default to ERB template buffer [#1500](sinatra/sinatra#1500) by Jordan Owens
close #1138
I added flush option to
content_for
method for flush content. which like content_for :flush options of Rails.see: http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#method-i-content_for