We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37619
    • 79 Posts
    I'm using MODX Revo 2.2.6

    Hi everyone,

    I have 7 TVs that I'm outputting as follows:

    <ul class="nav nav-tabs">
    	<li class="active"><a href="#item1" data-toggle="tab">Item 1</a></li>
    	[[*item2:!empty=`<li><a href="#item2" data-toggle="tab">Item 2</a></li>`]]
    	[[*item3:!empty=`<li><a href="#item3" data-toggle="tab">Item 3</a></li>`]]
    	[[*item4:!empty=`<li><a href="#item4" data-toggle="tab">Item 4</a></li>`]]
    	[[*item5:!empty=`<li><a href="#item5" data-toggle="tab">Item 5</a></li>`]]
    	[[*item6:!empty=`<li><a href="#item6" data-toggle="tab">Item 6</a></li>`]]
    	[[*item7:!empty=`<li><a href="#item7" data-toggle="tab">Item 7</a></li>`]]
    </ul>
    


    What I'd like to do is create an output filter that states "if item1 is empty, don't display item1's LI and add the class 'active' to item2's LI. If item1 AND item2 are empty, don't display either and add the class 'active' to item3's LI." and so forth.

    Is there any way to do that with output filters? Or do I need to create a snippet for that?

    Thanks.

    This question has been answered by paulp. See the first response.

      Jean-Marc Buytaert (@jmbuytaert)

      MODX truly is the greatest thing that's ever happened to the Internet.
    • discuss.answer
      • 22840
      • 1,572 Posts
      I would personally use jquery to add a class to the first li
      http://api.jquery.com/first/
        • 37619
        • 79 Posts
        I would personally use jquery to add a class to the first li

        Why didn't I think of that before?! Thanks!
          Jean-Marc Buytaert (@jmbuytaert)

          MODX truly is the greatest thing that's ever happened to the Internet.
          • 3749
          • 24,544 Posts
          I think I would use a custom snippet (though JS would work fine too). Something like this (untested):

          <?php
          $output = '';
          $tvIds = '1,77,9,12';
          $tvIdArray = explode(',', $tvIds);
          
          $class = ' class="active" '; // Don't forget the spaces at each end
          $i = 1;
          foreach ($tvIdArray as $k => $tvId) {
              $tv = $modx->resource->getTVValue( (integer) $tvId);
              if (!empty($tv)) {
                  $output .= '<li' . $class . '><a href="#item' . $i . '" data-toggle="tab">Item ' . $i . '</a></li>';         
                  $class = '';
                  $i++;
              }
          }
          return $output;
          
            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
            • 37619
            • 79 Posts
            Thanks Paul and Bob for your replies. The jQuery solution worked like a charm.
              Jean-Marc Buytaert (@jmbuytaert)

              MODX truly is the greatest thing that's ever happened to the Internet.
              • 22840
              • 1,572 Posts
              Glad it worked, and don't forget you can use that for first, last on whatever you need it for ;-)