# HAML partial IF/ELSE, how can I DRY this up?

**URL:** <https://www.ruby-forum.com/t/haml-partial-if-else-how-can-i-dry-this-up/209657>\
**Category:** Rails\
**Created:** [July 19, 2011, 6:56pm UTC](https://www.ruby-forum.com/t/haml-partial-if-else-how-can-i-dry-this-up/209657 "2011-07-19T18:56:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [July 19, 2011, 6:56pm UTC](https://www.ruby-forum.com/t/haml-partial-if-else-how-can-i-dry-this-up/209657/1 "2011-07-19T18:56:06Z")

</div>

Can I do it without resorting to making another partial to contain the  
%span  
content below?

# partial begin

- if defined(skipLI) && skipLI  
%span this is not DRY
- else  
%li  
%span this is not DRY

# partial end

---

<div class="post-metadata">

**Author:** ![Chris\_Braddock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/919ad9/32.png) [@Chris\_Braddock](https://www.ruby-forum.com/u/Chris_Braddock)\
**Post date:** [July 19, 2011, 7:12pm UTC](https://www.ruby-forum.com/t/haml-partial-if-else-how-can-i-dry-this-up/209657/2 "2011-07-19T19:12:12Z")

</div>

Try content\_for maybe? Usually it’s used for passing snippets up the  
template chain, but it would probably work in this case as well.

- content\_for(:not\_dry) do  
%span this is not DRY
- if defined?(skipLI) && skipLI
  - yield :not\_dry

- else  
%li
  - yield :not\_dry

---

<div class="post-metadata">

**Author:** ![Chris\_Braddock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/919ad9/32.png) [@Chris\_Braddock](https://www.ruby-forum.com/u/Chris_Braddock)\
**Post date:** [July 19, 2011, 7:18pm UTC](https://www.ruby-forum.com/t/haml-partial-if-else-how-can-i-dry-this-up/209657/3 "2011-07-19T19:18:24Z")

</div>

Nice!

Had to change from - yield to = yield.

Thanks.

---

<div class="post-metadata">

**Author:** ![Chris\_Braddock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/919ad9/32.png) [@Chris\_Braddock](https://www.ruby-forum.com/u/Chris_Braddock)\
**Post date:** [July 19, 2011, 7:13pm UTC](https://www.ruby-forum.com/t/haml-partial-if-else-how-can-i-dry-this-up/209657/4 "2011-07-19T19:13:26Z")

</div>

One idea off the top of my head:

Create a helper method and use content\_tag

def give\_it\_to\_me  
html\_content = content\_tag(:span, ‘This is DRY’)

defined?(skipLI) && skipLI ? html\_content : content\_tag(:li,  
html\_content)  
end

I think this will work.

Jamey

On Tue, Jul 19, 2011 at 12:54 PM, Chris Braddock

---

<div class="post-metadata">

**Author:** ![Chris\_Braddock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/919ad9/32.png) [@Chris\_Braddock](https://www.ruby-forum.com/u/Chris_Braddock)\
**Post date:** [July 19, 2011, 7:19pm UTC](https://www.ruby-forum.com/t/haml-partial-if-else-how-can-i-dry-this-up/209657/5 "2011-07-19T19:19:39Z")

</div>

Thanks. I was hoping to not have to pull the code out in to another  
file.  
Not sure if I would have been able to define a method inside the same  
haml  
file or not. The yield suggestion seems to have worked in any case.

---

<div class="post-metadata">

**Author:** ![Chris\_Braddock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/919ad9/32.png) [@Chris\_Braddock](https://www.ruby-forum.com/u/Chris_Braddock)\
**Post date:** [July 21, 2011, 4:58pm UTC](https://www.ruby-forum.com/t/haml-partial-if-else-how-can-i-dry-this-up/209657/6 "2011-07-21T16:58:33Z")

</div>

Bernat Kallo wrote in post #1012148:

> =link\_to\_if make\_link, url\_for(…) do  
> this is dry 😃

No, I was wrong 😊 this is not the way link\_to can be used

What actually worked for me was this helper:

def wrap\_with\_link\_to\_if condition, \*args, &block  
if condition  
link\_to \*args, &block  
else  
capture &block  
end  
end

and then:

=wrap\_with\_link\_to\_if make\_link, url\_for(…) do  
this is dry 😃

---

<div class="post-metadata">

**Author:** ![Chris\_Braddock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/919ad9/32.png) [@Chris\_Braddock](https://www.ruby-forum.com/u/Chris_Braddock)\
**Post date:** [July 21, 2011, 4:38pm UTC](https://www.ruby-forum.com/t/haml-partial-if-else-how-can-i-dry-this-up/209657/7 "2011-07-21T16:38:02Z")

</div>

Thanks, it’s good. I had the same problem with links instead of li, and  
I’ve encountered link\_to\_if in the reference:

=link\_to\_if make\_link, url\_for(…) do  
this is dry 😃

You could see the source of link\_to\_if (and link\_to\_unless) to see how  
to implement it for %li’s if you want to.

Also, I’ve just found capture:

- not\_dry = capture do  
%span This is not dry
- if defined?(skipLI) && skipLI  
=not\_dry
- else  
%li  
=not\_dry

---

<div class="post-metadata">

**Author:** ![Chris\_Braddock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/919ad9/32.png) [@Chris\_Braddock](https://www.ruby-forum.com/u/Chris_Braddock)\
**Post date:** [July 21, 2011, 7:38pm UTC](https://www.ruby-forum.com/t/haml-partial-if-else-how-can-i-dry-this-up/209657/8 "2011-07-21T19:38:44Z")

</div>

Another option is to create a generic helper:

def wrap\_with\_li\_if(condition, content = nil, &block)  
if block\_given?  
content = capture(&block)  
end  
return content\_tag(:li, content) if condition  
return content  
end

Then you can use it like so:

= wrap\_with\_li\_if(defined?(skipLI) && skipLI) do  
%span This is not DRY

or

- notDRY = “This is not DRY”  
= wrap\_with\_li\_if(defined?(skipLI) && skipLI, notDRY)

---

<div class="post-metadata">

**Author:** ![Chris\_Braddock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/919ad9/32.png) [@Chris\_Braddock](https://www.ruby-forum.com/u/Chris_Braddock)\
**Post date:** [July 21, 2011, 7:49pm UTC](https://www.ruby-forum.com/t/haml-partial-if-else-how-can-i-dry-this-up/209657/9 "2011-07-21T19:49:17Z")

</div>

Now that I’m thinking about it, you can maybe even make it more generic  
if  
you like. I can actually think of a few places in my own code this would  
come in handy:

def content\_tag\_if(condition, tag, content = nil, &block)  
if block\_given?  
content = capture(&block)  
end  
return content\_tag(tag, content) if condition  
return content  
end

= content\_tag\_if(defined?(skipLI) && skipLI, :li) do  
%span This is not DRY
