0

I have a partial similar to this:

/ _some_partial.md.haml

%div.container
  %div.optional-row
    = yield

With a view like this:

/ some_view.html.haml

---
layout: some_layout
---

= partial "some_partial" do
  %p Content here
...

However, I'd like the to render some_partial with the %div.optional-row wrapper in some places, but not others. Is this something that can be achieved with an if/ else statement?

I envisage the solution using content_for and looking something like:

/ _some_partial.md.haml

%div.container
  - if content_for?(:optional_row)
    %div.optional-row
      = yield :optional_row
  - else
    = yield

With

/ some_view.html.haml

---
layout: some_layout
---

- content_for :optional_row do
  = partial "some_partial" do
    %p Content here
...

I know this is horrendously wrong (it returns a TypeError), but I hope it explains my thought process.

This question seems to answer mine in part, but it doesn't explain how to render the partial with the if/ else conditions.

Ps. I'm using Middleman and so throwing in a Padrino tag

Community
  • 1
  • 1
lukebm
  • 163
  • 11
  • How can you yield the content for `:optional_row` in a partial when the output of the partial is part of that content for `:optional_row`? – bschaeffer Dec 30 '15 at 20:21
  • All I know is that you can yield a block in a partial and render it on a page, as per https://github.com/middleman/middleman/issues/1099. Literally been learning haml a week and wondering whether `if/ else` can be referenced when using the partial – lukebm Dec 30 '15 at 20:34
  • Well, I think part of the problem is exactly that. It seems like it should be an infinite loop. What exact error do you get? If/else in a partial is perfectly fine so it's something else. – bschaeffer Dec 31 '15 at 22:37

0 Answers0