为什么 Helpers 中不应该包含 html? [英] Why shouldn't Helpers have html in them?

查看:41
本文介绍了为什么 Helpers 中不应该包含 html?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说最好不要在您的助手中实际使用任何 html;我的问题是,为什么不呢?此外,如果您尝试生成 html 列表或类似内容,我该如何避免使用实际标签?

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?

谢谢!

-fREW

推荐答案

我的建议 - 如果它是一小段 HTML(几个标签),请不要担心.不仅如此 - 考虑部分(因为在助手中将 html 字符串拉在一起是一种痛苦,而这正是视图擅长的).

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).

我经常在我的助手中包含 HTML(直接或通过调用像 link_to 这样的 Rails 方法).我的世界并没有在我周围崩溃.事实上,我想说我的代码因此非常干净、可维护和易于理解.

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.

就在昨晚,我写了一个 link_to_user 助手来吐出带有用户正常链接的 html 以及旁边的用户图标.我本可以部分完成,但我认为 link_to_user 是一种更简洁的处理方式.

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.

这篇关于为什么 Helpers 中不应该包含 html?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