Profile picture for user Sean Lange
Sean Lange
19 Aug 2013

This is a guest post from Sean Lange.

We know that HTML comments on your code work for eliminating the visual rendering of elements. Wrapping a section of code in between the HTML comments signs (<!--<p>this</p>--> for example) alter the display only. The code is still rendered in the DOM with the "comment" syntax. Most of the time though, no big deal.

However, when you are working with something like Drupal, and you are using PHP statements, there's something you should consider. If you include any PHP within the block of HTML code that you comment out, it will still get processed. Which means that something like this:

<!-- <div>
  <?php if (!empty($title)): ?>
    <h2><?php print $title; ?></h2>
  <?php endif; ?>
</div> -->

will get processed and rendered as commented out HTML in the DOM like so:

<!-- <div><h2>This is the Title</h2></div> -->

Further confirmation of the potential danger of this was discovered when I tested using dsm() which executed. Placing it within HTML comments, I was thinking that it would NOT execute. What do I mean? If you place the following code in your PHP file it will execute and display on the page:

<!-- <div><?php dsm('does this run?'; ?></div> -->

So what's the problem?

The problem with this is that you may end up having errors being logged for undefined variables, debugging code being run when you thought it wouldn't and even having resources spent on calculating and printing DOM elements that don't need to be there. And the solution?

Your safest bet for working in .tpl files and ensuring code does not render to the screen would be to use PHP to comment out the block of code. This will ensure that nothing within the block of code will execute via php or render to the screen.

<?php /*><div>
  <?php if (!empty($title)): ?>
    <h2><?php print $title; ?></h2>
  <?php endif; ?>
</div><?php */;>

This way we can leave HTML comments for small informative DOM helpers as needed.

<div>
  <?php if (!empty($title)): ?>
    <h2><?php print $title; ?></h2>
  <?php endif; ?>
</div> <!-- /title goes here -->

So hopefully this reminder will help you avoid any of the issues above and will encourage you to use proper commenting. Keep in mind the exact purpose and effect of each commenting system.

Profile picture for user Sean Lange

Sean Lange

Front-end developer

Sean Lange is a front-end developer @ Lullabot. Tackling each day with a love and respect for the challenging and ever changing landscape of front-end development. And forever distracted by backend development and all things shiny, tech-y, gizmo-ish, and/or widget-y.

Contact us

Comments

Johnd249 14 May 2014 04:13

You made some really good

You made some really good points there. I checked on the internet for additional information about the issue and found most people will go along with your views on this site. deccfkggccba

Jim 20 Jun 2017 12:06

Thanks

Explained what was happening to my code when I

it out.
Thanks

Add new comment