Remember, It’s a Template

Eric Anderson
4 min readSep 27, 2016

--

Helpers (as well as their similar cousins decorators and presenters) are useful in Rails, but sometimes we can go a little overboard. It’s important to remember, we are writing a template. Templates are designed to make it easier to change a site design by separating the code from the HTML markup. Excessive helper usage goes against this goal since we are swapping markup for code. Sometimes the conciseness of the helper makes its usage a worthy trade-off. Its important to make sure you are getting something for that helper.

This article is Rails specific, but the principal can be applied to any system using templates and helpers.

A Simple Example

`link_to` is one of the most common helpers in Rails. It’s simple to use:

<%= link_to 'Edit Profile', edit_profile_path %>

Seems easy enough. But are we really getting any advantage by using `link_to`? What happens if we make our template more markup focused?

<a href="<%= edit_profile_path %>">Edit Profile</a>

I are still using a helper. The `edit_profile_path` url helper is too useful to drop as it ties into the routing system. It’s cost is worth the increase in Ruby code. But is the `link_to` worth it?

The second example is slightly longer but not significantly so. More importantly, the second example looks like a template. Someone who doesn’t know Rails very well, but can hack HTML would be comfortable editing it, the first example is more scary to someone who doesn’t know Rails as well.

Adding Attributes

You might look at the previous examples and feel both are easy to read and modify. How about when we start adding attributes?

<%= link_to 'Edit Profile', edit_profile_path,
class: 'button', data: {remote: true} %>

Now lets make it more markup focused:

<a href="<%= edit_profile_path %>"
class="button" data-remote="true">Edit Profile</a>

Interestingly enough, the markup focused example is actually shorter by a few characters (ignoring whitespace used for Medium formatting).

More importantly, the template is more HTML focused and less Ruby focused. This is the goal of templates. They are focused on markup, not code. Although the first example might be fairly clear to a Ruby programmer the second example is clearer to a broader audience.

Nested Elements

Lets look at nested elements. Say we want our link to also contain an icon. Here is the helper version:

<%= link_to edit_profile_path do %>
<%= image_tag 'profile.png', alt: '' %>
Edit Profile
<% end %>

Now lets look at a version that looks more markup focused:

<a href="<%= edit_profile_path %">
<img src="<%= image_path 'profile.png' %> alt="">
Edit Profile
</a>

I choose to use the `image_path` helper to tie into the asset pipeline. If I was not using the asset pipeline I might just put the raw path to the image for the `src` attribute. As Ruby coders, we might look at the first example and find it fairly readable. But the audience of templates are not always Ruby coders. The template is meant for HTML design. Even if your designers are very Ruby friendly the separation of code from template is just good architecture.

All In

Still not convinced? Let’s combine the two. Nested tags and attributes. Real world applications are made up of complex markup. This is an actual example from a real application. The images are user data managed by Dragonfly. It displays a thumbnail but links to the full-size version in a popup. First the helper version:

<%= link_to content.image.url, rel: 'shadowbox;player=img' do %>
<%= image_tag content.image.thumb('250x').url,
alt: content.alt_text, class: 'right' %>
<% end %>

Now for the version that is more markup focused:

<a href="<%= content.image.url %>" rel="shadowbox;player=img">
<img src="<%= content.image.thumb('250x').url %>"
alt="<%= content.alt_text %>" class="right">
</a>

Which do you think a HTML designer who only has slight knowledge of Ruby would understand easier? Which would be easier for that person to add another attribute to the markup? The idea of ERB (and other template languages like it) are to be mostly markup with scattered bits of dynamic code. Excessive helper usage just means we are writing Ruby but with annoying <%= %> markers everywhere.

Going Helper Crazy

Is it ever ok to use lots of helpers? Sure. The goal of all code (Ruby or templates) is to express information to others. Clarity is always the primary objective. Sometimes, using lots of helpers can be clearer than lots of markup. Form related helpers often fall in that category. Feel free to use a helper if it makes your template more capable or more clear in it’s purpose. The goal of this article is just to make you think before using a helper. Ask yourself:

  • Does this helper provide me functionality I can’t get with plain markup?
  • Does this helper make my code clearer than plain markup?
  • Is the loss of separation between code and markup worth the advantages I am getting?
  • Am I using the helper just because it exists or because it is serves a purpose?

--

--