We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40385
    • 75 Posts
    Wow great Fuzzical Logic!! That was a great lesson on php and modx. Thanks again...

    Regarding your proposed script...unfortunately it is not working...site_url and useragent snippet are not being updated... sad So no matter what, toArray is not working as expected within the if condition...terrible [ed. note: paul_kemp last edited this post 14 years, 1 month ago.]
      • 39932
      • 483 Posts
      OK. Next final steps:

      <?php
      $output = ''
      if ( condition ) {
          $output = $modx->getChunk('templateChunkA',$modx->resource->toArray()); 
      } else {
          $output = $modx->getChunk('templateChunkB',$modx->resource->toArray());
      }
      return $output;
      


      OR


      <?php
      $output = '';
      if ( condition ) {
          $output = $modx->getChunk('templateChunkA',$modx->resource->toArray()); 
         return $output;
      } else {
          $output = $modx->getChunk('templateChunkB',$modx->resource->toArray());
          return $output;
      }
      


      Functionaly both of these are the same, but the initialization of the $output and separation from the return might give a little push. I personally like the first option. Its prettier.

      P.S. Now you know why inline functions are faster to run, but why they are also not always desired.
        Website: Extended Dialog Development Blog: on Extended Dialog
        Add-ons: AJAX Revolution, RO.IDEs Editor & Framework (in works) Utilities: Plugin Compatibility List
        Tutorials: Create Cross-Context Resources, Cross-Context AJAX Login, Template-Based Actions, Remove Extensions from URLs

        Failure is just another word for saying you didn't want to try. "It can't be done" means "I don't know how".
        • 3749
        • 24,544 Posts
        I wonder how this would compare in terms of speed:

        $fields = $modx->resource->toArray();
        $chunk = $condition? 'templateChunkA' : 'templateChunk'B';
        return $modx->getChunk($chunk, $fields);




        ------------------------------------------------------------------------------------------
        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
          • 40385
          • 75 Posts
          Thanks guys, I tested both of Fuzzical Logic's snippets and the snippet BobRay suggested. And guess what...non of them is working...

          I have to admit that I'm totally lost now. BobRay's example is different from the ones we where testing the whole time because here the toArray() is definitely called out of line whereas Fuzzical Logic's and my (not working) examples are all called inline (in the if condition).

          I also tested this example, which is very similar to the version of BobRay..

          if ( condition ) {
              $finalTpl = 'templateChunkA';
          } else {
              $finalTpl = 'templateChunkB';
          }
          
          return $modx->getChunk($finalTpl,$modx->resource->toArray());


          Here the toArray() comes obviously after and outside of the if condition and it is still not working. (now it's getting really confusing or am I missing something here?)

          This maybe means we have to change the assumption from the first page to:

          When toArray() is called within or after an if condition it won't refresh uncached content fields from chunks that are nested within the actual chunk if they have already been cached once.


          This means the only snippet that is working as expected right now is the following:

          <?php
          $templateChunkA = $modx->getChunk('templateChunkA',$modx->resource->toArray());
          $templateChunkB = $modx->getChunk('templateChunkB',$modx->resource->toArray());
           
          if ( condition ) {
              return $templateChunkA;
          } else {
              return $templateChunkB;
          }
          [ed. note: paul_kemp last edited this post 14 years, 1 month ago.]
            • 39932
            • 483 Posts
            This is interesting, indeed. BobRay's snippet calls toArray() outside of the condition entirely, before any such switch. Something else is going on here and I have to admit I am at a loss here.
              Website: Extended Dialog Development Blog: on Extended Dialog
              Add-ons: AJAX Revolution, RO.IDEs Editor & Framework (in works) Utilities: Plugin Compatibility List
              Tutorials: Create Cross-Context Resources, Cross-Context AJAX Login, Template-Based Actions, Remove Extensions from URLs

              Failure is just another word for saying you didn't want to try. "It can't be done" means "I don't know how".
              • 39932
              • 483 Posts
              Ah ha! After some tooling, getChunk() is also being affected by this for similar (but not exactly the same) reasons. This makes no sense, however, as it is not consistent across the board.

              I have replicated the problem in places where toArray() is in a condition, but getChunk is not. Additionally, I have replicated it where getChunk() is in a condition, but toArray is not.

              The problem is not replicated if getChunk() is in a condition, but does uses a manually coded Array. It does not happen if toArray is not in a condition and getChunk is not in a condition. It is definitely linked to using the two in tandem. Incidentally, it doesn't happen at all using $mod->runSnippet() with a toArray().

              It may be possible that MODx caching is using some caching trick for handling conditions, similar to the way compilers will optimize conditionals. Since it doesn't happen with runSnippet(), this may be a bug. [ed. note: fuzzicallogic last edited this post 14 years, 1 month ago.]
                Website: Extended Dialog Development Blog: on Extended Dialog
                Add-ons: AJAX Revolution, RO.IDEs Editor & Framework (in works) Utilities: Plugin Compatibility List
                Tutorials: Create Cross-Context Resources, Cross-Context AJAX Login, Template-Based Actions, Remove Extensions from URLs

                Failure is just another word for saying you didn't want to try. "It can't be done" means "I don't know how".
                • 40385
                • 75 Posts
                So the final "solution" would be: when using getChunk/toArray in tandem put these in front of the condition to ensure expected parsing of the chunk.


                • getChunk/toArray before condition = working
                • getChunk/toArray in condition = not working
                • getChunk/toArray after condition = not working
                  • 3749
                  • 24,544 Posts
                  You are calling the snippet uncached, right?

                  [[!MySnippet]]



                  Uncached, any of our versions should work. Cached, none of them would work every time.


                  ------------------------------------------------------------------------------------------
                  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
                    • 39932
                    • 483 Posts
                    My stuff is working with both cached and uncached Snippets. Although, it may be the nature of the Snippets I am using, come to think of it.
                      Website: Extended Dialog Development Blog: on Extended Dialog
                      Add-ons: AJAX Revolution, RO.IDEs Editor & Framework (in works) Utilities: Plugin Compatibility List
                      Tutorials: Create Cross-Context Resources, Cross-Context AJAX Login, Template-Based Actions, Remove Extensions from URLs

                      Failure is just another word for saying you didn't want to try. "It can't be done" means "I don't know how".
                      • 40385
                      • 75 Posts
                      Quote from: BobRay at Sep 07, 2012, 07:56 PM
                      You are calling the snippet uncached, right?

                      [[!MySnippet]]



                      Uncached, any of our versions should work. Cached, none of them would work every time.

                      Correct...I'm calling it uncached...however as I stated out earlier it is working with all three version, but only if there are no nested chunks within the actual called chunk. Example:

                      [[!snippet]]=>ChunkA or ChunkB both contain [[!++site_url]
                      => Works with all three version

                      [[!snippet]]=>ChunkA or ChunkB both contain a headChunk with [[!++site_url]]
                      => Works only with version one, where the toArray/getChunk tandem is called prior to the condition