Chunks
Translations of this material:
- into Russian: Чанки. Translated in draft, editing and proof-reading required.
-
Submitted for translation by elastic 13.11.2009
Published 2 years, 6 months ago.
Text
Chunks are useful for reusing blocks of code or HTML to your sites. Chunks cannot contain any logic directly, although they can contain calls to Snippets that do contain logic.
Usage
1. [[$chunkName]]
Also, you can pass properties through a Chunk. Say you had a chunk named 'intro' with the contents:
Hello, [[+name]]. You have [[+messageCount]] messages.
You could fill those values with:
1. [[$intro? &name=`George` &messageCount=`12`]]
Which would output:
Hello, George. You have 12 messages.
Processing Chunk via the API
Chunks can be processed from a snippet by the process() function; for example, this code gets the 'rowTpl' Chunk like so:
# <tr class="[[+rowCls]]" id="row[[+id]]">
# <td>[[+pagetitle]]</td>
# <td>[[+introtext]]</td>
# </tr>
and processes it with an array of properties for all the published Resources, into a table, setting the class to "alt" if is an even row:
1. $modx->getCollection('modResource',array('published'));
2. $chunk = $modx->getChunk('rowTpl');
3. $i = 0;
4. $output = '';
5. foreach ($resources as $resource) {
6. $properties = $resource->toArray();
7. $properties['rowCls'] = $i % 2 ? '' : 'alt';
8.
9. $output .= $rowChunk->process($properties);
10. $i++;
11. }
12. return '<table><tbody>'.$output.'</tbody></table>';
Modifying a Chunk Via the API
Chunks can also be manipulated by the MODx API:
# <?php
# /* create a new chunk, give it some content and save it to the database */
# $chunk = $modx->newObject('modChunk');
# $chunk->set('name','NewChunkName');
# $chunk->setContent('<p>This is my new chunk!</p>');
# $chunk->save();
#
# /* get an existing chunk, modify the content and save changes to the database */
# $chunk = $modx->getObject('modChunk', array('name' => 'MyExistingChunk'));
# if ($chunk) {
# $chunk->setContent('<p>This is my existing chunks new content!</p>');
# $chunk->save();
# }
#
# /* get an existing chunk and delete it from the database */
# $chunk = $modx->getObject('modChunk', array('name' => 'MyObsoleteChunk'));
# if ($chunk) $chunk->remove();
# ?>
