33

Often while coding view templates in html, my habit of adding some helpful comments causes lots of time-consuming effort while testing.

Consider this code...

<!-- Here starts the sidebar -->
<div id="sidebar">
....
</div>

<!-- Here starts the main contents pane -->
<div id="main-contents">
...
</div>

<!-- Here starts the footer -->
<div id="footer">
...
</div>

Now, if I have to hide out some portion of the view template, in case of php I would just select the desired code and put single-line comments (using a shortcut key most of the times).

However, in html code, where only the block comments work, I end-up removing all the closing comment tags (-->) till the position I want the commenting to occur - something like this...

<!-- Here starts the sidebar
<div id="sidebar">
....
</div>

<!-- Here starts the main contents pane
<div id="main-contents">
...
</div>

<!-- Here starts the footer
<div id="footer">
...
</div>-->

Then when I'm done testing I have to go through the agony of putting back those closing tags.

Is there a better and time saving way of block commenting in HTML?

vikmalhotra
  • 9,981
  • 20
  • 97
  • 137

11 Answers11

27

Yes, to comment structural metadata out,

Using <script>/* ... */</script> in .html

Comment out large sections of HTML (Comment Out Block)

my personal way in a .html file is opening: <script>/* and close it with */</script>

<script>/* hiding code go here */</script>

Is a workaround to the problem since is not HTML.

Considering your code in .html...

  <!-- Here starts the sidebar -->
  <div id="sidebar">
  ....
  </div>

<script>/*
  <!-- Here starts the main contents pane -->
  <div id="main-contents">
  ...
  </div>

  <!-- Here starts the footer -->
  <div id="footer">
  ...
  </div>
*/</script>

And in a case is HTML inside PHP file using comment tag <?/* or <?php /* and close it with */?> . Remember that the file must be .php extension and don't work in .html.

<?/* hiding code go here */?>

Considering your code in .php...

  <!-- Here starts the sidebar -->
  <div id="sidebar">
  ....
  </div>

<?/*
  <!-- Here starts the main contents pane -->
  <div id="main-contents">
  ...
  </div>

  <!-- Here starts the footer -->
  <div id="footer">
  ...
  </div>
*/?>

Is worth nothing that is not HTML but a common developer practice is to comment out parts of metadata so that it will not be rendered and/or executed in the browser. In HTML, commenting out multiple lines can be time-consuming. It is useful to exclude pieces of template structural metadata containing comments, CSS or code and systematically commenting out to find the source of an error. It is considered a bad practice to comment blocks out and it is recommended to use a version control system. The attribute "type" is required in HTML4 and optional in HTML5.

Alan Mattano
  • 860
  • 3
  • 14
  • 22
11

Depends on the extension. If it's .html, you can use <? to start and ?> to end a comment. That's really the only alternative that I can think of. http://jsfiddle.net/SuEAW/

Robert
  • 21,110
  • 9
  • 55
  • 65
2

you can try to replace --> with a different string say, #END# and do search and replace with your editor when you wish to return the closing tags.

u11
  • 97
  • 1
  • 11
0

Put a space between the "-->" of your header comments. e.g. "- ->"

0

I find this to be the bane of XML style document commenting too. There are XML editors like eclipse that can perform block commenting. Basically automatically add extra per line and remove them. May be they made it purposefully hard to comment that style of document it was supposed to be self explanatory with the tags after all.

Community
  • 1
  • 1
whatnick
  • 5,400
  • 3
  • 19
  • 35
-1

Eclipse Juno has a good way for it. You just do the cmd+/

scrblnrd3
  • 7,228
  • 9
  • 33
  • 64
-1

The following works well in a .php file.

<php? /*your block you want commented out*/ ?>
dstrong
  • 7
  • 2
-1

My view templates are generally .php files. This is what I would be using for now.

<?php // Some comment here ?>

The solution is quite similar to what @Robert suggested, works for me. Is not very clean I guess.

vikmalhotra
  • 9,981
  • 20
  • 97
  • 137
-2

No. Unless you find a tool that does what you described for you.

David
  • 2,770
  • 5
  • 35
  • 43
-3

Depending on your editor, this should be a fairly easy macro to write.

  • Go to beginning of line or highlighted area
  • Insert <!--
  • Go to end of line or highlighted area
  • Insert -->

Another macro to reverse these steps, and you are done.

Edit: this simplistic approach does not handle nested comment tags, but should make the commenting/uncommenting easier in the general case.

Jeff Knecht
  • 2,518
  • 1
  • 14
  • 12
  • 2
    That doesn't work, because any `-->` in between will end the comment, which is not what he wants. He wants to easily be able to comment out a block, *without* having to manually delete all of the `-->` in between, and then re-add them at the end. – Brian Campbell Sep 21 '10 at 04:11
-7

/* (opener) */ (closer)

for example,

<html>
 /*<p>Commented P Tag </p>*/
<html>
user2280232
  • 155
  • 2
  • 8
  • 3
    No, this will not work. Those characters will still show up in the text. see the other answers here. – AndyG Mar 22 '14 at 03:04