Fast and reliable parsing addition for MODX Revolution. Maybe later available on MODX Extras and via Package Management.
MODX internal getChunk has a speed problem if you call it multiple in a snippet. This could be even worse if you iterate through nested objects and call getChunk every iteration.
My approach to solve this, is to generate a parsing queue with a keypath during iteration and fill this queue with keypath based templates and placeholders. Replaceable placeholders are inserted directly and the ones with modifiers were replaced later during one finishing $chunk->process call.
During my small tests the speed improvement with newChunkie was about the factor 3.5 with not nested getChunk calls. With one nested level of iteration the improvement was about the factor 4. Maybe the impact is bigger with larger and/or deeper nested objects. If you want to check the preparing/rendering times, activate the profile
option in the class and get these times for each queue stored newChunkie->profile.
The class has also the option not to process uncached MODX tags (option parseLazy
). So the processed result could be cached from the calling snippet/plugin and the uncached MODX tags were processed later by the MODX parser.
- PHP 5.2 or higher
newChunkie is a PHP class and can only be called from snippet/plugin code.
####Initialization:
$modx->loadClass('newchunkie.newChunkie', MODX_CORE_PATH . 'components/newchunkie/model/', true, true);
$chunkie = new newChunkie($this->modx, array('parseLazy' => TRUE, 'useCorePath' => TRUE));
####Prepare/process the templates
$c = $modx->newQuery('whateverClass');
...
$collection = $modx->getCollection('whateverClass', $c);
$chunkie->setBasepath('whateverBasepath');
// $i represents the keypath in this example
$i = 0;
$output = array();
foreach ($collection as &element) {
$chunkie->setPlaceholders($element->toArray(), $i, '', 'whateverQueue');
$chunkie->setTpl($chunkie->getTemplateChunk('@FILE whatever.row.html'));
$chunkie->setTplWrapper($chunkie->getTemplateChunk('@FILE whatever.outer.html'));
$chunkie->prepareTemplate($i, array(), 'whateverQueue');
$i++;
}
$output = $chunkie->process('whateverQueue');
####Prepare/process nested templates
$c = $modx->newQuery('whateverClass');
...
$collection = $modx->getCollection('whateverClass', $c);
$chunkie->setBasepath('whateverBasepath');
// $i . '.tags.' . $j represents the keypath in this example
$i = 0;
$output = array();
foreach ($collection as &$element) {
$tags = $element->getMany('Tags');
$currentTags = array();
$j = 0;
foreach ($tags as $tag) {
$currentTag = $tag->getOne('Tag');
$title = $currentTag->get('title');
$chunkie->setTpl($chunkie->getTemplateChunk('@FILE whatever.tag.html'));
$chunkie->setTplWrapper($chunkie->getTemplateChunk('@FILE whatever.tagouter.html'));
$chunkie->setPlaceholders(array('title' => $currentTag->get('title')), $i . '.tags.' . $j, '', 'whateverQueue');
$chunkie->prepareTemplate($i . '.tags.' . $j, array(), 'whateverQueue');
$j++;
}
$chunkie->setPlaceholders($element->toArray(), $i, '', 'whateverQueue');
$chunkie->setTpl($chunkie->getTemplateChunk('@FILE whatever.row.html'));
$chunkie->setTplWrapper($chunkie->getTemplateChunk('@FILE whatever.outer.html'));
$chunkie->prepareTemplate($i, array(), 'whateverQueue');
$i++;
}
$output = $chunkie->process('whateverQueue');
####Prepare/process nested templates with multiple queues
$c = $modx->newQuery('whateverClass');
...
$collection = $modx->getCollection('whateverClass', $c);
$chunkie->setBasepath('whateverBasepath');
// $i and $j representing the keypaths in this example
$i = 0;
$output = array();
foreach ($collection as &element) {
$tags = $classified->getMany('Tags');
$currentTags = array();
$j = 0;
foreach ($tags as $tag) {
$currentTag = $tag->getOne('Tag');
$title = $currentTag->get('title');
$chunkie->setTpl($chunkie->getTemplateChunk('@FILE tag.row.html'));
$chunkie->setTplWrapper($chunkie->getTemplateChunk('@FILE tag.outer.html'));
$chunkie->setPlaceholders(array('title' => $currentTag->get('title')), $j, '', 'tags');
$chunkie->prepareTemplate($j, array(), 'tags');
$j++;
}
$chunkie->setPlaceholders($chunkie->process('tags', ', '), 'tags', $i, 'whateverQueue');
$chunkie->setPlaceholders($element->toArray(), $i, '', 'whateverQueue');
$chunkie->setTpl($chunkie->getTemplateChunk('@FILE whatever.row.html'));
$chunkie->setTplWrapper($chunkie->getTemplateChunk('@FILE whatever.outer.html'));
$chunkie->prepareTemplate($i, array(), 'whateverQueue');
$i++;
}
$output = $chunkie->process('whateverQueue');