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

Make possible rendering of a block with a layout and a collection. #13447

Closed
wants to merge 1 commit into from

Conversation

bughit
Copy link
Contributor

@bughit bughit commented Dec 22, 2013

Fixes #13354.

@bughit
Copy link
Contributor Author

bughit commented Dec 22, 2013

@rafaelfranca

both render_partial and now collection_with_template render the block via view._layout_for which allows the layout to render content_for captures instead of/in addition to the block, by passing a symbol to yield

I am not sure this make sense. In a controller layout scenario the template is rendered first and is therefore able to supply content_for to the layout. Here, the block is rendered on demand, and is not able to supply content to the the layout. So what value does _layout_for provide?

Also the docs already state that the layout is able to pass args to the block, so I guess rendering the block first is not an option.

So perhaps it makes sense to avoid _layout_for for block rendering.

@bughit
Copy link
Contributor Author

bughit commented Dec 22, 2013

well, there are tests that depend on _layout_for, so never mind

@@ -412,6 +412,15 @@ def test_render_layout_with_block_and_yield_with_params
@view.render(:layout => "layouts/yield_with_params") { |param| "#{param} Content from block!" }
end

def test_render_layout_with_collection_and_block_and_yield_with_params
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could make this a bit more readable without sacraficing line count:

args     = {layout: "layouts/yield_with_params", collection: %w[item1 item2], as: :item }
actual   = @view.render(args) do |param, locals|
              "#{param} Content from block! #{locals[:item]}:#{locals[:item_counter]}"
            end
expected = %(Yield! Content from block! item1:0\nYield! Content from block! item2:1\n)
assert_equal expected, actual

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test started as a copy, they all look like this, I think it's readable enough.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was hard for me to read, even if the others are hard to read no reason we can't make this one better. At the end of the day it won't block the PR but are you happy with the Rails codebase being just good enough ? 😄

@bughit
Copy link
Contributor Author

bughit commented Dec 24, 2013

going to make a change later, don't merge

@bughit
Copy link
Contributor Author

bughit commented Dec 24, 2013

I am thinking of the following change:

        content = template.render(view, locals) do |*name|
          if view._yield_to_block?(name.first, @block)
            name << name.extract_options!.merge!(locals)
            view.capture(*name, &@block)
          else
            view._layout_for(*name)
          end
        end

      def _layout_for(*args, &block)
        name = args.first

        if _yield_to_block?(name, block)
          capture(*args, &block)
        else
          super(name)
        end
      end

      def _yield_to_block?(first_yield_arg, block)
        block && !first_yield_arg.is_a?(Symbol)
      end

the idea is to only do locals merging if you know you are going to be capturing the block, but that decision is made in _layout_for, so I factored it out. Should I go with this?

@bughit
Copy link
Contributor Author

bughit commented Dec 28, 2013

@rafaelfranca can you please review this and the proposed changes above

@bughit
Copy link
Contributor Author

bughit commented Jan 3, 2014

@rafaelfranca - or suggest someone else

@ghost ghost assigned rafaelfranca Jan 3, 2014
@rafaelfranca
Copy link
Member

ack. Assigned to me and I'll take a look tomorrow

@bughit
Copy link
Contributor Author

bughit commented Apr 9, 2014

@rafaelfranca

@sbonami
Copy link

sbonami commented Sep 17, 2015

@rafaelfranca I believe this is still an issue. Anything I can do to get this bumped?

@rails-bot
Copy link

rails-bot bot commented Dec 18, 2019

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Thank you for your contributions.

@rails-bot rails-bot bot added the stale label Dec 18, 2019
@rails-bot rails-bot bot closed this Dec 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

partial rendering a block with layout and collection does not work
6 participants