This is cross-posted, as I originally posted this to the Templates forum, where it got no traction.
I have written a new snippet that allows you to insert a chunk based on the current document’s assigned template .
Suggested use: if you have multiple templates assigned to documents in your site, and these templates require different html, css, or other includes, you can use this snippet to maintain a single source code for the template’s html.
For instance, if you have a section of your site assigned to the template "article", which includes TV’s for author and references, you may insert a chunk that includes those TVs (possibly using the "chunkIf" snippet, on which the current snippet’s code is based). Another template, for instance "affiliatedClubs," may require two divs, one for a sub-menu of club names or locations, another for each affiliate’s contact information. All other templates in use might be the same. The ifTemplate snippet has the option to call a chunk ("falseChunk") if the current template is not matched in order to handle all other cases.
Your global template might look like this:
<head>
{{headerChunk}}
</head>
<body>
[[ifTemplate? &template=`article` &trueChunk=`articleContent` ]]
[[ifTemplate? &template=`affiliateListings` &trueChunk=`affiliateContent` &falseChunk=`standardContent`]]
...
</body>
As you can see, this handles three different sets of template codes in one template file and is suitable for use with all assigned templates in the site. In fact, the above could all be included in a single chunk that would be called in every template on the site.
A short call for this snippet would look like this:
[[ifTemplate? &template=`article` ]]
and will return a chunk with the same name as the template (in this case "article"), if it exists. This probably isn’t useful, but was part of the ifChunk code, so I duplicated that here.
Here is the snippet code. I welcome comments for its improvement. It is my first snippet!
<?php
/*
Name: ifTemplate
Author: Russell Wagner <[email protected]>
Version: 0.1
Date: 30 June 2009
Credits: This snippet is based on the snippet ChunkIf by Duncan Lock <[email protected]>.
Description:
<strong>0.1</strong> Outputs the contents of the chunk passed in
$trueChunk if the template name passed in $template is the name of the current
document's template. Otherwise outputs the contents of the $falseChunk chunk. Expects $template
to be a tempalte name and $trueChunk & $falseChunk to be names of chunks.
eg:
[[ifTemplate? &template=`templateName` &trueChunk=`chunkName` &falseChunk=`chunkName` ]]
- Long version outputs $trueChunk if $template equals current template name, otherwise
outputs $falseChunk.
[[ifTemplate? &template=`templateName` &trueChunk=`chunkName` ]]
- This short version outputs an empty string if $template does not match current document template's name
[[ifTemplate? &template=`tvname` ]]
- This very short version attempts to output the contents of a chunk
with the same name as $tempalte
[[ifTemplate? &template=`tvname` &debug=`1` ]]
- Switches on debugging output - outputs HTML comments for debugging.
*/
$template = isset( $template ) ? $template : '';
$docTemplateName = $modx -> db -> getValue('SELECT templatename FROM '.$modx->getFullTableName('site_templates').' WHERE id='.$modx->documentObject['template']);
$docTemplateName = ''.trim($docTemplateName);
if ($debug) {
$dtmp = '';
$dtmp = '<!--';
$dtmp .= '<p>Specified template: ' .$template;
$dtmp .= '<p>docTemplateName: '.$docTemplateName;
$dtmp .= '<p>Specified trueChunk: ' .$trueChunk;
$dtmp .= '-->';
echo $dtmp;
}
// Decide which chunk to return.
//if template is not a match, then return falseChunk or nothing if falseChunk is not set
//I'm doing this first, since I assume that the negative case is most common, and we want to cut down on overhead
if ($template != $docTemplateName) {
$falseChunk = isset( $falseChunk )? $modx -> getChunk( $falseChunk ): '';
return $falseChunk;
}
else {
//return trueChunk if it's set, else try to return the chunk with the same name as the template
if (isset( $trueChunk )) {
$trueChunk = $modx -> getChunk( $trueChunk );
}
else {
//Attempt to get the chunk with the same name as $template
$trueChunk = $modx -> getChunk( $template );
}
return $trueChunk;
}
?>
In my initial tests I discovered that you must call this snippet for every template in your sight, or else you will get multiple chunks inserted where you only wanted one.
For instance, if you use htis chunk thusly:
[[ifTemplate? &template=`article` &trueChunk=`articleContent` ]]
[[ifTemplate? &template=`affiliateListings` &trueChunk=`affiliateContent` &falseChunk=`standardContent`]]
and the current template is `article`, then you will get the `articleContent` chunk placed by the first call, and the falseChunk `standardContent` placed by the second call. This is not desired behavior. In other words, the false chunk feature doesn’t work. Instead, you’d need to have a separate call of ifTemplate for each Template, inserting `stadardContent` as the trueChunk for each to get this to work as it is.
I realize now that the snippet should take a comma-delimited list of templates and their corresponding chunks, to be the most compact (as is the case for the extended version of the ifChunk snippit), but before continuing with this project, I’d like to know if there is a perceived need or desired in the ModX community for this snippet.