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
introtext in a
p-tag (with a class of
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:
Even prettier fix:
[[If? &subject=`[[*introtext]]` &operator=`notempty` &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!
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:
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!
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!
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!
[[getResources? &parents=`-1` &resources=`[[*comma_separated_id_list]]` &tpl=`ResourceListTemplate` &sortby=`FIELD(modResource.id, [[*comma_separated_id_list]] )` &sortdir=`ASC` ]]
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:
[[[[If? &subject=`[[*comma_separated_id_list]]` &operator=`notempty` &then=`$ResourceListWrapper` &else=`-empty` ]]]]
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(modResource.id, | )`: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!