Slim Partial with Yield

3.1k views Asked by At

I have a Slim partial for a widget. The widget has common elements and then some custom content that I want to yield to. What's the correct syntax for that? Here's what I thought would work, but doesn't.

Widget Partial

.container
  .title= title
  .content
    == yield

Page

.page
  = render partial: "widget_partial", locals: { title: "Content 1" } do
    div Some really awesome content.

  = render partial: "widget_partial", locals: { title: "Content 2" } do
    span Different but also awesome content.
2

There are 2 answers

0
Greg Tarsa On

According to the Action View Overview on partials (section 3.2.3), when you are only using a partial template and locals, you can get away with a minimal approach like this:

(widget partial)

.container
  .title
    p #{title}
  = yield

(Page)

  = render "widget_partial", title: "content 1" do
    .div
      p Some really awesome content.

  = render "widget_partial", title: "content 2" do
    .div
      p Different but also awesome content.

Note that in the partial, itself, you cannot put text directly into a div, so I added a <p> element.

0
blackchestnut On

You should add layout argument in the render. Something like this:

(widget partial)

.container
  .title = title
  .content
    = yield

(page)

.page
  = render layout: 'widget_partial', locals: { title: 'Content 1' } do
    div Some really awesome content.

  = render layout: 'widget_partial', locals: { title: 'Content 2' } do
    span Different but also awesome content.

Doc: http://edgeguides.rubyonrails.org/action_view_overview.html#partial-layouts