Here, show is the only public method. By calling render it will invoke rendering for the show view.

Logicless Views

Views come packaged with the cell and can be ERB, Haml, or Slim.

<h3>New Comment</h3>
<%= body %>
By <%= author_link %>

The concept of "helpers" that get strangely copied from modules to the view does not exist in Cells anymore.

Methods called in the view are directly called on the cell instance. You're free to use loops and deciders in views, even instance variables are allowed, but Cells tries to push you gently towards method invocations to access data in the view.

File Structure

In Rails, cells are placed in app/cells or app/concepts/. Every cell has their own directory where it keeps views, assets and code.

app├──cells│├──comment_cell.rb│├──comment││├──show.haml││├──list.haml

The discussed show view would reside in app/cells/comment/show.haml. However, you can set any set of view paths you want.

Invocation Styles

In order to make a cell render, you have to call the rendering methods. While you could call the method directly, the prefered way is the call style.

In Rails, this is all you need to do. In other environments, you need to include the respective module into your cells.

classCommentCell<Cell::ViewModelinclude::Cell::Erb# or Cell::Haml, or Cell::Slim
end

Generators

In Rails, you can generate cells and concept cells.

rails generate cell comment

Or.

rails generate concept comment

Concept Cells

To have real self-contained cells you should use the new concept cell which follows the Trailblazer naming style. Concept cells need to be derived from Cell::Concept, sit in a namespace and are usually named Cell.

classComment::Cell<Cell::Concept# ..
end

Their directory structure looks as follows.

app├──concepts│├──comment││├──cell.rb││├──views│││├──show.haml

This integrates with Trailblazer where classes for one concept sit in the same directory. Please read the book to learn how to use cells with Trailblazer.

You might run into problems with wrong escaping or missing URL helpers. This is not Cells' fault but Rails suboptimal way of implementing and interfacing their helpers. Please open the actionview gem helper code and try figuring out the problem yourself before bombarding us with issues because helper xyz doesn't work.

View Paths

In Rails, the view path is automatically set to app/cells/ or app/concepts/. You can append or set view paths by using ::view_paths. Of course, this works in any Ruby environment.

classCommentCell<Cell::ViewModelself.view_paths="lib/views"end

Asset Packaging

Cells can easily ship with their own JavaScript, CSS and more and be part of Rails' asset pipeline. Bundling assets into a cell allows you to implement super encapsulated widgets that are stand-alone. Asset pipeline is documented here.

Render API

Unlike Rails, the #render method only provides a handful of options you gotta learn.

defshowrenderend

Without options, this will render the state name, e.g. show.erb.

You can provide a view name manually. The following calls are identical.

render:indexrenderview::index

If you need locals, pass them to #render.

renderlocals:{style:"border: solid;"}

Layouts

Every view can be wrapped by a layout. Either pass it when rendering.

renderlayout::default

Or configure it on the class-level.

class CommentCell < Cell::ViewModel
layout :default

The layout is treated as a view and will be searched in the same directories.

Nested Cells

Cells love to render. You can render as many views as you need in a cell state or view.

<%= render :index %>

The #render method really just returns the rendered template string, allowing you all kind of modification.

defshowrender+render(:additional)end

You can even render other cells within a cell using the exact same API.

defaboutcell(:profile,model.author).()end

This works both in cell views and on the instance, in states.

View Inheritance

Cells can inherit code from each other with Ruby's inheritance.

classCommentCell<Cell::ViewModelendclassPostCell<CommentCellend

Even cooler, PostCell will now inherit views from CommentCell.

PostCell.prefixes#=> ["app/cells/post", "app/cells/comment"]

When views can be found in the local post directory, they will be looked up in comment. This starts to become helpful when using composed cells.

If you only want to inherit views, not the entire class, use ::inherit_views.