This is the story of a Snippet I once wrote, how it started out as a very simple way of doing a specific task and how MODX enabled it to become something more -- something super.

Simple "problem" solving

I am building a site in MODX Revolution and I start by making a template for an article with the pagetitle placeholder in an h1-tag, introtext in a p-tag (with a class of intro) and content not wrapped in anything -- I use a rich-text editor, so the markup is of course already there. All is well, until one of the users of the site wants to leave introtext empty... that leaves us with an empty paragraph beneath the page's title -- not pretty, especially when that paragraph has a bottom margin I can't get rid of.

But this is MODX, so it's not a problem at all. Pretty fix:

[[*introtext:ne=``:then=`<p class="intro">[[*introtext]]</p>`]]

Even prettier fix:

    &then=`<p class="intro">[[*introtext]]</p>`

Problem fixed, time to move on! Right? Sure! Only, I suddenly feel an urge to not have MODX parse the introtext placeholder more than necessary. So what can I do? I'll create a Snippet -- more specifically, an output modifier!

Enter wrap

I name my Snippet wrap and put one line of code in it:

return (empty($input) === false && strpos($options, '|') !== false) ? str_replace('|', $input, $options) : '';

And here's how I use it to solve my empty paragraph problem:

[[*introtext:wrap=`<p class="intro">|</p>`]]

So all it does is check if both $input is not empty and $options has a pipe-character in it? And if both these conditions are met, then go ahead and replace the pipe with the value in $input and then return the result? Yes! That's what it does!

There's more

For me, this is a cool little Snippet, capable of solving one tiny little "problem". And that's all, until I suddenly find another use for it: mailto-links!

[[*email:wrap=`<a href="mailto:|">|</a>`]]

Of course it replaces multiple pipes! Now the Snippet is going from cool to beautiful! I'm really proud. But now it turns super, by fixing a problem that is even greater than both empty paragraphs and duplicate placeholders in mailto-links, combined!

    &sortby=`FIELD(, [[*comma_separated_id_list]] )`

This getResources call works perfectly, just don't leave the comma_separated_id_list empty. The result is an incomplete sorting clause in the query to the database, MODX throwing errors and clients getting angry. Not good. Easy to fix? Sure! Just do this:


That's it, right? Of course not! I'm the guy who had to take care of an empty paragraph by adding a tiny little Snippet capable of (up until now) only helping out with two tiny problems. So, can wrap "save" the day, yet again? Here's why it went from beautiful to super:

&sortby=`[[*comma_separated_id_list:wrap=`FIELD(, | )`:default=`menuindex`]]`

Now we can lose the If-call and don't worry about empty template variables. I even get to add a default sorting field!

So there you have it. That's the how and why. The reasoning and solutions are purely subjective and there may be other, better alternatives out there, but the point is: this is what makes MODX so great! To me MODX is an excellent extension of programming. There's never just one way to do something. MODX lets me create something quickly and then gives me the option to easily use it in ways I never even imagined. That's why it's so super!