I've been using :notempty filters to wrap MODX tags in some HTML for years:
[[!mySnippet:notempty=`<div class="foo">[[!mySnippet]]</div>`]]
Reading Jason Coward's blog post on conditionals in tags (
http://modx.com/blog/2012/09/14/tags-as-the-result-or-how-conditionals-are-like-mosquitoes/) got me thinking recently about how this method basically calls the entire snippet twice. I'm not sure if I'm thinking intelligently or not, but I am thinking. Which is usually dangerous for me.
Today I had the idea to use a custom output filter instead of the :notempty filter.It looks something like this:
[[!mySnippet:wrapper=`div.foo`]]
'wrapper' is a Custom Output Modifier that sees if 'mySnippet' returns an empty value, and, if not, returns it wrapped in the chunk 'div.foo'
'wrapper' snippet:
if (strlen(trim($input)) > 0) {
$modx->setPlaceholder('inner.content', $input);
return $modx->getChunk($options);
}
'div.foo' chunk:
<div class="foo">[[+inner.content]]</div>
This also lets me control the wrapper HTML in a chunk, allowing me to create a reusable library of wrapper chunks.
I have to go back and look at a lot of code I've written to see just how useful this idea is, but I think it might be pretty useful.
I'd be curious to know others' thoughts on it.