[MODX] Don’t Be a Nester

When I troubleshoot people’s sites, one thing I often notice is a tendency toward unnecessary nesting. On one site I looked at recently, there was a chunk tag in the template, inside the chunk tag was another chunk tag (and nothing else), and in that chunk, was a single snippet tag. I couldn’t see any reason at all not to put the snippet tag itself in the template, especially since it was the only template on the site.

MODX logo

Just because you can nest things in MODX doesn’t mean you should. A common mantra in the world of web programming it that one should always separate form from content. Or in other words, model from view, or data from display, or code from content. In general, this is good advice because it makes things easier to maintain.

A Bad Mixture

Here is a classic example of mixing layers in a way that will make the code difficult to maintain:

$output = "";
$output .= '<h3>User: ' . $username .  '</h3>' . '<p>Email: ' .
    $email . '</p>' . '<p>Phone: ' . $phone . '</p>';

How much cleaner and more sensible is it to put the display aspects in a chunk and let the code handle just the data?

The Chunk:

<h3>User: [[+username]]</h3>
<p>Email: [[+email]]</p>
<p>Phone: [[+phone]]</p>

The Code

$fields = (
   'username' => $username,
   'email' => $email,
   'phone' => $phone,
);
$output .= $modx->getChunk('myChunk', $fields);

The MODX getChunk() method is very fast and when you decide to change the way users are displayed, it will be very easy to modify the chunk or the CSS to improve the display without touching the code at all. Keep in mind, too, that every time you modify the PHP code, you risk temporarily breaking the site. Modifying the chunk could give you a less-than-perfect display, but it’s very unlikely to crash anything.

Too Much of a Good Thing

We can agree that in the example above, the second version is vastly better than the first. That doesn’t mean, however, that it’s a good idea to put *everything* in a chunk that can go in one. Before moving content down to another level with a MODX tag, think about whether you really have a reason to do so.

If, for example, you’re going to use a common header in multiple templates, it makes sense to put the code of that header in a chunk (though you should still ask yourself if you really need multiple templates). On the other hand, breaking down the head further may not make any real sense. I’ve seen layouts like this, for example. In the template, there’s a chunk tag for a chunk called “Header”. In the Header chunk, you find something like this:

[[$Logo]]
[[$Banner]]
[[$Copyright]]
[[$TopMenu]]

This even though the five tags could be replaced by five lines of HTML code and the TopMenu chunk has nothing but a single Wayfinder tag in it. Think about the process that occurs for the header on every page. MODX has to load the template, then find and load the Header chunk, then find and load all the individual chunks, then find and run the Wayfinder snippet. This is mitigated somewhat by having things cached, but every time you make a change to any of the parts, or the site cache is cleared the next visitor is going to have to wait.

Worse yet, when it’s a year later and you need to make some changes, you have to load the template to find out the name of the Header chunk, then load the Header chunk to find out the names of the chunks it contains, then figure out which one holds the part you want to change (assuming that it has a sensible name), then load and edit that chunk perhaps only to discover that what you thought was in the Banner is really part of the Logo or should be moved there. How much simpler to have all that code in the Header chunk instead of spread all over the map.

The example above may seem like an exaggeration, but I’ve actually seen sites where some of the chunks referenced in the Header chunk contained several chunks of their own.

Even if you could somehow justify the creation of all these separate chunks, you’d still be dealing with the fact that it takes six times as long to load six chunks as it does to load one.

Time Is of the Essence

Take a look at this NY Times article on bounce rates and page-load times.

If your page loads are too slow, users won’t stick around long enough to find out what a great site you have. And when I say too slow, the effects start at around 250 milliseconds. That’s a quarter of a second. It’s not at all unreasonable to suppose that loading five or six unnecessary chunks (let alone ten or twenty) would take longer than that on many servers.

And don’t get me started on conditional output modifiers. These were created to let people who were not able to use PHP code create conditions that determined what was shown on the page. They are notoriously slow, however. Unless your server is blindingly fast, you should either learn to write simple snippets to replace your conditional output modifiers or hire someone (like me, for instance) to replace them for you. Generally, an experienced PHP coder can replace conditional output modifiers very quickly. You only have to have them replaced once and your site will be considerably snappier from then on.

 


For more information on how to use MODX to create a web site, see my web site Bob’s Guides, or better yet, buy my book: MODX: The Official Guide.

Looking for quality MODX Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , | Posted under MODX, MODX | RSS 2.0

Author Spotlight

Bob Ray

Bob Ray

I am the author of MODX: The Official Guide and over 30 MODX add-on components. I host Bob's Guides, a source of valuable information for MODX users, and I've been very active in the MODX Forums with over 14,000 posts.

Leave a Reply

Your email address will not be published. Required fields are marked *


× 1 = 4

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>