cells icon indicating copy to clipboard operation
cells copied to clipboard

Pass block to cell from Rails template not working

Open lukeredpath opened this issue 10 years ago • 17 comments

New user of cells here, I'm trying to see if I can use it to DRY up a bunch of reusable components from a Bootstrap theme I'm working with.

As well as application specific views, I'm trying to write some generic reusable components. I'm not sure what the best approach is for this but as a starting point I'm trying to use blocks and the new yield feature but it doesn't seem to be working as I'd hoped.

Given a cell:

class PageBodyCell < Cell::ViewModel
end

And it's template:

.row
  .col-lg-12
    .wrapper.wrapper-content
      = yield if block_given?

When I call it in my Rails view like this:

<%= cell(:page_body) do %>
  Some Content
<% end %>

Then the block does not get passed to the cell. If I pass the block to call instead:

<%= cell(:page_body).() do %>
  Some Content
<% end %>

The block does get passed, but the contents of the block are rendered twice. Once before the actual cell and once within the cell.

What's the right approach here?

lukeredpath avatar May 25 '16 13:05 lukeredpath

Passing blocks from views doesn't really work because of the way the horrible output buffer is implemented. I'd use nested cells instead of this.

We will work on a new technique to allow this kind of code, but it will take a while. Sorry!

apotonick avatar May 25 '16 21:05 apotonick

If content is small the workaround could be to bypass templating and pass the block directly:

<%= cell(:page_body).() do
  'Some Content'
 end %>

This approach also allows rendering nested cells:

<%= cell(:page_body).() do
  cell(:content)
 end %>

DimaSamodurov avatar Jan 05 '17 12:01 DimaSamodurov

I also have the need for something like this. Creating nested cells would not be feasible to me, as I'd quickly have hundreds on mini cells.

Is there any solution in the works for this? Or is there another recommended approach?

pdfrod avatar Jan 18 '18 11:01 pdfrod

Are you talking about rendering cells in cells (that we can make work), or rendering cells in controller views? :beers:

apotonick avatar Jan 18 '18 11:01 apotonick

In my case, it's rendering cells in controller views.

pdfrod avatar Jan 18 '18 13:01 pdfrod

See this comment: https://github.com/trailblazer/cells/issues/406#issuecomment-221709226

This will never work, because the compiled controller view code is not compatible with the compiled code in Cells. They use a magic output buffer instance variable, we use a simple data flow. Sorry!

apotonick avatar Jan 18 '18 13:01 apotonick

@pdfrod Can you make a pseudo code example of the view rendering the cell, with the block? Maybe we can find another way.

apotonick avatar Jan 18 '18 13:01 apotonick

@apotonick So here's an example of where this has come up: I have an UI accordion compontent that I want to implement as a cell. An accordion can have multiple items, so an accordion item would be modeled as an AccordionItemCell. The AccordionItemCell renders something like this:

<li>
  <div class="title"><%= title %></div>
  <div class="content"><%= content %></div>
</li>

I would like to be able to use it like this:

<%= cell(:accordion_item, title: 'Item 1') do %>
  Content 1
<% end %>

<%= cell(:accordion_item, title: 'Item 2') do %>
  Content 2
<% end %>

....

<%= cell(:accordion_item, title: 'Item N') do %>
  Content N
<% end %>

Since accordion item's content can be arbitrarily complex, I want to pass a block instead of a simple argument. Do you see a better approach to do this?

pdfrod avatar Jan 18 '18 14:01 pdfrod

Of course there's a ~better~ different approach! :grimacing: Define an object that models the accordion's content. An array would do.

titles = [ "Overview", "API" ]

Then, use the collection: option when rendering the cells.

cell( Accordion::Cell::Item, collection: titles )

What you are trying to do with the block actually makes managing the accordion harder.

apotonick avatar Jan 18 '18 16:01 apotonick

I'm not sure how that would work when each accordion item can have content as arbitrary as in the following example:

<form>
  <ul class="accordion">
    <li class="accordion-item">
      <div class="title">...</div>
      <div class="content">
        <input ...>
        Blah blah
        <select>...</select>
      </div>
    </li>

    <li class="accordion-item">
      <div class="title">...</div>
      <div class="content">
        Hello
        <input ..>
      </div>
    </li>

    <li class="accordion-item">
      <div class="title">...</div>
      <div class="content">
        <img ...>
        <input ..>
      </div>
    </li>
  </ul>
</form>

In general, there isn't a common pattern between the items that I can easily extract into a single object. By your suggestion, it seems that for this example I would have to write 3 additional objects (one for each item content) or am I missing something?

pdfrod avatar Jan 18 '18 18:01 pdfrod

Hm, you could also write a dedicated cell that manages that accordion fragment (that's your controller view now). You could then use the built-in capture and pass that into the cell as an option?

apotonick avatar Jan 18 '18 19:01 apotonick

Yeah that works, but I was hoping the Cell's library would already offer a more elegant/simple solution for this use case, since there are a lot of situations where I need something like this. Anyway, thanks for your help!

pdfrod avatar Jan 18 '18 22:01 pdfrod

It's difficult, since all template engines have a different way of capturing (which happens in the block you want), and some are a bit hacky where the captured block isn't eval'ed where it should be, and so on. Are you using ERBse?

apotonick avatar Jan 19 '18 06:01 apotonick

BTW, in Rails views, this only works because they have one global view instance for all fragments. The fact that capturing works solely relies on the contract that all tpl engines write to @output_buffer, which is, again, global state, and leads to many problems once you split up your views into smaller objects. That's probably the reason ActionView will never have real components. :cry:

apotonick avatar Jan 19 '18 06:01 apotonick

Actually I'm not using ERB, I'm using Slim. Not sure if that makes any difference.

Yeah, that's why I'm trying to move away from Rails views as much as possible - there's too much global state and functions going around, it's hard to build modularized and reusable components.

pdfrod avatar Jan 19 '18 11:01 pdfrod

With Slim, it should work fine if you use capture.

apotonick avatar Jan 19 '18 11:01 apotonick

Cool, I'll try that. Thanks!

pdfrod avatar Jan 19 '18 11:01 pdfrod