-
-
Save Burick/e5551984e3d2738d7792afed46638028 to your computer and use it in GitHub Desktop.
Utility snippet for rapid prototyping in MODX CMS.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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