As I was working on our Pleth site I recently came across a simple method for creating a conditional template variable in MODx.
We have a Ditto driven News section in our site and I was looking for a method of sharing some authorship related chunks. The only catch was that I needed the title “About the Author” to either display or not dependant on where the chunk was residing.
The How
As I insinuated before, this is actually a pretty straightforward solution. In my site I was working with a Template Variable (TV) called authorShip. Here is my TV:
Variable Name: authorShip
Caption: Author
Description: Calls in authorship section.
Input Type: Radio Options
Input Option Values: None=={{}}||Greg=={{aboutGreg}}||Cotton=={{aboutCotton}}||Kegal=={{aboutKegal}}||GoGo=={{aboutIllene}}
Default Value: LEAVE THIS BLANK
It is important to leave the Default Value blank. I would normally assign it something like {{}}, but in this instance the snippet I created is searching for a TV that is empty.
Next, I created a snippet called aboutAuthor and placed the following code.
<?php
if( $TVarray = $modx->getTemplateVarOutput(array(authorShip)) ){ $tvOutput = $TVarray[authorShip];
if(empty($tvOutput)) return '';
else return '< h2>About the Author< /h2>'; };
?>
I then placed this snippet call, [!aboutAuthor?!] in each of the Chunks I referenced above.
The end result is a chunk that displays without the “About the Author” statement. The statement only displays if an non-empty authorShip TV is present.


