Ruby-on-rails erb、haml 或 slim:你推荐哪一个?为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11390512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 03:39:37  来源:igfitidea点击:

erb, haml or slim: which one do you suggest? And why?

ruby-on-railshamlerbslim-lang

提问by Omid Kamangar

I am learning Rails and I have seen these template engines. I have no experience with them (only erb).

我正在学习 Rails,我见过这些模板引擎。我对他们没有经验(只有erb)。

But as I am a beginner, I am really confused. Which one do you suggest and why? Erb, Haml or Slim? Please tell your reason for preferring one over the others. And if you have any other recommendations, please let us know.

但是因为我是初学者,我真的很困惑。你推荐哪一个,为什么?Erb、Haml 还是 Slim?请说出您偏爱其中一种的原因。如果您有任何其他建议,请告诉我们。

EDIT: I am NOT looking for a winner here. I just want to hear your opinions about them, their syntax, speed of execution, and so on.

编辑:我不是在这里寻找赢家。我只是想听听你对它们的看法、它们的语法、执行速度等等。

采纳答案by Erez Rabih

ERB is good mainly if you have a web designer that will work on plain HTML and does not know either haml or slim. This way he can write HTML and you can embed ruby logic with the proper tags.

ERB 是好的,主要是如果您有一个可以处理纯 HTML 并且不知道 haml 或 slim 的网页设计师。通过这种方式,他可以编写 HTML,而您可以将 ruby​​ 逻辑嵌入到适当的标签中。

If you work on both HTML and ruby logic, or your designer is ready to learn something new (like HAML) I'd go for HAML. It is a lot more ruby-friendly, reduces char count by much and a lot more readable than ERB.

如果你同时使用 HTML 和 ruby​​ 逻辑,或者你的设计师准备学习新的东西(比如 HAML),我会选择 HAML。它对 ruby​​ 更友好,比 ERB 减少了更多的字符数并且更具可读性。

For example (taken from official HAML site):

例如(取自官方HAML 站点):

In ERB your view will look like this:

在 ERB 中,您的视图将如下所示:

<div id="profile">
  <div class="left column">
    <div id="date"><%= print_date %></div>
    <div id="address"><%= current_user.address %></div>
  </div>
  <div class="right column">
    <div id="email"><%= current_user.email %></div>
    <div id="bio"><%= current_user.bio %></div>
  </div>
</div>

While in HAML it will look like this:

在 HAML 中,它看起来像这样:

#profile
  .left.column
    #date= print_date
    #address= current_user.address
  .right.column
    #email= current_user.email
    #bio= current_user.bio

A lot cleaner!

干净多了!

As for the difference between HAML and SLIM - I never really worked with SLIM but I guess it is a matter of taste - take a look at both syntaxes and decide which looks better in you eyes. I don't think there is a definite winner between those two (HAML/SLIM).

至于 HAML 和 SLIM 之间的区别——我从来没有真正使用过 SLIM,但我想这是一个品味问题——看看这两种语法,然后决定哪个在你眼中看起来更好。我认为这两者之间没有明确的赢家(HAML/SLIM)。

回答by Bart ten Brinke

Two big advantages of using slim over haml:

与 Haml 相比,使用 slim 的两大优势:

  1. Slim is currently about eight times faster than haml.

  2. Slim supports HTTP streaming, while HAML doesn't.

  3. Slim has a more natural syntax: a href="foo.html"

  1. Slim 目前比 haml 快约八倍。

  2. Slim 支持 HTTP 流,而 HAML 不支持。

  3. Slim 有一个更自然的语法: a href="foo.html"

回答by engineerDave

Off the top of my head this is what I came up with

这就是我想到的

ERB:

再培训局

Pros

优点

  • default out of the box
  • not white space dependent
  • lowest barrier of entry (if coming from HTML) as its HTML with Ruby code sprinkled in
  • most IDE's lexers read it by default
  • DHH prefers it
  • legacy apps are probably still using it
  • 默认开箱即用
  • 不依赖于空白
  • 最低的进入门槛(如果来自 HTML),因为它的 HTML 带有 Ruby 代码
  • 大多数 IDE 的词法分析器默认读取它
  • DHH 更喜欢它
  • 遗留应用程序可能仍在使用它

Cons

