We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 25103
    • 5 Posts
    How can I add an if/else statement into my template so if its the homepage it shows a block of code, otherwise it shows another block of code?
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      There are a number of ways that you can do this; which version of MODx are you using?
        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
        • 25103
        • 5 Posts
        MODx Revolution 2.0.0-pl rev7212 smiley
          • 5340
          • 1,624 Posts
            • 3749
            • 24,544 Posts
            The fastest way would be a snippet:

            [[!ShowAtHome? &yesChunk=`SomeChunkName` &noChunk=`SomeOtherChunkName`]]


            <?php
            /* ShowAtHome snippet */
            
            if ($modx->resource->get('id') == $modx->getOption('site_start')) {
              $chunk = $yesChunk;
            } else {
               $chunk = $noChunk;
            }
            return $modx->getChunk($chunk);


            Or, a slightly more efficient version of the same thing:

            <?php
            /* ShowAtHome snippet */
            return $modx->getChunk( ($modx->resource->get('id') == $modx->getOption('site_start')) ? $yesChunk : $noChunk);
            



              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
              Evo got a "HomeAlone" snippet like this years ago, but I’ve been using a TV for this instead for almost as long. The default value for the TV is what it normally should be (i.e. the chunk used on all other pages), and it’s only edited on the home page resource (to specify the chunk used for the home page). Then I assign the TV to a group that only the Admin user belongs to, so nobody else even sees it when editing resources. If it’s not something that anybody but Admin will ever need to change, I just use the TV content itself rather than chunks.
                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
                • 28948
                • 42 Posts
                Quote from: ripcurlksm at Nov 12, 2010, 11:57 PM

                How can I add an if/else statement into my template so if its the homepage it shows a block of code, otherwise it shows another block of code?

                [[*id:eq=`1`:then=`I am page 1`:else=`I am not page 1`]]

                (assuming the home page has ID 1)
                  --
                  cal wilson
                  oksushi.com
                  • 37246
                  • 128 Posts
                  I know this is way old... that said, I love the simplicity of the output filter method. To smarten it up just a tad you could modify the code to:
                  [[*id:eq=`[[++site_start]]`:then=`I'm the site_start`:else=`Nope, not site_start`]]
                    I LOVE MODX! | greyskymedia.com
                    • 28042 ☆ A M B ☆
                    • 24,524 Posts
                    I will have to say that over the years I've gradually shifted from manipulating a single template to using two or three, occasionally more, templates to handle the home page and one- or two- or more column pages. Using chunks to manage the common sections (masthead, main menu, footer) having multiple templates can have a number of advantages The templates can act as categories for resources, thus allowing for organization of TVs and other resource content manipulation.
                      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
                      • 37246
                      • 128 Posts
                      Oh for sure! And especially if you're doing form customization. Personally I had some weird banner that the client wanted ONLY on the home page. My code above seems like the easiest way to accomplish this.
                        I LOVE MODX! | greyskymedia.com