Ages ago, I wrote a MODx snippet called ChunkIf and I thought it was about time that I shared it with the MODx community. ChunkIf allows you to choose which of two chunks gets output, based on a template variable being set or not; its not big or clever but I find it useful and hopefully you might do, too.
The long form of the snippet call looks like this:
[[ChunkIf? &tv=`tvname` &trueChunk=`chunkname` &falseChunk=`chunkname` &debug=`1|0` ]]
The parameters to the snippet call work like this:
- &tv
- This is the name of the template variable you want to test. The value of this variable will be checked and if it’s not empty or blank, &trueChunk will be output – otherwise &falseChunk will be.
- &trueChunk
- The name of the chunk who’s content you want output, in the event that your template variable (&tv) has a value.
- &falseChunk
- The name of the chunk who’s content you want output, if your template variable (&tv) is empty. If you omit this parameter, an empty string will be output if &tv is empty.
- &debug
- Set this to 1 to emit debugging comments into the HTML output; set it to 0 or leave it out to switch off.
There is a short version too:
[[ChunkIf? &tv=`tvname` ]]
The shorthand form attempts to output the contents of a chunk with the same name as &tv, if &tv is set, or an empty string. If there’s no chunk with the same name, it also returns an empty string.
This shorthand version is very handy for switching chunks on and off using a single variable. As chunks can contain other chunks and snippet calls, you can use this to switch sidebars, footers and other complex bits of layout on and off on a per page basis, with a simple checkbox template variable.
<?php
/*
Version: 0.2
Date: 21/07/2008
Description:
<strong>0.2</strong> Outputs the contents of the chunk passed in
$trueChunk if the template variable passed in $tv is set (i.e. has a value).
Otherwise outputs the contents of the $falseChunk chunk. Expects $tv to be the
name of the Template Variable to test, and $trueChunk & $falseChunk to be names
of chunks.
eg:
[[ChunkIf? &tv=`tvname` &trueChunk=`chunkname` &falseChunk=`chunkname` ]]
- Long version outputs $trueChunk if $tv has a value, otherwise
outputs $falseChunk.
[[ChunkIf? &tv=`tvname` &trueChunk=`chunkname` ]]
- This short version outputs an empty string if $tv has no value.
[[ChunkIf? &tv=`tvname` ]]
- This very short version attempts to output the contents of a chunk
with the same name as $tv
[[ChunkIf? &tv=`tvname` &debug=`1` ]]
- Switches on debugging output - output's HTML comments for debugging.
*/
// get tv name
$tv = isset( $tv ) ? $tv : '';
// get current page ID
$id = isset( $id )? $id: $modx -> documentObject['id'];
// Get the value of $tv
$tvarray = $modx -> getTemplateVarOutput( $tv, $id );
$value = ''.trim($tvarray[$tv]);
if ($debug) {
$dtmp = '<!--';
$dtmp .= 'tv: '.$tv.', ';
$dtmp .= 'id: '.$id.', ';
$dtmp .= 'value: '.$value;
$dtmp .= '-->';
echo $dtmp;
}
// Decide which chunk to return.
if ($value == '') {
// get chunk to return if $tv has no value
$falseChunk = isset( $falseChunk )? $modx -> getChunk( $falseChunk ): '';
return $falseChunk;
} else {
// get chunk to return if $tv has a value
if (isset( $trueChunk ))
{
$trueChunk = $modx -> getChunk( $trueChunk );
} else {
// Attempt to get the chunk with the same name as $tv
$trueChunk = $modx -> getChunk( $tv );
}
return $trueChunk;
}
?>

