We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36786 ☆ A M B ☆
    • 209 Posts
    Hi I'm trying to check if a single checkbox is selected in a checkbox tv and conditionally output specific content. Multiple checkboxes can be selected. I started with this but obviously it won't work (usually I work with radios). Can someone tell me how to achieve this? The output on the checkbox TV is comma delineated.

    [[*animalHomeWithout:is=`dogs`:then=`<img src="assets/images/nodogs.png" class="pull-left"/>`]]
    [[*animalHomeWithout:is=`cats`:then=`<img src="assets/images/nocats.png" class="pull-left"/>`]]
    [[*animalHomeWithout:is=`kids`:then=`<img src="assets/images/nokids.png" class="pull-left"/>`]]
      Ben Morrison
      Hacking templates to pieces since 2003
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      You'll need to use a snippet, which will be more efficient than three output modifiers anyway. Something like this:

      $val = $modx->resource->getTVValue('animalHomeWithout');
       $arr = explode(',', $val);
      $output = '';
      foreach($arr as $value) {
          $output .= '<img src="assets/images/no' .$value. '.png" class="pull-left"/>';
      }
      return $output;
      
        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
        • 3749
        • 24,544 Posts
        @Susan: Nice PHP hack! wink

        I can't resist making one tiny suggestion to format the raw HTML:

        $output .= "\n" . '<img src="assets/images/no' .$value. '.png" class="pull-left"/>';
          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
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Yeah, I impressed myself on that one wink Of course, it does depend on the naming convention of the images.

          There probably should be checks to deal with no or one value (no commas), so no need to explode() or loop. It just seemed like a very small improvement in efficiency in exchange for a bigger, uglier snippet.

          And of course if it's going to start expanding, with three different possible processing paths, it might as well use a tpl, which would mean enabling properties... like using a shotgun to go after a mosquito.
            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
            • 3749
            • 24,544 Posts
            There probably should be checks to deal with no or one value (no commas), so no need to explode() or loop.

            I'm pretty sure that checking the number of values and responding accordingly would take longer than a one- or zero-iteration foreach loop, and the test would be performed every time -- so almost certainly slower than what you have and with extra code.
              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
              • 36786 ☆ A M B ☆
              • 209 Posts
              I actually ended up doing this by just having chunks be part of the input options:

              Dogs==[[$dogs]]||Cats==[[$cats]]||Small Kids==[[$kids]]
                Ben Morrison
                Hacking templates to pieces since 2003
                • 3749
                • 24,544 Posts
                That looks good, though if the content of the chunks isn't too long and seldom changes, skipping the chunks and putting the chunk content directly in the input options would speed up page loads a little.
                  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