Search code examples
phpmodxmodx-revolution

Using TV in CHunk after grabbing data within Snippet


I'm getting the page with ID 3. This page has 3 subpages. So, on the page with ID 3 I call a snippet to get the subpages like this:

$c = $modx->newQuery('modResource');
$c->leftJoin('modTemplateVarResource', 'TV1', array('TV1.contentid = modResource.id'));
$c->leftJoin('modTemplateVar', 'TV2', 'TV1.id = TV1.tmplvarid AND TV1.`value` = "XXX"');
$c->where(array(
   'parent' => 3,
   'deleted' => false,
   'published' => true,
   'TV1.value' => 'YYY'
));
$c->limit($limit);
$c->prepare();

$output = "";
$resources = $modx->getCollection('modResource', $c);
foreach ($resources as $resource) {
    $resourceArray = $resource->toArray();
    $output .= $modx->getChunk($tpl, $resourceArray);

}

return $output;

So, when I use get chunk I cannot access the Template Variabel with [[*MyVar]]. Did i have to tell the getCollection to grab them? Or did I have to make a new query to grab the Template Vars and give it to the properties in getChunk two?

Greetings


Solution

  • You can`t use [[*MyVar]] in chunk for this case, because of this placeholders are always contains values for current resource.

    You must replace placeholders in chunk to [[+MyVar]] and select them from database.

    Simple way to do this using package pdoTools:

    $pdo = $modx->getService('pdoFetch');
    $tpl = '@INLINE <p>[[+pagetitle]]</p> [[+tv.date]] [[+tv.image]]';
    $output = '';
    
    $resources = $pdo->getCollection('modResource',
        // Where conditions
        array(
            'published' => 1,
            'deleted' => 0,
        ),
        // Other options
        array(
            'includeTVs' => 'date,image',
            'processTVs' => true,
            'tvPrefix' => 'tv.',
            'limit' => 10,
        )
    );
    
    foreach ($resources as $resource) {
        $output .= $pdo->getChunk($tpl, $resource);
    }
    
    return $output;
    

    General options of pdoTools classes.