Ruby-on-rails 在 rails 中阻止 html.erb 模板中的注释

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3127644/
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-02 22:49:34  来源:igfitidea点击:

Block comments in html.erb templates in rails

ruby-on-railsrubyerb

提问by Nikita Rybak

How do you comment out html mixed with ruby code?

你如何注释掉与 ruby​​ 代码混合的 html?

some text <% ... %> more text <%= ... %>
something else
<% ... %>

In jsp it's real simple: <%-- ... --%>, but I'm unable to find any concise option in rails.

在jsp 中它真的很简单: <%-- ... --%>,但我无法在rails 中找到任何简洁的选项。

Simple html comments <!-- ... -->do not work: ruby code is still executed and yells errors.

简单的 html 注释<!-- ... -->不起作用:仍然执行 ruby​​ 代码并发出错误提示。

There's an option to use if falsewith html comments, but it's quite verbose, not to mention IDEs doesn't support it.

有一个选项可用于if falsehtml 注释,但它非常冗长,更不用说 IDE 不支持它。

There's also an option coming from pure ruby, which surprisingly works.

还有一个来自纯红宝石的选项,它出人意料地有效。

<%
=begin %>
... html and ruby code goes here
<%
=end %>

It's generally fine, except that it's verbose, weird-looking and none of ruby IDEs I know support it (yep, I like to comment/comment-out with one keystroke).

它通常很好,除了它冗长,看起来很奇怪,而且我知道没有一个 ruby​​ IDE 支持它(是的,我喜欢用一个键来评论/注释掉)。

I'm curious, is there any 'official' of doing this in rails?

我很好奇,在 Rails 中是否有任何“官方”这样做?

Thanks!

谢谢!

采纳答案by Chubas

I wouldn't count as a solution, but perhaps enclosing the chunk between an

我不会算作解决方案,但也许将块包含在

<% if false %>
   ...
<% end %>

or if you feel a little dirty, create a helper that simply outputs nothing.

或者如果你觉得有点脏,创建一个不输出任何东西的助手。

I've never needed it, but I'm stumbled there seems to be no out-of-the-box solution for this.

我从来不需要它,但我偶然发现似乎没有现成的解决方案。

回答by Garfield

Use this for commenting single lines:

使用它来注释单行:

<%# your_ruby_code %>

For multiple lines, the

对于多行,

<% 
=begin %>  <% ruby_code %>
<% 
=end %>

What you said would work.

你说的会奏效。

回答by sumizome

The =beginapproach is annoying because:

这种=begin方法很烦人,因为:

  1. It doesn't work for mixed HTML and Ruby (or just HTML) that's on a single line
  2. It's annoying to type
  1. 它不适用于单行的混合 HTML 和 Ruby(或仅 HTML)
  2. 打字很烦

The <% if false %>approach works, but it looks weird and doesn't give anyone else who looks at your code a hint about your intentions.

<% if false %>方法有效,但它看起来很奇怪,并且不会向查看您代码的任何其他人暗示您的意图。

My solution is as follows:

我的解决方案如下:

In application_helper.rb, add a method so:

在 中application_helper.rb,添加一个方法,以便:

def comment
end

Then in your view template, you can say:

然后在您的视图模板中,您可以说:

<% comment do %>Some stuff that won't be rendered...<% end %>

This works because any Ruby method can take a block, but will silently ignore the passed-in block if your method doesn't include a yield.

这是有效的,因为任何 Ruby 方法都可以接受一个块,但如果您的方法不包含yield.

回答by Piotr Turek

<%#=

...commented
multiline
block...

%>

回答by klenwell

For block comments in templates, my text editor (Komodo) finds this variation on @Garfield's recommendationleast obnoxious:

对于模板中的块注释,我的文本编辑器 (Komodo) 发现@Garfield 的建议中的这种变化最不令人讨厌:

<%# A long multiline comment in a rails template ...
  # line 2
  # and so on ... 
  # %>

回答by jamesc

To comment out erb tags use the ruby comment hash symbol before the = sign in the opening tag

要注释掉 erb 标签,请在开始标签中的 = 符号之前使用 ruby​​ 注释哈希符号

<p>
 This is some text I want to keep
 <%= @some_object.some_attribute %>
</p>
<p>
  I want to keep this text but comment out the erb tag
  <%#= @some_object.another_attribute %>
</p>
<!--
<p>
  I want all of this text commented out including the erb tag
  <%#= @some_object.some_attribute %>
</p>
-->
<!--
<p>
 I just want this html commented out but I want to keep the erb tag
 <%= @some_object.some_attribute %>
</p>
-->

回答by Sagar Ranglani

Since you can use <% %>to put a ruby block, it can be certainly used to put in comments into it.

既然可以<% %>用来放入 ruby​​ 块,当然也可以用来在里面放入注释。

A simpler and elegant solution would look like...

一个更简单优雅的解决方案看起来像......

<%
# See! I am a Ruby Comment
# And I am multi-line
# I look like a recognizable ruby comment block too
# and not so complex
# The only drawback with me is the Hash symbol you have to repeat
# But it's the norm, isn't it?
%>

回答by Michal

After =begin you do not need to put %>

在 =begin 之后你不需要放 %>

<%
=begin

code code code code code code 
code code code code code code 
code code code code code code 
code code code code code code 

=end %>

回答by ViggoV

Just an addendum to some of the previous answers. I found the =begin/=end solution most useful, but for the sake of beauty I write it like so:

只是之前一些答案的附录。我发现 =begin/=end 解决方案最有用,但为了美观,我这样写:

<%
=begin
  <p>HTML will be ignored</p>
  <%= 'and so will ruby' %>
  <p>
    <%= 'plus the whole block will be greyed in editor' %>
  </p>
=end
%>

Note that since everything is ignored until the =endthere is no need to close the =begintag with %>or open the =endtag with <%(which has also been pointed out in an earlier answer)

请注意,由于所有内容都被忽略,直到=end不需要关闭=begin标签%>或打开=end标签<%(这也已在之前的答案中指出)

I found this to be the most elegant solution to completely outcomment a block of mixed ruby and html code and have it greyed out in my editor as well, as opposed to the <% if false %>solution. Only drawback is that =beginand =endmust be placed at the very beginning of the line..

我发现这是最优雅的解决方案,可以完全注释一块混合的 ruby​​ 和 html 代码,并在我的编辑器中将其显示为灰色,而不是<% if false %>解决方案。唯一的缺点是=begin并且=end必须放在行的最开始。

回答by stillwaiting

You can use both <%if false%> and HTML comments at the same time:

你可以同时使用 <%if false%> 和 HTML 注释:

<%if false%><--

stuff to comment out

--><%end%>

The benefits are:

好处是:

  • Ruby code is not executed

  • The commented block has gray color in IDE

  • The intention is obvious for other developers

  • Ruby 代码未执行

  • 注释块在 IDE 中为灰色

  • 其他开发者的意图很明显