We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33982 ☆ A M B ☆
    • 476 Posts
    So I’ve got a site where I’m trying to modify the output of Ditto based on whether a Parent document has children or not. In the tree I’ve got the following setup:

    Directory Pages
    - Category Name 1
    -- Business Name 1
    -- Business Name 2
    -- etc.
    - Category Name 2
    -- Business Name 1
    -- Business Name 2
    -- etc.
    ...
    etc.

    So on the "Directory Pages" document, I had Ditto outputting the Category Name with it’s children underneath the Business Name. But my challenge is figuring out how to not have the Category Name not be output if that specific Category has no children. Obviously I could simply unpublish it, but I need all the Category Names set to publish b/c we’re using the Category Names elsewhere in the site. So my set up is like so:
    [[Ditto? &parents=`8` &tpl=`directory_tpl` &sortBy=`menuindex` &sortDir=`ASC`]]


    Then the directory tpl has:
    [+pagetitle+]
    [[Ditto? &tpl=`directory_organization_link_tpl` &parents=`[+id+]` &sortBy=`menuindex` &sortDir=`ASC` &noResults=`Coming Soon`]]


    The pagetitle above is the Category name and the Ditto call underneath it is the children being outputted.

    I tried to create a snippet to output nothing (no category name or children) in the "directory_tpl", but I’m not sure where I went wrong or if there is a better way to do this. Here’s the snippet I created:
    <?php
    // initialize and get the number of children for a particular parent
    $childCount = 0;
    $children = $modx-> getDocumentChildren($parent);
    // determines whether childCount and children are greater than 0
    if ($childCount < count($children))
    // If childCount is greater to the value of 0, then return the following chunk which has the Category and children here
    echo "{{child_count_gt_0}}";
    // But if there are no children for the parent document, return the following below which is empty
    else
    echo "{{child_count_eq_0}}";
    ?>


    Anyone got any ideas? Thanks!

      Precision Web Development ... SmashStack.com
      • 4172
      • 5,888 Posts
      Can you tell us, whats your issue with your solution?
      How do you call your snippet in your ditto-chunk?
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 33982 ☆ A M B ☆
        • 476 Posts
        Can you tell us, whats your issue with your solution?
        How do you call your snippet in your ditto-chunk?

        Sorry, guess I didn’t clarify my problem very well wink

        I couldn’t get the snippet to work basically. It still would output the Category Name even when the Category had no children (which it’s not supposed to do).

        As far as how I was calling my snippet in my ditto-chunk (directory_tpl), I tried both cached and uncached for the snippet and neither way worked.

        Do you need anymore information or is that helpful?
          Precision Web Development ... SmashStack.com
          • 4172
          • 5,888 Posts
          You can try this way:

          create snippet ’checkForChildren’:

          <?php
          //[[checkForChildren? &parents=`0`]]
          
          $documents=array();
          $parents=$modx->getChildIds($parents,1);
          if (count($parents)>0){
          foreach ($parents as $parent){
          $children=$modx->getChildIds($parent,1);
          if (count($children)>0){
          $documents[]=$parent;    
          }
          }
          }
          
          return implode(',',$documents);
          ?>


          your Ditto-call:

          [!Ditto? &documents=`[[checkForChildren? &parents=`8`]]` &tpl=`directory_tpl` &sortBy=`menuindex` &sortDir=`ASC` &display=`all`!]


          this way Ditto catches only these documents which have children.

            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 33982 ☆ A M B ☆
            • 476 Posts
            Perfect...that did it...thanks! smiley
              Precision Web Development ... SmashStack.com