AnnysLM, you may also want to read this thread:
https://forums.modx.com/thread/?thread=72575&page=1
Bruno17 posted a snippet that (with a bit of modification) does something similar and should perform quite a bit better because instead of getting all of the resource values again, it just uses the existing placeholders. The technique would also work with my original simple little valueToChunk snippet -- the trick is to call the snippet cached, and include a parameter for the id of the current resource.
I put that idea together with some code from the If snippet, changed the name as you suggested, changed the names of some parameters and changed the array of value-chunk pairs to JSON and came up with this:
<?php
/**
* getMyChunk
*
* Copyright 2009-2011 by Jason Coward <[email protected]>, Shaun McCormick
* <[email protected]>, and Eli Snyder <[email protected]>
*
* getMyChunk is free software; you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* If is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* If; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
* Suite 330, Boston, MA 02111-1307 USA
*
* @package getMyChunk
*/
/**
* Snippet to conditionally select a chunk from a JSON array of operand-chunk pairs.
*
* @package getMyChunk
*/
if (!empty($tplCondition) && !empty($conditionalTpls)) {
$conTpls = $modx->fromJSON($conditionalTpls);
if (!empty($scriptProperties['tplCondition'])){
$subject = $modx->getPlaceholder($scriptProperties['tplCondition']);
}
$tplOperator = !empty($tplOperator) ? $tplOperator : '=';
$tplOperator = strtolower($tplOperator);
$tplCon = '';
foreach ($conTpls as $operand => $conditionalTpl) {
switch ($tplOperator) {
case '!=':
case 'neq':
case 'not':
case 'isnot':
case 'isnt':
case 'unequal':
case 'notequal':
$tplCon = (($subject != $operand) ? $conditionalTpl : $tplCon);
break;
case '<':
case 'lt':
case 'less':
case 'lessthan':
$tplCon = (($subject < $operand) ? $conditionalTpl : $tplCon);
break;
case '>':
case 'gt':
case 'greater':
case 'greaterthan':
$tplCon = (($subject > $operand) ? $conditionalTpl : $tplCon);
break;
case '<=':
case 'lte':
case 'lessthanequals':
case 'lessthanorequalto':
$tplCon = (($subject <= $operand) ? $conditionalTpl : $tplCon);
break;
case '>=':
case 'gte':
case 'greaterthanequals':
case 'greaterthanequalto':
$tplCon = (($subject >= $operand) ? $conditionalTpl : $tplCon);
break;
case 'isempty':
case 'empty':
$tplCon = empty($subject) ? $conditionalTpl : $tplCon;
break;
case '!empty':
case 'notempty':
case 'isnotempty':
$tplCon = !empty($subject) && $subject != '' ? $conditionalTpl : $tplCon;
break;
case 'isnull':
case 'null':
$tplCon = $subject == null || strtolower($subject) == 'null' ? $conditionalTpl : $tplCon;
break;
case 'inarray':
case 'in_array':
case 'ia':
$operand = explode(',',$operand);
$tplCon = in_array($subject,$operand) ? $conditionalTpl : $tplCon;
break;
case '==':
case '=':
case 'eq':
case 'is':
case 'equal':
case 'equals':
case 'equalto':
default:
$tplCon = (($subject == $operand) ? $conditionalTpl : $tplCon);
break;
}
}
if(!empty($tplCon)) {
return $modx->getChunk($tplCon);
}
}
return '';
You have to call it cached inside the getResources template and include a dummy parameter for the resource id. So in your case you would call it like this:
[[getMyChunk? &tplCondition=`parent` &tplOperator=`is` &conditionalTpls=`{"2":"blogPost2", "3":"blogPost3","4":"blogPost4","5":"blogPost5","6":"blogPost6","7":"blogPost7","10":"blogPost10"}` &resid=`[[+id]]`]]
[ed. note: esnyder last edited this post 14 years, 10 months ago.]