We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34120
    • 236 Posts
    I’ve had a search over the forum and found some similar issues but nothing that quite answers my question. What is the default value of an empty TV? I’m trying to run an if statement to check if a TV exists or is populated. I’ve tried various things eg:
    if($brand !='') {
    echo '<h3>Brand: </h3><p>' . $brand . '</p>';
    }

    I can set a default TV value and check for this and it works but ideally the default TV would be blank. Hope this make sense.

    Evolution 1.0.0

    Any advice much appreciated
    Thanks
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Usually you’re only interested in the value of a TV for a given document. If the default TV is empty, then so will the value for a given document if it hasn’t been edited and modified for that document.

      Here’s a quick snippet I wrote to only load a flash player (the HTML for the embedded player is in a chunk) only if a valid movie file is in the document’s TV
      $movie = $modx->getTemplateVar('Movie');
      if (!empty($movie['value'])) {
          if(is_file('assets/flash/videos/pages/' . $movie['value'] . '.flv')) {
               $chunk = $modx->getChunk('rightflash');
               return $chunk;
          }
      }
      return;

      There are several snippets "in the wild" checking for TV values.

      http://wiki.modxcms.com/index.php/API:getTemplateVar
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 34120
        • 236 Posts
        I’ve just noticed your edit, I’ll take a look now, thanks

        ----------------
        Thanks for the reply Susan, sorry though I don’t quite follow (it’s been a long day)

        I’ve got a snippet call
        [!productInfo? &weight=`[*weight*]` &brand=`[*brand*]` &price=`[*price*]` !]

        Snippet
        if($brand !='') {
        echo '<h3>Brand: </h3><p>' . $brand . '</p>';
        }

        Some items don’t have a weight so in these cases I want to hide the weight heading. That’s what I want the if statement to do.
        I may have got it working now, I think the cached snippet was giving me problems. I’m checking it out now.

          • 34120
          • 236 Posts
          Many thanks for the snippet, that was a great help. I’ve got it working using the following:
          $weight = $modx->getTemplateVar('weight');
          $brand = $modx->getTemplateVar('brand');
          
          if (!empty($weight['value'])) {
             echo "<h6>Weight: </h6>" . $weight['value'];
          }
          if (!empty($brand['value'])) {
             echo "<h6>Brand: </h6>" . $brand['value'];
          }


          Thanks for your time.