
[+pagetitle+] [+content+] [+firsttemplatevariable+]
<?php
//some variables for the template names..
$mainchunkname = "myOuterChunkTpl";
$rowchunkname = "myChunkTpl";
// your sql or what ever gets your data into an array
$sql = "SELECT * FROM ....";
$rs=$modx->db->query($sql);
$chunkrows="";
// go trough the array
while($row=$modx->fetchRow($rs)) {
//get the chunk and parse it for this row
$chunk = $modx->getChunk($rowchunkname);
$chunkrows .= str_replace('[+firstname+]', $row['firstname'], $chunk);
$chunkrows .= str_replace('[+lastname+]', $row['lastname'], $chunk);
}
// just another example to merge the rows in to some outer template
$chunk = $modx->getChunk($mainchunkname);
$output = str_replace('[+myrows+]', $chunkrows, $chunk);
// return the output to MODx
return $output;
?>
$chunkrows .= str_replace('[+firstname+]', $row['firstname'], $chunk);
<?php //
function renderHtml($chunkHTML,$htmlTpl){
global $modx;
if(!is_array($chunkHTML)){
return "ca coince";
}else{
$replace= array ();
$matches= array ();
//find the placeholders in the chunk
if (preg_match_all('~\[\+(.*?)\+\]~', $htmlTpl, $matches)) {
$cnt= count($matches[1]);
$v = $chunkHTML ;
for ($i= 0; $i < $cnt; $i++) {
$key= $matches[1][$i];
$replace[$i]= $v[$key];
}
$html = str_replace($matches[0],$replace, $htmlTpl);
return $html;
}
return $htmlTpl;
}
}
<?php
$htmlTpl = $modx->getChunk($rowchunkname);
while($row=$modx->fetchRow($rs)) {
//get the chunk and parse it for this row
$chunkHTML ['firstname'] = $row['firstname'];
$chunkHTML ['lastname'] = $row['lastname'];
}
$output = renderHtml($chunkHTML,$htmlTpl);
$modx->parseChunk('myTpl', $modx->placeholders, '[+', '+]');
You could also use the API function built-in for this purpose... $modx->parseChunk(chunkName, replaceVars, prefix, suffix), i.e.
$modx->parseChunk('myTpl', $modx->placeholders, '[+', '+]');
while($row=$modx->fetchRow($rs)) {
$output .= $modx->parseChunk('myTpl', $modx->placeholders, '[+', '+]');
}