#MODX Quick Tip: Gallery Output Separator

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…

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 add commas between objects, but not after the last object, correct? 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. This solution was actually provided to me by Shaun McCormick, @splittingred of the MODX Core Team so despite being a “hack” it’s done properly :)

Go to edit your duplicate Gallery snippet, and line 132 you’ll see this:

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

Replace that with this (there shouldn’t be line breaks except after the semi-colons)

$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=`,`]]

If/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!