We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • I have three TVs I want to check if either of them IS NOT EMPTY.

    I'm currently using this

    [[AndIf? &condition=`[[*TV1]] isempty 0|[[*TV2]] isempty 0|[[*TV3]] isempty 0` &operator=`OR` &then=`<div class="someclass">` &else=``]]

    AndIF is a snippet I use quite often now and found it here: http://forums.modx.com/thread/32601/andif---conditional-snippet-allowing-multiple-and-or-conditions

    My intention is to have the '<div class="someclass">' output so long as either of the three TVs hold content, else there should be NO output at all. But the above call to ANDif snippet is not working. I have tried &operator=`AND`. I also tried using the IF snippet, that didn't work at all.

    Thanks for your thoughts and time in advance.
      • 42046
      • 436 Posts
      Try:

      [[andIf? &condition=`[[*TV1]] notempty |[[*TV2]] notempty |[[*TV3]] notempty` &operator=`OR` &then=`true` &else=`false`]]


      Replacing true and false with your positive and negative outputs.
        • 3749
        • 24,544 Posts
        That's going to be painfully slow compared to a dedicated snippet because of the time spent parsing the conditional and evaluating every condition whether it's required or not. You're also going to want a closing div when the condition is met to keep your design from blowing up. You might try this:

        [[!TvCheck? &returnValue=`<div class="someClass">`]]
           (Div contents here)
        [[!TvCheck? &returnValue=`</div>`]]
        


        <?
        /* TvCheck snippet */
        
        $r = $modx->resource;
        if ($r->getTVValue('TV1') || $r->getTVValue('TV2') || $r->getTVValue('TV3')) {
           return $scriptProperties['returnValue'];
        }
        return '';


        If you put the TV most likely to be not empty first, it will speed things up further, as will using the ID of the TV rather than the name (no quotes if you use the ID).

        The code below would be even faster, though maybe not enough to notice. It will only work if you can use the raw value of the TV (i.e., it doesn't need processing).
        <?
        /* TvCheck snippet */
        
        $docId = $modx->resource->get('id');
        $tvId = array(  /* Use the actual IDs of the TVs here */
            12,
            22,
            29,
        );
        
        foreach ($tvs as $tv) {
           $tvr = $modx->getObject('modTemplateVarResource', 
               array('contentid' => $docId, 'tmplvarid' => $tv);
           if ($tvr) {
               $value = $tvr->get('value');
           } else {
               $tvObject = $modx->getObject('modTemplateVar', $tv);
               $value = $tvObject? $tvObject->get('default_text') : '';
           }
           if (!empty($value)) {
              return $scriptProperties['returnValue'];
           }
        }
        
        return '';




        [ed. note: BobRay last edited this post 9 years, 9 months ago.]
          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
        • Thank you both.

          Dan, with my initial call it works except that even when all of the TVs are empty you still get <div class="someclass">. But with your call nothing is displayed at all whether the TVs are empty or not.

          BobRay I tried the snippet and got an error:
          Fatal error: Call to undefined method modX::resource() in C:\wamp\www\SiteDir\ore\cache\includes\elements\modsnippet\23.include.cache.php on line 9


          Here is my code:

          <?php
          /** TvCheck snippet **/
           
          $r = $modx->resource();
          if ($r->getTVValue('27') || $r->getTVValue(26) || $r->getTVValue(6)) {
             return $scriptProperties['returnValue'];
          }
          return '';
          


          and here is my call:

          [[TvCheck? &returnValue=`<div class="someclasses">`]]
            • 40706
            • 128 Posts
            Mostly we catch this up like this:

            [[+tv1:default=`[[+tv2]][[+tv3]]`:notempty=`<div class="someclass">`]]
            
            [[+tv1:default=`[[+tv2]][[+tv3]]`:notempty=`</div>`]]
            


            Also I use is an custom Outputfilter Snippet "wrap" which mostly used to wrap output from an TV or snippet with an div. Sometimes this is just shorter and feels better.

            <?php
            if(trim($input)=='') return '';
            list($pre,$post) = explode('||', $options,2);
            return $pre.$input.$post;


            [[wrap?
              &input=`[[+tv1]][[+tv2]][[+tv3]]`
              &options=`<div class="someclass">||</div>`
            ]]
            

              • 3749
              • 24,544 Posts
              Sorry, just take out the parentheses:

              $r = $modx->resource;
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
              • BobRay: No errors now but no output either, be it when the TVs are empty or not.

                Thanks Michael. Unfortunately your solutions stil output the <DIV> even when all of the TVs are empty. The scenario seeks to not output anything when all three TVs are empty.

                  • 3749
                  • 24,544 Posts
                  I'd need to see your exact snippet tag and snippet code to help.
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting
                  • The Snippet:

                    <?php
                    /** TvCheck snippet **/
                     
                    $r = $modx->resource;
                    if ($r->getTVValue('27') || $r->getTVValue(26) || $r->getTVValue(6)) {
                       return $scriptProperties['returnValue'];
                    }
                    return '';


                    The call:

                    [[!TvCheck? &returnValue=`<div class="pagehighlight highlight">`]]
                    (some content in between both)
                    [[!TvCheck? &returnValue=`</div>`]]
                      • 3749
                      • 24,544 Posts
                      Remove the quotes around 27, though I don't think that will fix things. Again, the TV's can't be set to the default value or to 0, but I don't see anything wrong. Make sure the name of the snippet is spelled 'TvCheck' exactly.

                      You might try using the exact names of the TVs (in quotes).
                        Did I help you? Buy me a beer
                        Get my Book: MODX:The Official Guide
                        MODX info for everyone: http://bobsguides.com/modx.html
                        My MODX Extras
                        Bob's Guides is now hosted at A2 MODX Hosting