In the previous article, we looked at a snippet that shows the number of published and unpublished resources connected to each template. In this one, we’ll expand that to show each template with a list of the pagetitles of its attached resources. We’ll call this one
TemplateReportPlus
.
The Code
Put this tag on a page where you’d like to see the report:
[code language=”html”]
[[!TemplateReportPlus]]
[/code]
Paste this code into a snippet called TemplateReportPlus
:
[code language=”php”]
/* TemplateReportPlus snippet */
$templates = $modx->getCollection(‘modTemplate’);
$output = ‘<h2>Template Report</h2>’;
$output .= "\n<p" . "re>"; /* pre tag would trash blog output */
foreach ($templates as $template) {
$id = $template->get(‘id’);
$name = $template->get(‘templatename’);
$output .= "\n\n" . $name ;
/* Published resources */
$c1 = $modx->newQuery(‘modResource’);
$c1->where(array(
‘published’ => ‘1’,
‘template’ => $id,
));
$output .= "\n Published";
$docs = $modx->getCollection(‘modResource’, $c1);
if (empty($docs)) {
$output .= "\n (none)";
} else {
foreach ($docs as $doc) {
$output .= "\n " . $doc->get(‘pagetitle’);
}
}
/* Unpublished resources */
$c2 = $modx->newQuery(‘modResource’);
$c2->where(array(
‘published’ => ‘0’,
‘template’ => $id,
));
$output .= "\n Unpublished";
$docs = $modx->getCollection(‘modResource’, $c2);
if (empty($docs)) {
$output .= "\n (none)";
} else {
foreach ($docs as $doc) {
$output .= "\n " . $doc->get(‘pagetitle’);
}
}
}
return $output . ‘</pre>’;
[/code]
How it Works
The code gets all the site’s templates with getCollection()
in line 2, then loops through them. For each template, first, we get the published resources (looping through them to add each pagetitle to the output). Then, we get that template’s unpublished resources and do the same with them. Finally, we return the output. We’ve wrapped the output in pre
tags to preserve the indentation.
This snippet could be made significantly faster, but it probably isn’t going to run very often, so it’s not worth the trouble to optimize it.
For more information on how to use MODX to create a web site, see my web site Bob’s Guides, or better yet, buy my book: MODX: The Official Guide.
Looking for quality MODX Web Hosting? Look no further than Arvixe Web Hosting!