Skip to content

Instantly share code, notes, and snippets.

@sepiariver
Last active December 21, 2017 12:09
Show Gist options
  • Save sepiariver/eedaae89fba715122ed4 to your computer and use it in GitHub Desktop.
Save sepiariver/eedaae89fba715122ed4 to your computer and use it in GitHub Desktop.
Utility snippet for rapid prototyping in MODX CMS.
<?php
// Options
$limit = (int) $modx->getOption('limit', $scriptProperties, 3, true);
// $limit must be > 0
$limit = ($limit === 0) ? 1 : abs($limit);
$tpl = $modx->getOption('tpl', $scriptProperties, '');
if (empty($tpl)) return; // if empty what's the point?
$outputSeparator = $modx->getOption('outputSeparator', $scriptProperties, '');
$delim = $modx->getOption('delimiter', $scriptProperties, ',', true);
// Process properties
$placeholders = array_diff_key($scriptProperties, array(
'limit' => '',
'tpl' => '',
'outputSeparator' => '',
'delimiter' => '',
));
foreach ($placeholders as $key => $val) {
$placeholders[$key] = array_map('trim', explode($delim, $val));
$placeholders[$key] = array_pad($placeholders[$key], $limit, $placeholders[$key][0]);
}
// Populate virtual items
$items = array();
for ($idx = 0; $idx < $limit; $idx++) {
foreach ($placeholders as $k => $v) {
$items[$idx][$k] = $v[$idx];
}
$items[$idx]['idx'] = $idx;
}
// Temporary chunk
$uniqid = uniqid();
$chunk = $modx->newObject('modChunk', array('name' => "{tmp}-{$uniqid}"));
$chunk->setCacheable(false);
// Output
$output = array();
foreach ($items as $item) {
$output[] = $chunk->process($item, $tpl);
}
return implode($outputSeparator, $output);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment