Tags as the Result or How Conditionals are like Mosquitoes

Mosquito by Nick Ares

Photo Credit: Nick Ares

Mosquitoes don't serve any useful purpose; they should be eradicated from the planet!

I've heard this or similar feelings expressed since I was a child. I've even felt this way myself after a particularly bad night outdoors leading to hours of painful itchiness. As annoying as they are, carrying dreadful disease, killing a great number of human beings every year, surely it would be a better world without mosquitoes buzzing around trying to find some blood to suck. Even a number of scientists argue that eradicating them may have some collateral damage, but assert that life would find a way to fill the gap left behind.

But who likes collateral damage, especially uncontrolled? No one really knows what the true scope of that damage would be without taking what boils down to an unnecessary risk. Considering the controversy, difficulty, high-cost, and unpredictability of trying to eradicate what we don't like, the best alternative in my estimation, is arming ourselves with knowledge against the known threats.

How Conditionals are like Mosquitoes

I've proudly expressed my disdain for using conditional tags or filters in MODX content since before MODX was founded. But, like carefully considering the consequences of eradicating our friend the mosquito, several experiences collaborating with MODX users on optimization projects made me realize they can be very useful tools. I also started to realize most of my objections to them were in the way they were being used in the presentation layer, leading to unnecessary performance problems.

So now, instead of just continuing to hope for their expedient eradication, I'd like to share some explicit details on how you can best use them, and why. Armed with a little knowledge of their potential pitfalls, you can make effective use of conditionals in your presentation layer while maintaining performance of the Resources you use them in.

Practical Protection from the Blood Suckers

Air Force sprays for mosquitoes after Hurricane Ike by U.S. Air Force Airman 1st Class Chad Kellum

Photo Credit: U.S. Air Force photo/Airman 1st Class Chad Kellum

Since we are not going to send a fleet of aircraft to spray toxic chemicals on our site to rid it of the annoying conditionals, making ourselves sick in the process, we must first identify what the actual threats of co-existing with them are. The disease conditionals most often bring to our sites is called performance degradation.

While this may seem like a potentially fatal disease, especially in sites that might already suffer similar ailments from other sources, there is actually a fairly easy way to avoid it's symptoms. It boils down to one simple concept that takes advantage of the MODX parsing engine's strengths, while avoiding it's weaknesses.

Build Tags As a Result of a Conditional, Not in Conditional Results

Yep, it's as simple as that.

Often times I will see content in MODX that contains the use of conditionals and cringe when I see it. Why? Because more often than not, there will be a complete tag located in both the then and else results. And why is this bad? Because regardless of which result is selected by the conditional assertion being made, MODX will still process the tags in both conditional paths before even considering the the result of the logical comparison.

This may not sound that bad, and in some cases it causes no real noticeable problem. But in too many cases, this can severely reduce performance without leaving any clues as to why. I first realized the potential scope of the problem when trying to provide default content for Resources through a Template Property.

[[*content:default=`[[+defaultContent]]`]]

Because the default output filter is not applied until after embedded tags are processed, every Resource using this Template always evaluated the defaultContent property regardless if the content field was empty or not. Since my defaultContent contained some Chunks that included Snippets and other MODX Tags, all of those got parsed too, even if the filter conditions failed. The resulting parsed output of the property was then discarded, and the content field was finally returned to be processed.

Luckily, after considering this recurring problem with a little more intensity, I finally realized that this problem is completely avoidable. By approaching conditionals armed with the knowledge of the threat, it is quite easy to avoid any performance degradation when using conditionals in your MODX presentation layer. Just do not embed MODX tags as results for your conditional expressions. Instead, construct MODX tags from the result.

Let's look at an example using conditional filter expressions that have multiple conditional paths. Here is a simple conditional that can quickly become a drain on performance because of the order of parsing:

[[*field:is=`0`:then=`[[!SomeScript]]`:else=`[[$SomeChunk]]`]]

Guess what happens when MODX parses this tag?

  1. Each of the tags contained in the then and the else conditional paths are parsed,
  2. The *field tag itself is parsed and
  3. Finally, the conditional filter is applied.

In other words, regardless of the result of your conditional comparison, everything in both conditional paths is executed recursively to it's logical end. How big of an unnecessary burden this becomes depends entirely on the nature of the tags involved. But you can see how it can quickly get out of control when many conditionals are involved.

The brilliantly simple way to avoid this problem? Just return what's between the MODX tags and have the result of your conditional build what is going to be parsed next by MODX.

[[[[*field:is=`0`:then=`!SomeScript`:else=`$SomeChunk`]]]]

That's it. Don't give MODX the chance to take your tags and run with them when embedded as results in conditional paths. Make it wait until you've evaluated your condition to do anything about it.

Better than a laser zapper, I can tell you that.

Accepting the Things I Cannot Change

It feels good to no longer wish for the eradication of something I've regarded as a pest for so long. Sometimes a little deeper consideration than we are willing to give an idea can reveal a lot about how we should best deal with it. Consider the mosquitoes!

Hi. We’re MODX.

We’re here to help you fix, build and grow fantastic sites. How can we help?




How can we help?

Tell us the general reason for reaching out so we can connect you with the right team.

MODX Diagnostics

MODX’s Open Source software is 100% free for anyone to download and use. As the team behind it for more than a decade, we know it inside, out, and then some.

Like any software, sometimes things break; we can usually fix them very fast. But, we do have to charge for our time to support our families and fund its ongoing development. There are almost an unlimited variety of things that can cause problems, including server upgrades, corrupt files, accidental changes, outdated software, database hiccups and more. We will save you a lot of time and frustration, and get you back in action.

With our MODX Diagnostic service, we determine the source of issues, and often fix them on the spot. For more extensive problems needing more time, like hacked sites or overdue upgrades, we provide additional estimates and guidance. MODX Diagnostics cost $99 for standard business hours support (US Central Time), or $500 for priority, rush or after-hours emergencies.

If you don’t have budget for professional support from the source, you look for answers in the MODX Forums or Documentation, or seek help from MODXers in the Community Slack, or from MODX Professionals near you.

  I’m not ready to pay, let’s talk…

After submitting this form and completing payment, we will collect your access credentials in a secure support ticket. We look forward to helping restore your site back to full health.

Hi! We’d love to work together.

If you have a simple problem that needs our assistance, please request quick fix help here.

What should we keep in mind?

The project involves:
(select all that apply)
What are you planning?
(select all that apply)

Some other considerations

Specific project information

Commercial Support Customers

Customers with a current Commercial Support agreeement can get help using this form. Learn more about MODX Preferred Support.

Let’s get started

What seems to be the issue?

Contact MODX

We welcome conversations, ideas, inquiries and even the occassional cold sales call, but support and requests about how to use MODX software sent via this form cannot be guaranteed a response. That said, we try to respond to everyone that reaches out to us within two business days.

To report a security issue or file a bug for MODX software, please email security [at] modx.com to reach our security team. If you are looking for help with MODX, many times you can find an answer in the MODX Forums or MODX Documentation, from MODXers in realtime at the MODX Community Slack Channel, or from a MODX Professional near you.

How can we help?