We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17148
    • 2 Posts
    I have a simple question regarding conditional syntax / Ditto logic in MODx.

    Here’s my current code (simplified) from a product listing page on an e-commerce site:

    Products in Stock:

    [!Ditto? &parents=`31` &sortBy=`menuindex` &sortDir=`ASC` &tpl=`category_listing_tpl` &filter=`in_stock,Yes,1`!]

    Products Not in Stock:

    [!Ditto? &parents=`31` &sortBy=`menuindex` &sortDir=`ASC` &tpl=`category_listing_tpl` &filter=`in_stock,No,1`!]


    My question is: how do I only display the "Products Not in Stock" text if there are products not in stock? I don’t think an external [+phx:if+] statement would work since the in_stock variable is only encountered within the Ditto loop.

    Thanks!
      • 22427
      • 793 Posts
      Just an idea:
      Using a template chunk [tt]foo[/tt] containing only [tt][+id+][/tt] with a Ditto call like that:
      [!Ditto? &parents=`31` &tpl=`foo` &filter=`in_stock,No,1`!]

      you would get as output a string (concatenating the IDs of the found products). The length of this string is a criterion you could use in a phx statement.
      The problem with this is that in the case of no results the Ditto output will not be empty, but the string "No documents found." You can change this default string to something else using &noResults=`none`. But I did not succeed to catch this case in a phx statement. I tried
      [+phx:input=`[!Ditto? &parents=`31` &tpl=`foo` &filter=`in_stock,No,1`!]`:isnot=`none`:then=`Products Not in Stock`:else=``+]
      but always landed in the else case... huh
        • 4310
        • 2,310 Posts
        Use the tplFirst option :
        &tplFirst=`category_listing_tpl_first`

        Then in your category_listing_tpl_first chunk o something like :
        <h2>Products Not in Stock:</h2>
        <div class="productListing">    
            <a href="[~[+id+]~]" title="[+longtitle+]">                
            <h3><img src="[+thumb_image+]" alt="[+longtitle+]" width="125" height="125" /> [+longtitle+]</h3> 
            <p>[+product_summary+]</p> 
            </a>
        </div> 
          • 22427
          • 793 Posts
          A clever way out!
          [tt]&tplFirst[/tt] just contains the additional headline, the rest should be identical to the content of [tt]&tpl[/tt].

          Anyway I would like to know why this is not working:
          [+phx:input=`[!Ditto? &parents=`31` &tpl=`foo` &filter=`in_stock,No,1`!]`:isnot=`none`:then=`Products Not in Stock`:else=``+]
          Obviously the no-results-output of Ditto can not be treated like a string?
          I seem to confuse something with the syntax of phx...
            • 33337
            • 3,975 Posts
            Use &noResults=`your text here`
              Zaigham R - MODX Professional | Skype | Email | Twitter

              Digging the interwebs for #MODX gems and bringing it to you. modx.link
              • 17148
              • 2 Posts
              Thanks for helping me mull this one over, guys.

              bunk58 and zi - won’t your code result in the <h2>Products Not in Stock:</h2> still displaying even if there are no out of stock products?

              Obviously, if there are no out of stock products, I don’t need a <h2>Products Not in Stock:</h2>...
                • 22427
                • 793 Posts

                @ zi:
                Of course, I had [tt]&noResults=`none`[/tt] in my call laugh (somehow lost in copying).
                With no results the Ditto output is [tt]none[/tt] , but it seems impossible to check that via phx condition. >:(

                @lilwillie:
                It works well if you set [tt]&noResults=` `[/tt] (a blank character, not an empty string [tt]``[/tt] !).
                If no documents are found, the blank will be output, but not the headline, because there is no first item to display.