We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34459
    • 134 Posts
    I have 3 contexts but only 1 template. I want to use a different chunk for each of those contexts but stay with one template. I understand that I have two option, output modifiers, if add on or a snippet. Basically I want if a rendered page is in web context show chunk 1, if a page is in we2 render chunk2 and so on.
    So my question is how to call context name(id?) and can I use elseif in outputmodifiers?
      Check out blackflow.pl
      • 3548
      • 102 Posts
      Go with snippet (IMHO, output modifiers are pure evil;)

      //get current context key
      $ctxKey=$modx->context->key;
      switch ($ctxKey) {
          case 'ctx1':
              $chunk='chunk1'
              break;
          case 'ctx2':
              $chunk='chunk2'
              break;
      }
      return $modx->getChunk($chunk);
      
        Antonio Zdilar
        linearvector.com
        • 4172
        • 5,888 Posts
        or name your chunks suffixed or prefixed with your context-keys and call them:

        [[$[[*context_key]]_context_chunk]]
          -------------------------------

          you can buy me a beer, if you like MIGX

          http://webcmsolutions.de/migx.html

          Thanks!
          • 34459
          • 134 Posts
          Quote from: blankcheck at May 16, 2012, 12:00 PM
          Go with snippet (IMHO, output modifiers are pure evil;)

          //get current context key
          $ctxKey=$modx->context->key;
          switch ($ctxKey) {
              case 'ctx1':
                  $chunk='chunk1'
                  break;
              case 'ctx2':
                  $chunk='chunk2'
                  break;
          }
          return $modx->getChunk($chunk);
          

          I don’t know why but I am getting a blank page after loading that snippet?
            Check out blackflow.pl
            • 3548
            • 102 Posts
            Well, first, check what are keys of your contexts and put them instead ctx1 and ctx2. Add default into switch statement (so u can know what is happening):
            //get current context key
            $ctxKey=$modx->context->key;
            switch ($ctxKey) {
                case 'ctx1':
                    $chunk='chunk1'
                    break;
                case 'ctx2':
                    $chunk='chunk2'
                    break;
                default:
                    echo "No context selected...";
            }
            return $modx->getChunk($chunk);
            

            Check error logs and in general try to investigate what is really happening...
              Antonio Zdilar
              linearvector.com
              • 3749
              • 24,544 Posts
              I think that should be:

              $modx->context->get('key');



              ------------------------------------------------------------------------------------------
              PLEASE, PLEASE specify the version of MODX you are using.
              MODX info for everyone: http://bobsguides.com/modx.html
                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
                • 3548
                • 102 Posts
                $modx->context->key;
                


                Bob, this should also return current context's key...
                  Antonio Zdilar
                  linearvector.com
                  • 3749
                  • 24,544 Posts
                  Quote from: blankcheck at Jul 03, 2012, 02:06 AM
                  $modx->context->key;
                  


                  Bob, this should also return current context's key...

                  Yes, it does, but it's a MODX convention to get the value of class variables with the class get() method (with a few exceptions like $modx->resource, $modx->user, and $modx->context).. The get() method is guaranteed to work in the future. In a future version, it's possible that $key could be made private to the class and then your method would break.

                  BTW, out of curiosity, I checked the MODX core code for examples and found this:

                  $modx->context->get('key')    --  (45 matches)
                  $modx->context->key           --  (0 matches)
                  



                  ------------------------------------------------------------------------------------------
                  PLEASE, PLEASE specify the version of MODX you are using.
                  MODX info for everyone: http://bobsguides.com/modx.html
                    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