MODX Quick Tip: MODX Commenting - Why & How

See the latest update.

The Problem

When building websites, sometimes you put markup on the page that you subsequently don’t need. You could just hit the "Delete" button, but this is the Web - things change. What if you want to use that bit of code later?

Well, we’re all familiar with HTML comment tags. The browser doesn’t render anything within the tags, so it’s useful for temporarily hiding deprecated code. But you do that for a template here, a chunk there…then when you Go Live, you have two choices:

  1. Hunt through the site for all your comments, or
  2. Every visitor to the site will be downloading your deprecated code for the rest of eternity.

Neither choice is pleasant. Now there’s a better way…

Be Lazy…with MODX Revolution

In one of the latest versions of MODX Revolution (not sure which one, you can read the changelog and let us know ;) the MODX comment tag was introduced:

[[- Anything you put in here won't be processed, parsed, or output at all. MODX simply skips it.]] 

This saves the MODX parser from dealing with the content, so the client never sees it. If you were lazy or forgetful on Go Live and left these comments in, the overhead would be minimal, if any. I’m not saying this is best practice, but MODX gives you Freedom, my friend. Freedom, in this case, to be Lazy.

If you’re going to be lazy, do it with awesomeness - do it the MODX way :)

UPDATE: An example where using MODX comment syntax is essential!
<!-- we don't want this menu anymore, let's hide it with html comments
[[!Wayfinder? &startId=`0` &level=`10` ...]]
by the way, it still gets served up in the markup -->  

I’ve seen this is a LOT. So you have html comments around that huge menu, but guess what? MODX has to process that menu anyways. If you have a slow-loading site, look first at global elements and get rid of unnecessary processing. Any processing that happens inside HTML comments will never be seen, and therefore only serves to waste electricity. Try this instead:

[[- we don't even want to render, nor process this menu
[[-!Wayfinder? &startId=`0` &level=`10` ...]]
we also comment out the nested Wayfinder tag, just to be safe.
MODX comment syntax is rad!
]]

Actually Jason (Chief Architect here at MODX) just informed me that as soon as the parser comes across a comment token “-” it returns “”, even before it tries to collect nested tags. So really you don’t need to comment nested tags like I’ve done in the example. But then habits die hard. I might continue to do it just cause the Ace code editor color codes it correctly that way ;)