We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3623
    • 7 Posts
    What I would like to do:
    I want to create a snippet where I check if a template variable is filled with a value (image) or not (Template Variable is an image). If tried almost the whole afternoon, but it makes me bezerk...

    The code:
    // Retrieve TV
    $tv_value = $modx->getTemplateVarOutput('image_1');
    
    // Check if TV is filled
    if(!empty($tv_value)) echo '$tv_value has a value';


    The strange thing is that always get back that the TV in the document I’m putting this snippet on has is filled with a value. That’s not the case as TV’s of some pages are filled and other not. I’m not a php expert, so any help is welcome.

    Thanks in advance!
      • 1122
      • 209 Posts
      <?php
      // Retrieve TV's record (it is an array and therefore always non-empty)
      $tv_row = $modx->getTemplateVar('image_1');
      
      // Check if TV is filled (actual TV's value is stored in 'value' field)
      if(!empty($tv_row['value'])) echo 'tv has a value';
      ?>
      
        • 3623
        • 7 Posts
        Thanks for your reply, this worked perfectly
          • 5340
          • 1,624 Posts
          In some cases PHx is more elegant. See example here.
            • 3623
            • 7 Posts
            It’s a while ago, but here is the solution that worked wink

            PHx was used indeed, and the code below checked if a TV was filled with an image, if it was filled the image was shown.

            [+phx:if=`[*image*]`:is=``:then=``:else=`<a href="[*image*]"><img class="thumb" src="[*image:phpthumb=`w=205&h=135&zc=c`*]" /></a>`+]
              • 53161
              • 130 Posts
              I am gonna ressurect this dead thread because I was looking for answers about using conditional operators on placeholders/template variables:

              If you are a running a newer version of Revo, PHx is built into the core and you cannot download the extra (well done to the MODx guys for including it)
              Here is the documentation that helped me and taught me how to use the conditionals on placeholders -> https://docs.modx.com/revolution/2.x/making-sites-with-modx/customizing-content/input-and-output-filters-(output-modifiers)


              This thread jumps up in Google search so I thought people should know.

              EDIT - Arghh, the forums doesn't allow me to link the documenration because it has "()" chars - here is the google page (top result) -> https://www.google.com/search?q=modx+filter+output&rlz=1C1EKKP_enUS741US741&oq=modx+filter+output&aqs=chrome..69i57j0l5.3679j0j4&sourceid=chrome&ie=UTF-8

              EDIT 2 - guys, you should seriously fix the forum. The above link doesn't list either.
              OK so Google Input and Output Filters (Output Modifiers) | MODX Revolution and click the documentation.... Bah...