public function getChunk($name,$properties = array()) {
/* first check internal cache */
if (!isset($this->chunks[$name])) {
/* if specifying chunk value in snippet properties */
if (!empty($this->config['tpl'.$name])) {
$chunk = $this->modx->newObject('modChunk');
$chunk->setContent($this->config['tpl'.$name]);
}
/* if using default chunk names, defaulting to files */
if (empty($chunk)) {
$chunk = $this->_getTplChunk($name);
if ($chunk == false) return false;
}
$this->chunks[$name] = $chunk->getContent();
} else { /* load chunk from cache */
$o = $this->chunks[$name];
$chunk = $this->modx->newObject('modChunk');
$chunk->setContent($o);
}
$chunk->setCacheable(false);
return $chunk->process($properties);
}
<?php
$base_path = (isset($scriptProperties['base_path'])) ? $scriptProperties['base_path'] : '' ;
$file_name = (isset($scriptProperties['file'])) ? $base_path.$scriptProperties['file'] : '';
if (file_exists($file_name))
{
if (strrchr($file_name, '.') == '.php')
{
return include $file_name;
}
else
{
include $file_name;
return '';
}
}
else
{
$modx->log(MODX_LOG_LEVEL_ERROR,'File not found at'.$file_name);
return '';
}
?>
[[!IncludeFromFile? &base_path=`path/to/files` &file=`somefile.chunk.html`]]
<tr class="[[+rowClass]]" id="account[[+id]]"> <td>[[+description]]</td> <td>[[+date]]</td> <td>[[+amount]]</td> </tr>
<tr class="" id="account"> <td></td> <td></td> <td></td> </tr>
$accountcells = '';
foreach ($accounts as $account)
{
$properties = $account->toArray();
$accountcells .= $this->modx->getChunk($rowTpl, $properties);
}
$properties = $account->toArray();
$chunk = $this->modx->newObject('modChunk');
$chunkContent = $this->modx->getSnippet('IncludeFromFile',array('file'=> $rowTpl.'chunk.html'));
$chunk->set('name',$rowTpl); // not sure this is needed
$chunk->setContent($chunkContent);
$accountcells .= $chunk->process($properties);
/**
* Processes the content of a chunk in either of the following ways:
*
* - Should the property tpl+chunkName be set, uses that content
* - Otherwise, loads chunk from file
*
* Also caches the preprocessed chunk content to an array to speed loading
* times, especially when looping through collections.
*
* @access public
* @param string $name The name of the chunk to process
* @param array $properties (optional) An array of properties
* @return string The processed content string
*/
public function getChunk($name,$properties = array()) {
/* first check internal cache */
if (!isset($this->chunks[$name])) {
/* if specifying chunk value in snippet properties */
if (!empty($this->config['tpl'.$name])) {
$chunk = $this->modx->newObject('modChunk');
$chunk->setContent($this->config['tpl'.$name]);
}
/* if using default chunk names, defaulting to files */
if (empty($chunk)) {
$chunk = $this->_getTplChunk($name);
if ($chunk == false) return false;
}
$this->chunks[$name] = $chunk->getContent();
} else { /* load chunk from cache */
$o = $this->chunks[$name];
$chunk = $this->modx->newObject('modChunk');
$chunk->setContent($o);
}
$chunk->setCacheable(false);
return $chunk->process($properties);
}
/**
* Creates a temporary modChunk object from a tpl file.
*
* @access private
* @param string $name The name of the chunk to load from file.
* @return modChunk The newly created modChunk object.
*/
private function _getTplChunk($name) {
$chunk = false;
$f = $this->config['chunks_path'].$name.'.chunk.tpl';
if (file_exists($f)) {
$o = file_get_contents($f);
$chunk = $this->modx->newObject('modChunk');
$chunk->set('name',$name);
$chunk->setContent($o);
}
return $chunk;
}Sure it can; you have to set the instance to not be cacheable though, consider the code from the parseTpl function used by getResources that similarly loads the chunk source:
- $chunk->process() cannot be called multiple times with different properties, since the output will just be cached. Instead, create a new $chunk for each ->process() call
<?php
$chunk = null;
if (!isset($_cache['@CHUNK'])) $_cache['@CHUNK'] = array();
if (!array_key_exists($source, $_cache['@CHUNK'])) {
if ($chunk = $modx->getObject('modChunk', array('name' => $source))) {
$_cache['@CHUNK'][$source] = $chunk->toArray('', true);
} else {
$_cache['@CHUNK'][$source] = false;
}
} elseif (is_array($_cache['@CHUNK'][$source])) {
$chunk = $modx->newObject('modChunk');
$chunk->fromArray($_cache['@CHUNK'][$source], '', true, true, true);
}
if (is_object($chunk)) {
$chunk->setCacheable(false);
$output = $chunk->process($properties);
}
?>