cells icon indicating copy to clipboard operation
cells copied to clipboard

Issues with `yield`

Open adambedford opened this issue 9 years ago • 9 comments

I have a cell, PanelCell, which renders a bootstrap panel and yields where the panel body belongs. I'm getting a no block given (yield) error when trying to render the page.

Code is as follows:

module SharedComponents
  class PanelCell < Cell::ViewModel
    def show(&block)
      render(&block)
    end

    def title
      options[:title]
    end
  end
end
.panel
  .panel-heading
    %h4= title

  .panel-body
    = yield

This is being invoked here (from another cell show.haml):

= cell(SharedComponents::PanelCell, nil, title: model.title) do
  %p Hello

I would expect <p>Hello</p> to be rendered in place of the yield, but it is blowing up.

Am I running up against a technical limitation of Cells or just misunderstanding the implementation?

adambedford avatar Nov 01 '16 02:11 adambedford

http://trailblazer.to/gems/cells/api.html#yield

The cell helper doesn't pass your block on.

apotonick avatar Nov 01 '16 04:11 apotonick

Actually, it's not the cell "helper" but ::call that doesn't pass on the block: https://github.com/apotonick/cells/blob/master/lib/cell/view_model.rb#L45

I can't remember exactly but there was some problem...?!?!?!

apotonick avatar Nov 01 '16 04:11 apotonick

I have the same problem, works fine if you pass the block from a normal rails view, but not from another cell. Some workaround for this?

007lva avatar Nov 08 '16 09:11 007lva

It looks like the cell method should pass through the block to the template with the current documentation?

Running into a similar issue also trying to make it work for something like panels.

With the above code, if I call the block that is passed to #show it will contain the whole template string of the file where the block/cell-call is defined in.

(Otherwise cells has been a really great experience and it brings a lot of fresh air into every-day development, thank you!)

Update:

A workaround, if you are using Rails, could be to wrap the block in a capture call:

= cell(SharedComponents::PanelCell, nil, title: model.title) do
  - capture do
    %p Hello

phansch avatar Jan 23 '17 09:01 phansch

I have a similar problem but where the block somehow gets yielded both where yield is called and above the cell itself. Using capture just causes the block to not render at all. Rails 4.2.3. It might work with HAML in @phansch's example, but that workaround doesn't appear to help with straight ERB/ActionView.

EDIT: Nevermind, that's a separate issue.

Ravenstine avatar Feb 20 '17 21:02 Ravenstine

The problem is the way ERB is implemented for Rails views: it writes to a stupid instance variable and then Rails changes this variable (aka output buffer) globally when capturing - it's a horrible hack that "makes it work".

The solution is to use our ERBse gem and Cells, only, because there is no global capture state. I'm sorry but I can't easily fix it and won't waste more time on improving Rails, please use Cells for your views and everything will work as expected. :beers:

apotonick avatar Feb 21 '17 08:02 apotonick

@adambedford it should work with:

= cell(SharedComponents::PanelCell, nil, title: model.title).() do
  %p hello world

ushis avatar Feb 26 '17 14:02 ushis

@ushis Can you share a bit of code on how this works? I am not able to get it to work properly.

topherfangio avatar Dec 04 '17 21:12 topherfangio

FWIW, making a helper to clean up the markup in your templates works:

  def component(cell_class, opts, &body)
    cell(CardBoxCell, nil, opts).() do
      capture(&body)
    end
  end

Cell view:

class CardBoxCell < ApplicationCell
  def title
    options[:title]
  end
end

Cell template:

.card-title-bar
  .title= title
.card-container
  = yield

Then in your plain Rails views:

= component(CardBoxCell, title: "Hi") do
  .content-piece
    p Inside part 2

ramontayag avatar Jun 14 '20 23:06 ramontayag