trailblazer / cells

View components for Ruby and Rails.
https://trailblazer.to/2.1/docs/cells.html
3.07k stars 236 forks source link

Issues with `yield` #433

Open adambedford opened 8 years ago

adambedford commented 8 years ago

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?

apotonick commented 8 years ago

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

The cell helper doesn't pass your block on.

apotonick commented 8 years ago

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...?!?!?!

007lva commented 8 years ago

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?

phansch commented 7 years ago

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
Ravenstine commented 7 years ago

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.

apotonick commented 7 years ago

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:

ushis commented 7 years ago

@adambedford it should work with:

= cell(SharedComponents::PanelCell, nil, title: model.title).() do
  %p hello world
topherfangio commented 6 years ago

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

ramontayag commented 4 years ago

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