We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6537
    • 70 Posts
    Hi, I’m still just getting into MODx, and PHx has seemed pretty appealing as my PHP knowledge is pretty limited. I’m definitely guilty of doing the above (using ifelse to change header images for example) - I’d rather use a snippet but I’ve fallen at the first hurdle!

    Like I said, I’m still finding my way with PHP, but I was trying to write a snippet that changes the header image depending on the page. I know I could use a TV, but that’s another thing a client has to worry about adding when they create a new document, which I’d like to avoid. So far I’ve been using:

    $id = $modx->documentIdentifier;


    ...which works fine, but I’m having trouble retrieving other document variables (namely the alias and parent values). So for example, for my basic page template, I want the path to the image to be ’/assets/images/[*alias*].jpg’, unless the parent is 7 eg, in which case the image path will be different.

    I know this is real basic stuff (and there are probably better ways of doing it!), but I’d appreciate some pointers. I’ve been looking through the wiki but can’t seem to find what I’m looking for, and the trial and error approach isn’t getting me very far either!

    Thanks
      • 16183
      • 1,390 Posts
      Quote from: dan_s8 at Sep 09, 2009, 10:18 AM

      Hi, I’m still just getting into MODx, and PHx has seemed pretty appealing as my PHP knowledge is pretty limited. I’m definitely guilty of doing the above (using ifelse to change header images for example) - I’d rather use a snippet but I’ve fallen at the first hurdle!

      Like I said, I’m still finding my way with PHP, but I was trying to write a snippet that changes the header image depending on the page. I know I could use a TV, but that’s another thing a client has to worry about adding when they create a new document, which I’d like to avoid. So far I’ve been using:

      $id = $modx->documentIdentifier;


      ...which works fine, but I’m having trouble retrieving other document variables (namely the alias and parent values). So for example, for my basic page template, I want the path to the image to be ’/assets/images/[*alias*].jpg’, unless the parent is 7 eg, in which case the image path will be different.

      I know this is real basic stuff (and there are probably better ways of doing it!), but I’d appreciate some pointers. I’ve been looking through the wiki but can’t seem to find what I’m looking for, and the trial and error approach isn’t getting me very far either!

      Thanks

      Dan,

      Here’s some starter links for ideas:
      http://modxcms.com/forums/index.php?topic=11222.0
      http://modxcms.com/forums/index.php/topic,16863.0.html

      Please start a new post for this if you have further questions...so that your query doesn’t get lost-in-topics...I think the "General Support" forum should do.

      cheers/k
        • 6537
        • 70 Posts
        Thanks (again!) Kongondo. I think I’ll probably start a new post; I get the MODx->documentIdentifier, I just can’t find/work out how to do the same for retrieving the alias or parent for example. If I can understand that bit I think I might be able to cobble the rest of the snippet together!

        Dan
          • 16183
          • 1,390 Posts
          Quote from: dan_s8 at Sep 09, 2009, 11:11 AM

          Thanks (again!) Kongondo. I think I’ll probably start a new post; I get the MODx->documentIdentifier, I just can’t find/work out how to do the same for retrieving the alias or parent for example. If I can understand that bit I think I might be able to cobble the rest of the snippet together!

          Dan
          Dan,

          Use
          $modx->documentObject['variable-name']
          array instead.

          http://svn.modxcms.com/docs/display/MODx096/Document+Variables

          In that link you will find a listing of all of the (currently) available document-specific variables.

          Very useful link: http://bobsguides.com/modx-snippet-faq.html

          cheers/k
            • 6537
            • 70 Posts
            Thanks Kongondo. I had tried that (and various other combinations!), that’s why I was getting frustrated as I had been through that page of the documentation, but it must have been another part of the snippet that was making it not work! That was a good lesson for me - I should have isolated each part of the snippet and made sure they were working separately first, then put the whole thing together; I’m learning slowly!

            Dan
            • You can and should probably use a TV in this instance, even if you wind up sticking a template in it. You can always give it a default value and even do things like make a pop-up list of options from which your client can choose, all without having to code anything beyond a simple double-pipe-delimited list of key==value||key2==value2 pairs.
                Ryan Thrash, MODX Co-Founder
                Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
                • 6537
                • 70 Posts
                Hi Ryan, thanks for adding to this, apologies if it’s going a bit off topic! At least it (once more) shows off the flexibility of MODx, as there’s at least 3 ways of doing this - with a TV, snippet, or PHx (although, as I’ve learnt today, you shouldn’t).

                Would I be better off using a TV because it’d be easier, or is it because my snippet could break something elsewhere? Although I’m reluctant to post my crude snippet for fear of embarressment, I guess I’m not going to learn otherwise - don’t laugh too much!:

                <?php
                $parent = $modx->documentObject['parent'];
                
                if ($parent=="0") {
                $banner = '<img src="/veru/assets/images/[*alias*].jpg" alt="" />';
                }
                if ($parent=="6") {
                $banner = '<img src="/veru/assets/images/trade.jpg" alt="" />';
                }
                if ($parent=="7") {
                $banner = '<img src="/veru/assets/images/news.jpg" alt="" />';
                }
                if ($parent=="8") {
                $banner = '<img src="/veru/assets/images/press.jpg" alt="" />';
                }
                if (($parent=="12") or ($parent=="13") or ($parent=="14")) {
                $banner = '<img src="/veru/assets/images/products.jpg" alt="" />';
                }
                if (($parent=="15") or ($parent=="16") or ($parent=="17")) {
                $banner = '<img src="/veru/assets/images/recipes.jpg" alt="" />';
                }
                
                return $banner;
                ?>


                This works ok (however ugly and clunky the code may be!), both in displaying the right image for the right document, and avoids an extra step for the client. Hopefully as I learn more PHP I can clean it up!

                Thanks again,
                Dan
                  • 16183
                  • 1,390 Posts
                  Dan,

                  Have a look at these:
                  http://devzone.zend.com/node/view/id/626 - if-else construct
                  http://www.tuxradar.com/practicalphp/3/12/2

                  All those extra "ifs" should actually be "elses".."elseif"...I am still learning myself! smiley

                  cheers/k
                    • 34017
                    • 898 Posts
                    or a simple switch() would be most appropriate

                    http://www.tizag.com/phpT/switch.php


                    
                    switch($modx->documentObject['parent'])
                    {
                      case 0:
                        $img = '[*alias*]';
                        break;
                      case 6:
                        $img = 'trade.jpg';
                        break;
                      case 7:
                        $img = 'news.jpg';
                        break;
                      case 8:
                        $img = 'press.jpg';
                        break;
                      case 12:
                      case 13:
                      case 14:
                        $img = 'products.jpg';
                        break;
                      case 15:
                      case 16:
                      case 17:
                        $img = 'recipes.jpg';
                        break;
                      default:
                        $img = 'defulat image here';
                        break;
                    }
                    
                    return '<img src="/veru/assets/images/'$img'" alt="" />';
                    
                    

                      Chuck the Trukk
                      ProWebscape.com :: Nashville-WebDesign.com
                      - - - - - - - -
                      What are TV's? Here's some info below.
                      http://modxcms.com/forums/index.php/topic,21081.msg159009.html#msg1590091
                      http://modxcms.com/forums/index.php/topic,14957.msg97008.html#msg97008
                      • 16183
                      • 1,390 Posts
                      Quote from: ChuckTrukk at Sep 09, 2009, 09:15 PM

                      or a simple switch() would be most appropriate

                      http://www.tizag.com/phpT/switch.php


                      
                      switch($modx->documentObject['parent'])
                      {
                        case 0:
                          $img = '[*alias*]';
                          break;
                        case 6:
                          $img = 'trade.jpg';
                          break;
                        case 7:
                          $img = 'news.jpg';
                          break;
                        case 8:
                          $img = 'press.jpg';
                          break;
                        case 12:
                        case 13:
                        case 14:
                          $img = 'products.jpg';
                          break;
                        case 15:
                        case 16:
                        case 17:
                          $img = 'recipes.jpg';
                          break;
                        default:
                          $img = 'defulat image here';
                          break;
                      }
                      
                      return '<img src="/veru/assets/images/'$img'" alt="" />';
                      
                      



                      Aah, nice

                      cheers/k

                      ps: MOD - please split the thread? Thx.