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

53 views
Skip to first unread message

Chris Braddock

unread,
Jul 19, 2011, 12:54:23 PM7/19/11
to rubyonra...@googlegroups.com
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

Chris Kottom

unread,
Jul 19, 2011, 1:09:41 PM7/19/11
to rubyonra...@googlegroups.com
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

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/LIoHBKXXDxIJ.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Jamey Cribbs

unread,
Jul 19, 2011, 1:12:07 PM7/19/11
to rubyonra...@googlegroups.com
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


--

Chris Braddock

unread,
Jul 19, 2011, 1:16:33 PM7/19/11
to rubyonra...@googlegroups.com
Nice!

Had to change from - yield to = yield.

Thanks.

Chris Braddock

unread,
Jul 19, 2011, 1:18:15 PM7/19/11
to rubyonra...@googlegroups.com
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.

Bernat Kallo

unread,
Jul 21, 2011, 10:38:02 AM7/21/11
to rubyonra...@googlegroups.com
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 :D

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

--
Posted via http://www.ruby-forum.com/.

Bernat Kallo

unread,
Jul 21, 2011, 10:58:33 AM7/21/11
to rubyonra...@googlegroups.com
Bernat Kallo wrote in post #1012148:

> =link_to_if make_link, url_for(...) do
> this is dry :D

No, it's wrong :$ this is not the way link_to can be used

Tim Shaffer

unread,
Jul 21, 2011, 1:37:36 PM7/21/11
to rubyonra...@googlegroups.com
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)

Tim Shaffer

unread,
Jul 21, 2011, 1:48:11 PM7/21/11
to rubyonra...@googlegroups.com
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
Reply all
Reply to author
Forward
0 new messages