We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 436 ☆ A M B ☆
    • 265 Posts
    At the moment I’ve got a main "All News" category which displays articles from folder IDs 22, 7 and 27; and then when clicking the individual categories you get just those articles.

    This is some of the category template code. As you can see I’m using a series of IF statements to detect which category we’re on, and then displaying a slightly varied version of getResources - only rally the &parents property changes...

    Anyone got any ideas how this could be achieved in a better way?

    	 <!-- Code triggered if on the All News page, including content from categories 22, 7 and 27 -->
    
    [[If? &subject=`[[*id]]` &operator=`IS` &operand=`1` &then=`
    
    	 [[getResourcesTag?
    	  &element=`getResources`
    	  
    	  &tpl=`article.summary`
    	  &pageVarKey=`page`
    	  &parents=`22,7,27`
    
    	  &includeContent=`1`
    	  &limit=`15`
    	  &showHidden=`0`
    	  &pageLimit=`2`
    	  &includeTVs=`1` &processTVs=`1` &tvPrefix=``
      
    	  &pageFirstTpl=`<li class="first-page"><a[[+classes]][[+title]] href="[[+href]]">First Page</a></li>`
    	  &pagePrevTpl=`<li class="previous-page"><a[[+classes]][[+title]] href="[[+href]]">Previous Page</a></li>`
      
    	  &pageNavTpl=`<li class="page"><a href="[[+href]]">[[+pageNo]]</a></li>`
    	  &pageActiveTpl=`<li class="page[[+activeClasses:default=` active`]]"><a href="[[+href]]">[[+pageNo]]</a></li>`
    	  
    	  &pageNextTpl=`<li class="next-page"><a[[+classes]][[+title]] href="[[+href]]">Next Page</a></li>`
    	  &pageLastTpl=`<li class="last-page"><a[[+classes]][[+title]] href="[[+href]]">Last Page</a></li>`
      
    	  ]]
    	  	  
    `]]
    	  
    	 <!-- Code triggered if on the category 22, only including content from categories 22 -->
    
    [[If? &subject=`[[*id]]` &operator=`IS` &operand=`22` &then=`
    	  
    	 [[getResourcesTag?
    	  &element=`getResources`
    	  
    	  &tpl=`article.summary`
    	  &pageVarKey=`page`
    	  &parents=`22`
    
    	  &includeContent=`1`
    	  &limit=`15`
    	  &showHidden=`0`
    	  &pageLimit=`2`
    	  &includeTVs=`1` &processTVs=`1` &tvPrefix=``
      
    	  &pageFirstTpl=`<li class="first-page"><a[[+classes]][[+title]] href="[[+href]]">First Page</a></li>`
    	  &pagePrevTpl=`<li class="previous-page"><a[[+classes]][[+title]] href="[[+href]]">Previous Page</a></li>`
      
    	  &pageNavTpl=`<li class="page"><a href="[[+href]]">[[+pageNo]]</a></li>`
    	  &pageActiveTpl=`<li class="page[[+activeClasses:default=` active`]]"><a href="[[+href]]">[[+pageNo]]</a></li>`
    	  
    	  &pageNextTpl=`<li class="next-page"><a[[+classes]][[+title]] href="[[+href]]">Next Page</a></li>`
    	  &pageLastTpl=`<li class="last-page"><a[[+classes]][[+title]] href="[[+href]]">Last Page</a></li>`
      
    	  ]]
    	  	  
    `]]
      	  
    	 <!-- Code triggered if on the category 7, only including content from categories 7 -->
    
    [[If? &subject=`[[*id]]` &operator=`IS` &operand=`7` &then=`
    	  
    	 [[getResourcesTag?
    	  &element=`getResources`
    	  
    	  &tpl=`article.summary`
    	  &pageVarKey=`page`
    	  &parents=`7`
    
    	  &includeContent=`1`
    	  &limit=`15`
    	  &showHidden=`0`
    	  &pageLimit=`2`
    	  &includeTVs=`1` &processTVs=`1` &tvPrefix=``
      
    	  &pageFirstTpl=`<li class="first-page"><a[[+classes]][[+title]] href="[[+href]]">First Page</a></li>`
    	  &pagePrevTpl=`<li class="previous-page"><a[[+classes]][[+title]] href="[[+href]]">Previous Page</a></li>`
      
    	  &pageNavTpl=`<li class="page"><a href="[[+href]]">[[+pageNo]]</a></li>`
    	  &pageActiveTpl=`<li class="page[[+activeClasses:default=` active`]]"><a href="[[+href]]">[[+pageNo]]</a></li>`
    	  
    	  &pageNextTpl=`<li class="next-page"><a[[+classes]][[+title]] href="[[+href]]">Next Page</a></li>`
    	  &pageLastTpl=`<li class="last-page"><a[[+classes]][[+title]] href="[[+href]]">Last Page</a></li>`
      
    	  ]]
    	  	  
    `]]
      	  	  
    	 <!-- Code triggered if on the category 27, only including content from categories 27 -->
    
    	 [[If? &subject=`[[*id]]` &operator=`IS` &operand=`27` &then=`
    	  
    [[getResourcesTag?
    	  &element=`getResources`
    	  
    	  &tpl=`article.summary`
    	  &pageVarKey=`page`
    	  &parents=`27`
    
    	  &includeContent=`1`
    	  &limit=`15`
    	  &showHidden=`0`
    	  &pageLimit=`2`
    	  &includeTVs=`1` &processTVs=`1` &tvPrefix=``
      
    	  &pageFirstTpl=`<li class="first-page"><a[[+classes]][[+title]] href="[[+href]]">First Page</a></li>`
    	  &pagePrevTpl=`<li class="previous-page"><a[[+classes]][[+title]] href="[[+href]]">Previous Page</a></li>`
      
    	  &pageNavTpl=`<li class="page"><a href="[[+href]]">[[+pageNo]]</a></li>`
    	  &pageActiveTpl=`<li class="page[[+activeClasses:default=` active`]]"><a href="[[+href]]">[[+pageNo]]</a></li>`
    	  
    	  &pageNextTpl=`<li class="next-page"><a[[+classes]][[+title]] href="[[+href]]">Next Page</a></li>`
    	  &pageLastTpl=`<li class="last-page"><a[[+classes]][[+title]] href="[[+href]]">Last Page</a></li>`
      
    	  ]]
    	  	  
    `]]	  
    	  
    	  [[!+page.nav:notempty=` 
    	  <ul id="pagination">
    	  [[!+page.nav]]  
    	  </ul>
    	  `]]
    


    Obviously this will get even more messy when more categories are added - and it could get time consuming if the getResources call needs to be edited...
      MODX Ambassador for Thailand. Managing Director at Monogon, a web design and development studio based in Bangkok, Thailand. - Follow me on Twitter.
      • 436 ☆ A M B ☆
      • 265 Posts
      Just tried one getResources call without any IF statements as follows:

      	 [[getResourcesTag?
      	  &element=`getResources`
      	  
      	  &tpl=`article.summary`
      	  &pageVarKey=`page`
      	  &parents=`[[*id:is=`1`:then=`22,7,27`:else=`[[*id]]`]]`
      	  &includeContent=`1`
      	  &limit=`15`
      	  &showHidden=`0`
      	  &pageLimit=`2`
      	  &includeTVs=`1` &processTVs=`1` &tvPrefix=``
        
      	  &pageFirstTpl=`<li class="first-page"><a[[+classes]][[+title]] href="[[+href]]">First Page</a></li>`
      	  &pagePrevTpl=`<li class="previous-page"><a[[+classes]][[+title]] href="[[+href]]">Previous Page</a></li>`
        
      	  &pageNavTpl=`<li class="page"><a href="[[+href]]">[[+pageNo]]</a></li>`
      	  &pageActiveTpl=`<li class="page[[+activeClasses:default=` active`]]"><a href="[[+href]]">[[+pageNo]]</a></li>`
      	  
      	  &pageNextTpl=`<li class="next-page"><a[[+classes]][[+title]] href="[[+href]]">Next Page</a></li>`
      	  &pageLastTpl=`<li class="last-page"><a[[+classes]][[+title]] href="[[+href]]">Last Page</a></li>`
        
      	  ]] 


      They key being setting the &parents to: &parents=`[[*id:is=`1`:then=`22,7,27`:else=`[[*id]]`]]`

      Seems to work - if anyone else comes across this thread and thinks of a good way of achieving the same results, please post your ideas smiley
        MODX Ambassador for Thailand. Managing Director at Monogon, a web design and development studio based in Bangkok, Thailand. - Follow me on Twitter.
        • 4172
        • 5,888 Posts
        you can create checkbox-TV with all parents. for example with this input-options:

        default==[[*id]],7,22,27
        or let this create dynamically with use of @EVAL return runSnippet(’yoursnippet’);

        and default value of
        [[*id]]

        with name: parents

        and use it with &parents=`[[*parents]]`
          -------------------------------

          you can buy me a beer, if you like MIGX

          http://webcmsolutions.de/migx.html

          Thanks!
          • 436 ☆ A M B ☆
          • 265 Posts
          Yes, both of those ideas are good!

          For some situations, It would be good if there were some way of having a three or four option IF statement; IF > Then > Else > ElseIf, etc?
            MODX Ambassador for Thailand. Managing Director at Monogon, a web design and development studio based in Bangkok, Thailand. - Follow me on Twitter.
            • 27708 MODX Staff
            • 2,502 Posts
            Adam,

            If you ever find the need for multiple conditions this is the ideal situation to make your own snippet using a switch statement. This is not high skill php here but simple snippet that does only what you tell it to based on various conditions. I’d suggest any time you need to add a 2nd level of condition it’s time to write even a tiny snippet. It will be more efficient and far easier to read too.
              Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. Blog ✦ Twitter ✦ LinkedIn ✦ GitHub
              • 33968
              • 863 Posts
              For some situations, It would be good if there were some way of having a three or four option IF statement; IF > Then > Else > ElseIf, etc?

              Adam, you should check out the Switch snippet which would be ideal for your situation:
              http://modx.com/extras/package/switch

              Also, I extended If several months ago to allow for multiple conditions:
              http://modxcms.com/forums/index.php?topic=63762.0
                • 27708 MODX Staff
                • 2,502 Posts
                Lucas,

                That’s simple and cool. I write switch statements all the time but often only need to use them once. This will come in handy. Thanks for sharing.
                  Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. Blog ✦ Twitter ✦ LinkedIn ✦ GitHub
                  • 33968
                  • 863 Posts
                  Quote from: smashingred at Aug 02, 2011, 04:24 PM

                  That’s simple and cool. I write switch statements all the time but often only need to use them once. This will come in handy. Thanks for sharing.
                  It sure is! Everyone seems to use ’If’ but it won’t handle multiple conditions; I’m surprised ’Switch’ isn’t more popular. Also, the code is incredibly simple.

                  I just want to point out that I did not code that extra - all credit should go to theuros as the snippet author.