We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22448
    • 241 Posts
    I got 2 templates for different product types. Each template has its own set of TVs describing that product’s attributes.
    On a product page there’s a chunk that shows all of the product’s attributes.
    So a PHx statement evaluates the template id and picks the correct TV for each attribute. Here’s the code:

    [*phx:if=`[*template*]`:eq=`22`:then=`[[Ditto?&documents=`[*optionsTempRoof*]`&tpl=`@FILE:assets/chunks/templates/product_option_related_tpl.chk`]]`:else=`[[Ditto?&documents=`[*optionsPermRoof*]`&tpl=`@FILE:assets/chunks/templates/product_option_related_tpl.chk`]]`*]


    The problem is that for every product only one TV exists. I would expect that in the example above PHx would evaluate from left to right and if the "eg=`22`" condition is true would output the value and stop. But it doesn’t. It continues to evaluate the statement in the "else" clause, finds the "optionsPerm2Roof" tv, and since it doesn’t exist throws this error:

    « MODx Parse Error »
    MODx encountered the following error while attempting to parse the requested resource:
    « Execution of a query to the database failed - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[*optionsPermRoof*]) AND sc.published=1 AND sc.deleted=0) AND (' at line 3 »
          SQL: SELECT DISTINCT sc.id FROM `smartspacedb`.`modx_site_content` sc LEFT JOIN `smartspacedb`.`modx_document_groups` dg on dg.document = sc.id WHERE (sc.id IN ([*optionsPermRoof*]) AND sc.published=1 AND sc.deleted=0) AND (sc.privateweb=0) GROUP BY sc.id
          [Copy SQL to ClipBoard]
     
    Parser timing
      MySQL: 	0.0100 s	(14 Requests)
      PHP: 	0.1448 s	 
      Total: 	0.1548 s


    If i change "optionsPermRoof" in the else clause to "optionsTempRoof", i.e. a TV that exists everything works fine. Is there a way to force PHx to stop evaluating after a true condition has been met?
      • 19388
      • 297 Posts
      Hi,
      Try:
      [+phx:if=`[*template*]`:eq=`22`:then=`[[Ditto?&documents=`[*optionsTempRoof*]`&tpl=`@FILE:assets/chunks/templates/product_option_related_tpl.chk`]]`:else=`[[Ditto?&documents=`[*optionsPermRoof*]`&tpl=`@FILE:assets/chunks/templates/product_option_related_tpl.chk`]]`+]


      or
      [*template:eq=`22`:then=`[[Ditto?&documents=`[*optionsTempRoof*]`&tpl=`@FILE:assets/chunks/templates/product_option_related_tpl.chk`]]`:else=`[[Ditto?&documents=`[*optionsPermRoof*]`&tpl=`@FILE:assets/chunks/templates/product_option_related_tpl.chk`]]`*]

        • 22448
        • 241 Posts
        Thanks for the pointer. Will give it a try and report back.
          • 9995
          • 1,613 Posts
          I'm always struggling with this:
          Is it possible with PHx?

          When id = 1 then = a : or when id = 2 then = b : or when id = 3 then = c : else = z
          [*id:is=`1`:then=`a`:or id:is=`2`:then=`b`:else=`z`*]

          Guess it needs a snippet..

          // Luckely for me I can use [*parent*] [ed. note: fourroses666 last edited this post 11 years, 7 months ago.]
            Evolution user, I like the back-end speed and simplicity smiley
            • 22427
            • 793 Posts
            There is a custom modifier switch by Jako which could be handy for this purpose.

            The collection of Jako's modifiers is worth a look anyway (I found it just today): https://github.com/Jako/PHx-Modifier

            Have a look at the modifiers elsec, elsef and elses, especially.

            From his comment:
            * reason: PHx has one big problem with 'then' or 'else' constructs because the modx-parser
            * inserts all (visible) chunks at the beginning of the parsing process.
            * PHx parses then snippets and the phx-logic from inside to outside
            * (i.e. snippet-calls inside of snippets-parameters were executed first).
            * Also every snippet in the then or else branch will be executed even if the
            * phx expression is not hit - only the output is surpressed.
            [ed. note: ottogal last edited this post 11 years, 7 months ago.]
              • 22427
              • 793 Posts
              [*id:is=`1`:then=`a`:or id:is=`2`:then=`b`:else=`z`*]
              You can use :or just in this way:
              [*id:is=`1`:or:is=`2`:then=`a`:else=`z`*]

              A snippet (using a switch statement) would be more adequate (and performing faster).
                • 3749
                • 24,544 Posts
                A custom snippet should be quite a bit faster.

                [!ShowStuff? $docId=`[[*id]]`!]



                <?php
                /* ShowStuff Snippet */
                $output = '';
                switch($id) {
                   case 1:
                   case 2:
                       $output = 'a';
                       break;
                
                   default:
                       $output = 'z';
                       break;      
                
                }
                
                return $output;



                  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
                  • 9995
                  • 1,613 Posts
                  Thanks !!
                    Evolution user, I like the back-end speed and simplicity smiley
                    • 3749
                    • 24,544 Posts
                    You're welcome! smiley

                    I should mention that there are cases where a custom snippet is not noticeably faster than a well-written output modifier. There are other cases where the snippet is *way* faster. There are no cases where the output modifier will be faster than a good snippet. wink
                      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
                      • 9995
                      • 1,613 Posts
                      How can I add parent to this snippet example Bob gave?

                      I need to "say", when id or parent = 1 then x
                      The output needs to be a chunk with ditto into it.

                      <?php
                      /* ShowStuff Snippet */
                      $output = '';
                      switch($id) {
                         case 1:
                         case 2:
                             $output = 'a';
                             break;
                       
                         default:
                             $output = 'z';
                             break;     
                       
                      }
                       
                      return $output;


                      Think I need to use Switch for this
                      [ed. note: fourroses666 last edited this post 11 years, 5 months ago.]
                        Evolution user, I like the back-end speed and simplicity smiley