Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why shouldn't Helpers have html in them?

I have heard that it's best not to actually have any html in your helpers; my question is, Why not? And furthermore, if you were trying to generate an html list or something like that, how can I avoid actual tags?

Thanks!

-fREW

like image 891
Frew Schmidt Avatar asked Sep 25 '08 15:09

Frew Schmidt


People also ask

Is HTML more accessible than PDF?

Compared with HTML content, information published in a PDF is harder to find, use and maintain. More importantly, unless created with sufficient care PDFs can often be bad for accessibility and rarely comply with open standards. Many departments are doing great work to move away from them.

Are HTML documents accessible?

What is usually meant is that HTML is inherently accessible, and in a strictly limited sense, that's pretty much the case. Simply remove any CSS and inline styling and you've got plain text and markup. At least within the page content itself, text and graphics are generally in more-or-less the right order.

Should I Use main tag HTML?

Definition and UsageThe <main> tag specifies the main content of a document. The content inside the <main> element should be unique to the document. It should not contain any content that is repeated across documents such as sidebars, navigation links, copyright information, site logos, and search forms.

How do screen readers read HTML?

Screen readers can't “read” images. Instead they read the alternate text provided through the HTML tag. This alternate text should give an accurate and clear description of the image's contents. If there is no alternate text provided, the screen reader will read “graphic”.


2 Answers

My advice - if it's small pieces of HTML (a couple of tags) don't worry about it. More than that - think about partials (as pulling strings of html together in a helper is a pain that's what the views are good at).

I regularly include HTML in my helpers (either directly or through calls to Rails methods like link_to). My world has not come crashing down around me. In fact I'd to so far as to say my code is very clean, maintainable and understandable because of it.

Only last night I wrote a link_to_user helper to spits out html with normal link to the user along with the user's icon next to it. I could have done it in a partial, but I think link_to_user is a much cleaner way to handle it.

like image 75
RichH Avatar answered Oct 21 '22 04:10

RichH


I don't see that there's anything wrong with it. The majority of the rails helpers generate HTML code (which is their purpose) - to me this implies that's what you're supposed to do yourself.

There is however the ever-present issue of code readability. If you have a helper which just builds a big string of raw HTML, then it's going to be hard to understand. While it's fine to generate HTML in helpers, you should do it using things like content_tag, and render :partial rather than just return %Q(<a href="#{something}">#{text}>)

like image 25
Orion Edwards Avatar answered Oct 21 '22 05:10

Orion Edwards