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

Issues with yield #433

Open
adambedford opened this issue Nov 1, 2016 · 9 comments
Open

Issues with yield #433

adambedford opened this issue Nov 1, 2016 · 9 comments

Comments

@adambedford
Copy link

adambedford commented Nov 1, 2016

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
Copy link
Member

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

The cell helper doesn't pass your block on.

@apotonick
Copy link
Member

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 apotonick reopened this Nov 1, 2016
@007lva
Copy link

007lva commented Nov 8, 2016

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
Copy link

phansch commented Jan 23, 2017

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
Copy link

Ravenstine commented Feb 20, 2017

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
Copy link
Member

apotonick commented Feb 21, 2017

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. 🍻

@ushis
Copy link

ushis commented Feb 26, 2017

@adambedford it should work with:

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

@topherfangio
Copy link

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

@ramontayag
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants