%a vs. link_to in haml

4,500 views
Skip to first unread message

Denis Haskin

unread,
Feb 25, 2014, 9:30:07 AM2/25/14
to boston-rubygroup
A colleague has been claiming it's okay to use %a{} in haml instead of link_to.

The equivalent of
    = link_to 'Feed', feed_path(feed)
would be
    %a{href: feed_path(feed)} Feed

I can't come up with a compelling reason that it's not okay to do that in HAML.  In ERB you wouldn't do it because the resulting ERB would be complex and hard to read.

Am I missing something?

Thanks,

Denis

Parker Morse

unread,
Feb 25, 2014, 9:44:55 AM2/25/14
to boston-r...@googlegroups.com
I would do it in HAML if I needed to nest additional HTML inside the anchor (e.g. a button tag), or to add attributes other than an HREF to the anchor, but not always. Essentially it comes down to the idea of which is easier to read. (It might be marginally faster to use the %a ?)


--
You received this message because you are subscribed to the Google Groups "Boston Ruby Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to boston-rubygro...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Jeremy Weiskotten

unread,
Feb 25, 2014, 9:53:08 AM2/25/14
to boston-r...@googlegroups.com, boston-r...@googlegroups.com
I think it's mostly a wash. The link_to helper offers things like the "remote" and "method" options, which can help you avoid reinventing some things. But it's easier to apply an id or class(es) in the Haml syntax.

I generally prefer using the helper, but that's probably due to force of habit more than anything else. I'd just try to be relatively consistent. The fact that a link is implemented differently than others could/should be a signal that there's something different about it. If every template is a mix of %a and link_to, I'd raise a flag. 

Parker, ICYMI the link_to helper can also take a block, which is handy for nested markup or conditional content.

Maurício Linhares

unread,
Feb 25, 2014, 9:53:28 AM2/25/14
to boston-r...@googlegroups.com
From this specific use case, there's no difference, but you could
write this link to as:

= link_to 'Feed', feed

But you can't write the %a as:

%a{href: feed} Feed

Also, calling a method instead of static HTML allows you to change the
default behavior of the code without having to track all references
throughout your codebase, if you use %a you and you wanted to change
the defaults you'd have to change them all.
-
Maurício Linhares
http://mauricio.github.io/ - http://twitter.com/#!/mauriciojr

Jesse Waites

unread,
Feb 25, 2014, 9:56:41 AM2/25/14
to boston-rubygroup
It would be interesting to view the HTML source both options generate; perhaps one is more SEO optimized as far as Google search indexing goes, over another.

It could be a waste of time, or identical and/or have no effect whatsoever, but the thought crossed my mind.
--
Jesse Waites
LinkedIn

Jeremy Weiskotten

unread,
Feb 25, 2014, 9:58:33 AM2/25/14
to boston-r...@googlegroups.com, boston-rubygroup
Spoiler: it would be a waste of time. ;)

Ben Tucker

unread,
Feb 25, 2014, 10:05:39 AM2/25/14
to boston-r...@googlegroups.com
But if you want to view some source, take a look at the implementation of link_to.  It's pretty straight forward:

Brian Cardarella

unread,
Feb 25, 2014, 3:40:27 PM2/25/14
to boston-r...@googlegroups.com
Your use-case it too simple. link_to makes use of url_for which for more complex situations will offer a better abstraction than the pure HAML version. In many cases it is cleaner to not use the path helpers and rely upon the array syntax for complex route generation, which delegates to url_for.

In addition, if you need to modify the link_to method you have a simple injection point to modify this application-wide. Using the HAML version means you will have to go to each %a one-by-one and modify which is not ideal.

Brian Cardarella

unread,
Feb 25, 2014, 3:42:00 PM2/25/14
to boston-r...@googlegroups.com
I see that Mauricio beat me to the right answer. So... ditto?
Reply all
Reply to author
Forward
0 new messages