简体   繁体   中英

Avoid rendering comments in HTML

I have a lot of comments in Rails views.

How i can prevent rendering them ?

If I understand the question correctly, you're asking about Ruby/Rails comments vs HTML comments... Give this a try in your view:

<!-- This is an HTML comment and will show up in the HTML source! -->

Now try this:

<%# This is a comment that won't show up in the HTML source! %>
<%# 
    You can even use this for commenting multiple lines!
    How useful!
%>

Does that help?

use = begin和= end标记评论的开头和结尾

There is no easy way to do that. You can monkey patch ERB sources, perhaps, but it is a bit nerdy.

I'm not a Rails programmer, but a quick but of Binging brought up this link: http://blog.brijeshshah.com/strip-tags-in-rails-javascript-and-php/

The approach he's using is one that I've used in the past where you sanitize the view's output. sanitize being the name of the function you want to use before rendering the view.

也许你可以使用Haml 评论: -允许评论你的haml代码,而不会出现在生成的html中。

Kind of hackish, but you can wrap it in a helper method

In your view:

<% comment do %>
  <%= "this won't be executed" %>
  or displayed
<% end %>

in app/helpers/application_helper.rb

module ApplicationHelper
  def comment(&block)
    ## you can do something with the block if you want
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM