Block comments in html.erb templates in rails

前端 未结 16 2157
北荒
北荒 2020-12-07 09:10

How do you comment out html mixed with ruby code?

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

In js

相关标签:
16条回答
  • 2020-12-07 09:40

    The only acceptable solution I ever found to this back-breaking problem was to put a space within the "<%=" to make it no longer register as ruby code, and then comment out the whole block with html comments

    Like this:

    <!--
    <p>
      < %= @some_object.some_attribute %>
    </p>
    <p>
      < %= @some_object.another_attribute %>
    </p>
    <p>
      < %= @some_object.some_attribute %>
    </p>
    <p>
      < %= @some_object.some_attribute %>
    </p>
    -->
    

    Yes, adding the spaces is annoying. But it is the least annoying of all the solutions I've yet seen.

    0 讨论(0)
  • 2020-12-07 09:41

    The =begin approach is annoying because:

    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

    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.

    My solution is as follows:

    In application_helper.rb, add a method so:

    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.

    0 讨论(0)
  • 2020-12-07 09:41

    Use a HEREDOC called comment

    Pros:

    • Self-explanatory that this is a comment
    • Works for erb and HTML tags
    • Has ok syntax highlighting (as one long string)

    Cons:

    • Weird 3 line closing syntax
    • No keyboard shortcuts

    Code:

    The opening tag can be

    <% <<-COMMENT %>
    
    the above closing erb tag is just for looks (to match the end),
    but don't put anything else there, it may show up on the page
    

    or

    <%
    <<-COMMENT
    %>
    

    Anything here won't run or show up in the browser

    <P>
        this will not be displayed in the browser
        <strong> even in the developer's tools </strong>
    </p>
    
    <% 1_000_000_000_000.times do |count| %>
    
    for the <%= count %>'th time, this won't run a trillion times,
    this is all just a string
    
    all of these %>, <%, <% end %>, end, do, <!--, won't cause any issues.
    
    but the below opening erb tag is important (if you used any erb tags in the comment).
    I have no clue why?
    

    The closing tag

    yes it needs to be 3 lines

    0 讨论(0)
  • 2020-12-07 09:41
    <% %w(
      <span title="<%= title %>">hello</span>
    ) %>
    

    I hope I've just blown your minds!

    0 讨论(0)
  • 2020-12-07 09:45

    Sublime Text's block comment shortcut ctrl+shift+/ notices whether you've selected normal HTML or an Erb tag and puts either the <!--- or <% =begin %> accordingly.

    0 讨论(0)
  • 2020-12-07 09:46

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

    <%# A long multiline comment in a rails template ...
      # line 2
      # and so on ... 
      # %>
    
    0 讨论(0)
提交回复
热议问题