We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18397
    • 3,250 Posts
    Template Variable Conditional is a snippet designed to show a chunk if a template variable is equal (or not equal) to a value.

    Code:

    /**
     * Template Variable Conditional
     * Created by Mark Kaplan on 26-Feb-2006
     * Used for showing chunks depending on a template variable
     * Call like so:
     * 
     * [!tvcnd? &tv=`TV name` &cnd=`value, mode` &rtn=`name of chunk to return`!]
     * 
     * Mode can either be 1 or  2. Mode 1 shows the chunk if the tv's value is not equal to the conditional and mode 2 shows if the tv's value equals the conditional. [2]
     */
    
    $tv = isset( $tv ) ? $tv : "";
     // get tv name
    $cnd = isset( $cnd )? $cnd: "";
     // get conditional
    $rtn = isset( $rtn )? $modx -> getChunk( $rtn ): "";
     // get chunk to return
    $id = isset( $id )? $id: $modx -> documentObject['id'];
     // get current page ID
    $tvarray = $modx -> getTemplateVarOutput( $tv, $id, $published = 1 );
     $cnd = explode( ',', $cnd );
     $value = $cnd[0];
     $filtertype = isset( $cnd[1] ) ? $cnd[1] : 2;
     if ( $filtertype == 1 && ( !isset ( $tvarray[$tv] ) || $tvarray[$tv] != $value ) ) {
         return $rtn;
          } else if ( $filtertype == 2 && $tvarray[$tv] == $value ) {
         return $rtn;
          } 
    
      • 33337
      • 3,975 Posts
      Great work Mark !!

      I was desperately in need of such thing ... woot! wink

      best wishes,

      zi
        Zaigham R - MODX Professional | Skype | Email | Twitter

        Digging the interwebs for #MODX gems and bringing it to you. modx.link
        • 10487 MODX Staff
        • 1,535 Posts
        Cool snippet Mark! grin That is going to be extremely useful.

        Cheers, Garry
          Garry Nutting
          Senior Developer
          MODX, LLC

          Email: [email protected]
          Twitter: @garryn
          Web: modx.com
          • 6726
          • 7,075 Posts
          Man, forgive me for this but I could kiss you on the lips for this one (though I like girls, I confess) grin !

          This is just GREAT !!!

          Many Thanks for this one, I’ll just try it right now so excited !
            .: COO - Commerce Guys - Community Driven Innovation :.


            MODx est l'outil id
            • 15914
            • 145 Posts
            Very handy... thanks Mark. I’ve already made one of these, but yours is much tidier! laugh
              Kunal Kapoor
              Technical Director
              Limesharp Internet Limited - Effective, Functional, Standards Compliant

              IRC Nick: KingKoopa
              Skype ID: KingKoopa16
              • 6726
              • 7,075 Posts
              I didn’t test it yet : will it work with the multiple checkbox TVs, unlike NewsListing ?
                .: COO - Commerce Guys - Community Driven Innovation :.


                MODx est l'outil id
                • 18397
                • 3,250 Posts
                Quote from: davidm at Feb 27, 2006, 08:40 AM

                I didn’t test it yet : will it work with the multiple checkbox TVs, unlike NewsListing ?

                Right now all it does is a basic if a = b do this and if a != b do this conditional. It actually uses the same filter code as NewsListing.

                As for your multiple checkbox dillema, could you PM me your Imput Values?
                  • 6726
                  • 7,075 Posts
                  Quote from: Mark at Feb 27, 2006, 08:58 AM
                  Right now all it does is a basic if a = b do this and if a != b do this conditional. It actually uses the same filter code as NewsListing. As for your multiple checkbox dillema, could you PM me your Imput Values?

                  I’ll send you this tomorrow morning, I am not clear headed right now and it’s getting late here... thanks for the help !
                    .: COO - Commerce Guys - Community Driven Innovation :.


                    MODx est l'outil id
                    • 34162
                    • 1 Posts
                    Very useful! And due to its relative simplicity it’s also very instructive.
                    Thanks for this one! I got an idea now how to implement more complicated things using @EVAL...
                    kiss
                      • 6470
                      • 69 Posts
                      great snippet! thanks

                      But how to make it multiple choice.

                      Let say
                      if a than {{a}}
                      if b than {{b}
                      if c than {{c}}

                      and etc