我为什么要使用content_tag而不是“regular”html?

时间:2020-12-21 15:08:50

I know that there is an option to use the content_tag function in ruby-on-rails, which helps to generate an html tag. Some rails developers in a company I work with told me that this is the "convenient and proper" way and I should not write a "native" html in order to generate a div for example... Is it true? Is it a kind of rails standard? Does it have something to do with performance issues or render speed?

我知道有一个选项可以在ruby-on-rails中使用content_tag函数,这有助于生成一个html标记。我工作的公司的一些铁轨开发人员告诉我,这是“方便和正确”的方式,我不应该写一个“本机”html,以便生成一个div例如......这是真的吗?它是一种铁轨标准吗?它是否与性能问题或渲染速度有关?

I attach the codes for my previous code:

我附上我之前代码的代码:

<div class="alert alert-<%= key %>"><%= value %></div>  

and the rails function usage

和rails功能使用

<%= content_tag(:div, value, class: "alert alert-#{key}") %>

The first looks to me pretty, understandable and intuitive - more than the second code.. What do you think about that?

第一个看起来很漂亮,可以理解和直观 - 比第二个代码更多..你怎么看待这个?

3 个解决方案

#1


8  

Being able to use the content_tag helps when you are programmatically generating HTML inside of ruby code, e.g.: helpers and presenters.

当您以编程方式在ruby代码中生成HTML时,能够使用content_tag会有所帮助,例如:帮助者和演示者。

In my opinion, I would not normally use content_tags in the layouts and templates of a project -- I don't think it helps coding readability. I do not see any gains for productivity or performance here.

在我看来,我通常不会在项目的布局和模板中使用content_tags - 我认为它不会有助于编码可读性。我认为这里的生产力或性能没有任何好处。

HOWEVER: word of advice: if that's what your team has standardized on -- go with the team.

然而:建议:如果这是你的团队标准化的 - 与团队一起去。

#2


2  

In addition to Project and Rails standards, Security is one more reason why one must use content_tag.

除了Project和Rails标准之外,安全性还是必须使用content_tag的另一个原因。

Two quick reason why I feel content_tag is better

我觉得content_tag更好的两个原因很简单

  1. View file of Rails is in Embedded Ruby (meaning) HTML page generated from Ruby.Hence, helper method like content_tag serves the purpose of helping you to generate HTML from ERB.

    Rails的查看文件位于嵌入式Ruby中(含义)从Ruby.Hence生成的HTML页面,content_tag等辅助方法用于帮助您从ERB生成HTML。

  2. When hardcoded directly with HTML (native style), it is prone to Cross Site Scripting attack (XSS).

    当直接使用HTML(本机样式)进行硬编码时,很容易发生跨站点脚本攻击(XSS)。

#3


1  

It's a helper method to attempt to simplify HTML generation for you. You pass in what you want to use and it spits out the HTML for it. It basically builds it for you, though I never use them, I'd rather write my own because it takes just as long as typing out the helper method call :) @Jesse good call, if this is what your team is using, stick with the convention

这是尝试简化HTML生成的辅助方法。你传递了你想要使用的东西,并为它吐出HTML。它基本上是为你构建的,虽然我从不使用它们,我宁愿自己编写,因为它只需要输入帮助方法调用就可以了:) @Jesse很好的调用,如果你的团队正在使用它,那就坚持下去吧与大会

#1


8  

Being able to use the content_tag helps when you are programmatically generating HTML inside of ruby code, e.g.: helpers and presenters.

当您以编程方式在ruby代码中生成HTML时,能够使用content_tag会有所帮助,例如:帮助者和演示者。

In my opinion, I would not normally use content_tags in the layouts and templates of a project -- I don't think it helps coding readability. I do not see any gains for productivity or performance here.

在我看来,我通常不会在项目的布局和模板中使用content_tags - 我认为它不会有助于编码可读性。我认为这里的生产力或性能没有任何好处。

HOWEVER: word of advice: if that's what your team has standardized on -- go with the team.

然而:建议:如果这是你的团队标准化的 - 与团队一起去。

#2


2  

In addition to Project and Rails standards, Security is one more reason why one must use content_tag.

除了Project和Rails标准之外,安全性还是必须使用content_tag的另一个原因。

Two quick reason why I feel content_tag is better

我觉得content_tag更好的两个原因很简单

  1. View file of Rails is in Embedded Ruby (meaning) HTML page generated from Ruby.Hence, helper method like content_tag serves the purpose of helping you to generate HTML from ERB.

    Rails的查看文件位于嵌入式Ruby中(含义)从Ruby.Hence生成的HTML页面,content_tag等辅助方法用于帮助您从ERB生成HTML。

  2. When hardcoded directly with HTML (native style), it is prone to Cross Site Scripting attack (XSS).

    当直接使用HTML(本机样式)进行硬编码时,很容易发生跨站点脚本攻击(XSS)。

#3


1  

It's a helper method to attempt to simplify HTML generation for you. You pass in what you want to use and it spits out the HTML for it. It basically builds it for you, though I never use them, I'd rather write my own because it takes just as long as typing out the helper method call :) @Jesse good call, if this is what your team is using, stick with the convention

这是尝试简化HTML生成的辅助方法。你传递了你想要使用的东西,并为它吐出HTML。它基本上是为你构建的,虽然我从不使用它们,我宁愿自己编写,因为它只需要输入帮助方法调用就可以了:) @Jesse很好的调用,如果你的团队正在使用它,那就坚持下去吧与大会