I’m a designer more than a programmer. Forgive any ignorance.
I’m trying to create a Snippet that will display one of three header types based on a Dropdown list TV. I’ve created a template variable called "headertype" that has three options: none||image||html. In the snippet, I’ve got the following logic:
<?php
if ( $modx->documentObject[’headertype’] = ’image’ ) {
echo "<div id=pageheader>
[*imageheader*]
</div>";
}
elseif ( $modx->documentObject[’headertype’] = ’none’ ) {
echo "<div id=subheader>
[*longtitle*]
</div>";
}
?>
Unfortunately, the logic doesn’t appear to be resolving as either true or false and is displaying both header types.
Any help is much appreciated.
-
☆ A M B ☆
- 24,524 Posts
Why do you need a snippet? Just use the TV tags in your template where you want the output.
In any case, the $modx->documentObject array only contains what is in the site_content table. To get the ouput of a TV you want the $modx->getTemplateVarOutput() function.
http://wiki.modxcms.com/index.php/API:getTemplateVarOutput
Thanks sotwell for the reference to the getTemplateVarOutput function. What I’m trying to do is have one page template that uses just one of a few different header options based on which type of header is selected. As I understand it, if I just place the TVs in the page template, it will display all of the TVs output, instead of just one. I’m also using different css wrappers depending on which headertype is selected.
I think that my problem is in the syntax of my if statement. If I just echo [*headertype*], it returns "image". But my if statement still does not seem to be resolving to true, even with getTemplateVarOutput or getTemplateVar. My if statement is now: if ( $modx->getTemplateVarOutput[’headertype’] = ’image’ ) {...
Thanks again, sotwell, and anyone else who wants to chime in.
-
☆ A M B ☆
- 24,524 Posts
Ok, you can’t use MODx tags in a snippet. You have to access them programmatically with the $modx object variables or functions.
So your snippet should be
<?php
if ( $modx->getTemplateVarOutput('headertype') == 'image' ) {
$output = '<div id="pageheader">';
$output .= $modx->getTemplateVarOutput('imageheader');
$output .= '</div>';
}
elseif ( $modx->getTemplateVarOutput('headertype') == 'none' ) {
$output = '<div id="subheader">';
$output .= $modx->documentObject['longtitle'];
$output .= '</div>';
}
return $output;
?>
Remember, one = is for assignment, two (or three) == is for comparison.
And snippets should be treated like PHP functions, and their output returned. "echo" will usually work, but sometimes has unexpected results.