If I understand you correctly, you want to display something if there is something to display in any given TV. Otherwise don’t show.
This is how I set things up like that, if there is a better way please chime in. I’m all for improvement!
I am trying to grow my knowledge and better my work with each project I work on. So now I like to call my snippets externally rather than pasting inside of MODx. I like this only because I can update the php in my editor and not have to copy/paste back into MODx.
Here is what my include snippet looks like. This is the only snippet I have that is pasted inside of MODx. Call it "include"
switch(getenv('HTTP_HOST'))
{
case 'local.yoursite.com': // LOCAL Environment
$path = "/path/to/your/site/assets/snippets/";
break;
case 'dev.yoursite.com': // DEV Environment
$path = "/path/to/your/site/assets/snippets/";
break;
default: // PRODUCTION Environment
$path = "/path/to/your/site/assets/snippets/";
break;
}
if ( file_exists($path.$file) )
{
$o = include $path.$file;
}
else
{
$o = 'File not found at: '.$path.$file;
}
return $o;
I like to use a switch since I work in several environments. This is very handy once the site starts moving through the development phases. I don’t have to maintain multiple versions for each environment this way.
This is what the snippet call looks like on my page template:
[[!include? &file=`name_of_your_snippet.php`]]
This is the TV snippet I use to display the TV.
$output = "";
$resource_id = $modx->resource->get('id'); // get the resource id we are working with
// check if there is an image available
$image = $modx->getObject('modTemplateVar',array('name'=>'your_tv_name')); // get the tv object
$image_value = $image->getValue($resource_id); // get the value from the tv
if ($image_value != "")
{
$output .= '<img src="'.$image_value.'" width="151" alt="" />'."\n";
}
else
{
// you could provide a fallback if you wanted here
}
return $output;
So as you can see, I look for the TV, get the value of the TV, then check if the value is null or not. If it is NOT null, I display the value. If it IS null, I can either do nothing, or display a default/fallback image.
Hope this helps!