We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 12379
    • 460 Posts
    ...displays an image if there’s a url spec’d in the TV, however, when there’s no TV value the code’s still displaying (missing image icon) on the page. For Evo.

    <?php
    // Checks for image url in TV and displays if present
    
    $document_tvs = $modx->getTemplateVarOutput(true, $id);
    $image = $document_tvs['titleImage'];
    
    $output='';
    if (isset($image)) {
         $output = '<div class="title-img" ><img src="[*titleImage*]" /></div>';
    }
         return $output;
    ?>
    

    Called in the template as [[displayImage]].

    (I know I can use phx but it’s out of action at the moment - thanks)
      Mostly harmless.
      • 26931
      • 2,314 Posts
      Hi hotdiggity.

      $id is not set
      $id = $modx->documentIdentifier;

        • 12379
        • 460 Posts
        Double doh! Knew it was going to be something embarrassing!
          Mostly harmless.
          • 12379
          • 460 Posts
          Blow me down. No difference!
            Mostly harmless.
            • 4041
            • 788 Posts
            Give this a try and see if it helps:
            <?php
            // Checks for image url in TV and displays if present
            
            $image =$modx->documentObject['titleImage'][1];
            
            $output='';
            if (isset($image)) {
                 $output .= '<div class="title-img" ><img src="'.$image.'" /></div>';
            }
                 return $output;
            ?>
              xforum
              http://frsbuilders.net (under construction) forum for evolution
              • 12379
              • 460 Posts
              No difference. No problem with image displaying when URL is spec’d in TV.

              Below is the old snippet works but its not the most elegant solution (despite only a few lines diff). Just been trying to write leaner code but maybe this is a case of "if it ain’t broke, don’t fix it"?!

              <?php
              $document_tvs=$modx->getTemplateVarOutput(true, $id);
              $image=$document_tvs['titleImage'];
              
              if ( $image == '' ) {
                   return;
              } else {
                   echo '<p><img src="[*titleImage*]" alt="[*longtitle*]"></p>';
                   return;
              }
              ?>
                Mostly harmless.