User Guide / Components

Using with Template Partials

ActionView’s template partials are a straighforward way to split larger view templates up into more modular, reusable chunks.

Most Rails apps (even those that also use a framework such as ViewComponent) will have at least a few partials in their codebase, and a set of well considered partials can even serve as a basic sort of component library for smaller apps with simpler requirements.

Lookbook enables partials to be previewed in isolation, either instead of or alongside any other components in the app.

See the Rails docs for more details on using partials: https://guides.rubyonrails.org/

Rendering in previews

Partials are rendered in preview classes using the render method.

# test/components/previews/article_preview.rb
class ArticlePreview < Lookbook::Preview
  def default
    render "elements/article"
  end

  def with_args
    render "elements/article", title: "This is the title" do
      "This is the body"
    end
  end
end

The partial path should be relative to the views directory root.

Rendering templates

It’s also possible to render entire view templates instead of partials. To do so, use the following argument format when calling the render method:

# test/components/previews/page_preview.rb
class PagePreview < Lookbook::Preview
  def default
    render template: "pages/show"
  end

  def with_locals
    render template: "pages/show", locals: { title: "The Title" }
  end
end

As with partials, the template path should be relative to the views directory root.

User Guide

Extending Lookbook

API

Elsewhere