缺点

  • more verbose
  • content_for tags in helpers and views can get out of hand quickly
  • content_for tags makes nesting tags harder as erb only returns the last line in the block. so you have to append to a string and then return that.
  • 更详细
  • 助手和视图中的 content_for 标签很快就会失控
  • content_for 标签使嵌套标签更加困难,因为 erb 只返回块中的最后一行。所以你必须附加到一个字符串然后返回它。

HAML

反洗钱

Pros

优点

  • more concise. no closing tags, fits in smaller screens
  • visually cleaner structure
  • has built in helpers (haml_concat, haml_capture) to utilize haml in helper methods
  • class chaining
  • lots of useful syntactic sugar like # for divs or . for class chaining, or :javascript for JS tags
  • 更简洁。没有结束标签,适合较小的屏幕
  • 视觉上更干净的结构
  • 已内置助手(haml_concat、haml_capture)以在助手方法中使用 haml
  • 类链接
  • 许多有用的语法糖,如 # 表示 div 或 . 用于类链接,或 :javascript 用于 JS 标签

Cons

缺点

  • whitespace dependent which makes for some hard errors to figure out at times
  • complex tags usually need to resort to "hash" format. (Although I actually think this is a great example of flexibility to someone starting out it could be a pain.)
  • added as a gem (again probably a stretch to put this as a con)
  • designers might have some trouble adjusting
  • in addition to the general whitespace warning... simple whitespace errors eg. tabs and spaces for indentation, can cause pages to err in production which normal specs/test won't catch. Moral: Expect greater need for view tests and possibly don't use haml for mission critical views, unless you're sure that your tests are testing the actual rendering of the view.
  • is slower (than erb)
    • caveat:this is ruby code we're talking about if speed is a blocking issue in your application there are alternatives to ruby, e.g. haskell
  • 空白依赖,这有时会导致一些硬错误
  • 复杂的标签通常需要采用“散列”格式。(虽然我实际上认为这是一个很好的灵活性例子,对于刚开始的人来说可能会很痛苦。)
  • 添加为宝石(再次将其作为骗局可能有点牵强)
  • 设计师可能难以调整
  • 除了一般的空白警告......简单的空白错误,例如。用于缩进的制表符和空格可能会导致页面在生产中出错,而正常的规范/测试不会捕捉到这些错误。道德:期望对视图测试有更大的需求,并且可能不要将 haml 用于关键任务视图,除非您确定您的测试正在测试视图的实际呈现。
  • 比 erb 慢
    • 警告:这是我们正在讨论的 ruby​​ 代码,如果速度是您的应用程序中的阻塞问题,则有 ruby​​ 的替代方案,例如 haskell

回答by montrealmike

The question for me comes down to would you rather put %before every tag or |before every new block of text?

对我来说,问题归结为您是愿意放在%每个标签|之前还是每个新文本块之前?

Slim:

苗条的:

 tag(attr= "value")
  | text

Haml:

哈尔:

 %tag{attr: "value"}
   text

One more thing to lookout for: haml assumes a white space between new lines (remove whitespace in haml) while slim assumes no space (Add whitespace in Slim hereand here)

还有一件事要注意:haml 假设新行之间有一个空格(删除 haml 中的空格)而 slim 假设没有空格(在此处此处在 Slim 中添加空格)

回答by ocodo

https://github.com/scalp42/hamlerbslim- is an independent benchmark which shows Slim and Erb as winners, performance wise (slim tends to reduce the HTML output size too.)

https://github.com/scalp42/hamlerbslim- 是一个独立的基准测试,它显示 Slim 和 Erb 作为赢家,性能明智(slim 也倾向于减少 HTML 输出大小。)

My personal opinion is that overall, Slim and Haml will save you time (== money) in terms of maintenance, providing you have Haml/Slim savvy people looking after your views.

我个人的观点是,总体而言,Slim 和 Haml 将在维护方面为您节省时间(== 金钱),前提是您有 Haml/Slim 精明的人来照顾您的观点。

If you don't have those people, Erb is definitely the way to go, because despite the best will in the world, there are a lot of very inexpensive people available who can work with HTML/Erb, but find Haml/Slim a complete mystery.

如果你没有这些人,Erb 绝对是你要走的路,因为尽管有世界上最好的意愿,有很多非常便宜的人可以使用 HTML/Erb,但发现 Haml/Slim 是一个完整的神秘。

Best of all cases, train these people to use Slim or at least expose them to it, and keep the numbers of the ones who "get it."

最好的情况是,培训这些人使用 Slim 或至少让他们接触它,并保留“得到它”的人数。