MODX Quick Tip: Gallery Output Separator

Aug 16, 2012

OMG this is so exciting I actually clocked out of work to post this! :P

The Problem

Gallery is a powerful, all-in-one image management component for the MODX Content Management Framework. It allows you to:

  • Sort and organize images,
  • Store image attributes in a custom database,
  • Dynamically render those values in fully customizable output, and
  • Do a whole lot of other things with your images...

*UPDATE: Critical vulnerabilities have been found in the Gallery Extra. While they have been patched, the codebase is aging, and it's highly recommended to use other free and paid solutions.

The one limitation you might have come across in templating Gallery's output, is the lack of support for the &outputSeparator property. For those of you familiar with the venerable MODX Snippet getResources you'll know that &outputSeparator is super-handy. With Gallery you're often rendering a JSON array for a JS gallery plugin, and you need &outputSeparator to delimit objects. Well as of yet Gallery doesn't do this out-of-the-box.

The Solution

This requires modification of the Gallery snippet itself, so I recommend making a copy of it - that way you remove it from the upgrade path and retain a copy of the original snippet. I called my duplicate "GalleryOutput" and will use that name to call the snippet in my chunks & templates, etc.

Edit your duplicate Gallery snippet, and line 132 you'll see this:
*UPDATE: surely line numbers have changed since this post.

$output = implode("\n",$output);

Replace that with this:

$outputSeparator = $modx->getOption('outputSeparator', $scriptProperties, "\n");
$output = implode($outputSeparator, $output);

Then you can use your duplicate Gallery snippet like this:

[[GalleryOutput? &albums=`albumName` &thumbTpl=`myCustomJSONTpl` &outputSeparator=`,`]]

When you upgrade the Gallery component, it will overwrite the Gallery snippet, but not your clone, so you can manage the upgrade manually. Actually in a near future release this will be included by default apparently ;). That's it, have fun with MODX!