Olaf Reply #1, 6 years, 5 months ago
I love using the hierarchy of the MODx document tree to create certain functionality. While I was working on a FAQ snippet, I found out that most of these snippets follow the same procedure.
The ChildDocumentMapper snippet allows you to map a chunk to every childpage. The usage is as follows:
The snippet should be given a parameter called docChunk. Like this:
What happens is that for every child document of the container the document variables are inserted into placeholders. These placeholders can be used in the docChunk together with some HTML layout.
For a list of all document variables run this command on your MySQL database:
An example chunk could be:
This snippet allows you to create a single layout for every item. I see the usage in FAQ's, news listings, etc. Keep in mind that the items do not use their template, so it can be set to blank. Or integrate links to the child documents in the chunk (the pageid is also available).
For the next version I'm thinking of using subfolders. Actually Theo went on with this snippet. You can find the latest version at the end of this thread. http://modxcms.com/forums/index.php/topic,843.msg26488.html#msg26488
The snippet code:
The ChildDocumentMapper snippet allows you to map a chunk to every childpage. The usage is as follows:
-
[list]
- create a folder that is your container for the items
- fill the contents of this folder with whatever text you want, and include the ChildDocumentMapper snippet
- create a chunk that contains the layout for the items
The snippet should be given a parameter called docChunk. Like this:
[[ChildDocumentMapper?docChunk=NewsMapChunk]]
What happens is that for every child document of the container the document variables are inserted into placeholders. These placeholders can be used in the docChunk together with some HTML layout.
For a list of all document variables run this command on your MySQL database:
describe site_content;
An example chunk could be:
<h3>[+pagetitle+]</h3> created by [+createdby+] on [+createdon+]<br /> [+introtext+]
This snippet allows you to create a single layout for every item. I see the usage in FAQ's, news listings, etc. Keep in mind that the items do not use their template, so it can be set to blank. Or integrate links to the child documents in the chunk (the pageid is also available).
For the next version I'm thinking of using subfolders. Actually Theo went on with this snippet. You can find the latest version at the end of this thread. http://modxcms.com/forums/index.php/topic,843.msg26488.html#msg26488
The snippet code:
/*
* ChildDocumentMapper. This snippet takes a start document ID and queries
* this page for it's children. For every document a chunk is inserted.
*
* Version 1.0
*
* Parameters:
*
* docChunk: The chunk that is inserted for every document. (required)
* emptyChunk: The chunk displayed when no child documents found.
* publishedOnly: Use only published documents. (default = true)
*/
// Check parameters and set defaults
if (!isset($docChunk))
return "No docChunk parameter set!";
if (!isset($emptyChunk))
$emptyChunk = "No documents found";
if (!isset($publishedOnly))
$publishedOnly = true;
$root = $modx->documentIdentifier;
$childPages = $modx->getDocumentChildren($root, $publishedOnly, 0);
$nrChildPages = count($childPages);
if ($nrChildPages != 0)
{
for ($index = 0; $index < $nrChildPages; $index++)
{
// Set placeholders that can be used in the Chunk
foreach ($childPages[$index] as $docVar => $docVarValue)
$modx->setPlaceholder($docVar, $docVarValue);
// Expand the chunk code, and replace Placeholders
$output .= $modx->mergePlaceholderContent(
$modx->mergeChunkContent("{{".$docChunk."}}"));
}
} else
$output = $emptyChunk;
return $output;