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

Nik*_*bak 113 ruby ruby-on-rails erb

你如何评论html与ruby代码混合?

some text <% ... %> more text <%= ... %>
something else
<% ... %>
Run Code Online (Sandbox Code Playgroud)

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

简单的html注释<!-- ... -->不起作用:ruby代码仍然执行并且会出错.

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

还有一种来自纯红宝石的选择,这令人惊讶地起作用.

<%
=begin %>
... html and ruby code goes here
<%
=end %>
Run Code Online (Sandbox Code Playgroud)

它通常很好,除了它冗长,看起来很奇怪,我知道没有任何ruby IDE支持它(是的,我喜欢用一次击键来评论/评论).

我很好奇,在轨道上有没有"正式"这样做?

谢谢!

Gar*_*eld 156

用它来评论单行:

<%# your_ruby_code %>
Run Code Online (Sandbox Code Playgroud)

对于多行,

<% 
=begin %>  <% ruby_code %>
<% 
=end %>
Run Code Online (Sandbox Code Playgroud)

你说的会奏效.

  • 如果=在新行的开头就像在答案中一样,它可以工作 (3认同)
  • 我知道会的,我很感兴趣,如果有更简单的:) (2认同)
  • =开始我认为开始 - 结束将是全部=结束#你可能会忽略我的评论,因为它完全评论:P (2认同)

Chu*_*bas 110

我不认为是一个解决方案,但也许可以把它放在一个块之间

<% if false %>
   ...
<% end %>
Run Code Online (Sandbox Code Playgroud)

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

我从来没有需要它,但我发现我似乎没有开箱即用的解决方案.

  • 抱歉,您应该在开头erb标签<%#%>或<%#=%>上使用#符号 (24认同)

sum*_*ome 26

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

  1. 它不适用于单行上的混合HTML和Ruby(或只是HTML)
  2. 输入很烦人

这种<% if false %>方法很有效,但它看起来很奇怪,并没有让任何看过你代码的人暗示你的意图.

我的解决方案如下:

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

def comment
end
Run Code Online (Sandbox Code Playgroud)

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

<% comment do %>Some stuff that won't be rendered...<% end %>
Run Code Online (Sandbox Code Playgroud)

这是有效的,因为任何Ruby方法都可以占用一个块,但如果你的方法不包含a,它会默默地忽略传入的块yield.

  • 您甚至可以将其写为<%comment do%> ... <%comment end%>.我已将此语法添加到sublime文本中,以便它甚至看起来像一个真正的注释. (3认同)

小智 9

<%#=

...commented
multiline
block...

%>
Run Code Online (Sandbox Code Playgroud)

  • 这个答案应该更有趣。 (3认同)

kle*_*ell 7

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

<%# A long multiline comment in a rails template ...
  # line 2
  # and so on ... 
  # %>
Run Code Online (Sandbox Code Playgroud)


jam*_*esc 6

要注释掉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>
-->
Run Code Online (Sandbox Code Playgroud)


Sag*_*ani 6

既然你可以使用<% %>ruby块,它当然可以用来放入注释.

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

<%
# 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?
%>
Run Code Online (Sandbox Code Playgroud)

  • 这不起作用.块注释中的任何ruby标记都将关闭外部块. (6认同)

Mic*_*hal 5

=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 %>
Run Code Online (Sandbox Code Playgroud)


Vig*_*goV 5

只是对之前一些答案的补充。我发现 =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
%>
Run Code Online (Sandbox Code Playgroud)

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

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

  • 我收到此错误:“嵌入文档遇到文件结尾” (2认同)