We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6038
    • 228 Posts
    G,day all,
    thought i would post this little snippet in case anyone else wanted use something similar to the alternative chunk (&tplAlt=`altChunk`) feature of Ditto, but every third time rather than every other time.

    I needed this function as i have a list of many items which are floated across the page, with three to a row, and the last item in each row needed a class of ’last’ applied to the list element so as to apply some css which removes the right margin - hence keeping the items flush with the right hand side of the page. Although this example shows how to manipulate every third output from ditto his could easily be adapted to any other number easily if required.

    So you need to set up a snippet to work out if we are on the third ’iteration’ of the ditto snippet:
    <?php
    // SNIPPET NAME: 'thirdLast'
    // ditto iterations start from zero, so add 1 to start count from 1
    $count = $iteration + 1;
    // use modulus function to check if count is divisible by 3
    if( ($count % 3) == 0 ) { return ' class="last"'; }
    
    return;
    ?>


    There is no need to use the ’&tplAlt’ parameter in Ditto. Simply modify your normal template chunk to include a call to your snippet like so:
    <li [!thirdLast? &iteration=`[+ditto_iteration+]` !] >


    Hope someone finds this useful! Any feedback welcome.

    • Interesting solution but this wouldn’t allow you to use any other of the ditto classes either. Would make for a handy addition to the main code though I think.
        Ryan Thrash, MODX Co-Founder
        Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
        • 6038
        • 228 Posts
        True - it doesnt allow you to use the native ditto generated classes, which is definitely a downside.
        I wasn’t feeling brave enough to tackle the integration with the ditto snippet!
        If someone thinks its useful, maybe they could consider it - I envisage it working something like:

        [[Ditto? &tpl=`chunk1` &altTpl=`chunk2` &altFreq=`3`]]

        altFreq maybe also accepting values ’odd’ or ’even’.



        • Good idea. Please file a Feature request at the Jira issue tracker os it doesn’t get lost in the forums. Thanks!
            Ryan Thrash, MODX Co-Founder
            Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
            • 6038
            • 228 Posts
            feature request logged as suggested
            • Excellent ... thanks smiley
                Ryan Thrash, MODX Co-Founder
                Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
                • 30694
                • 46 Posts
                Oh this will rock.

                I had the same need last night. I ended up editing my entire stylesheet instead, so I’m good for now, but every third will still be VERY useful!
                  • 7231
                  • 4,205 Posts
                  Maybe use dittos new iteration feature and use phx to do the logic.
                    [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

                    Something is happening here, but you don&#39;t know what it is.
                    Do you, Mr. Jones? - [bob dylan]
                    • 30694
                    • 46 Posts

                    My chunk already had a class applied to the div i wanted the "last" class applied to, so I removed the class=’’ from the snippet and just have it send out the class name alone...


                    <?php
                    // SNIPPET NAME: 'thirdLast'
                    // ditto iterations start from zero, so add 1 to start count from 1
                    $count = $iteration + 1;
                    // use modulus function to check if count is divisible by 3
                    if( ($count % 3) == 0 ) { return "third"; }
                    ?>



                    Is it possible to adapt this to have this return ’first’ ’second’ and ’third’, for applying correct classes to each item in a row?



                      • 6038
                      • 228 Posts
                      this sort of task people come up with phds on what is the best method to do it, but here’s a quick and easy method to get the snippet to output ’first’, ’second’ or ’third’ depending on which iteration of the ditto snippet you are currently at:

                      <?php
                      // SNIPPET NAME: 'one-two-three'
                      // ditto iterations start from zero, so add 1 to start count from 1
                      $count = $iteration + 1;
                      // use modulus function to check if count is divisible by 3
                      $remainder = $count % 3;
                      switch ($remainder) {
                      case 0: $class='third';break;
                      case 1: $class='first'; break;
                      case 2: $class='second';break;
                      }
                      return $class
                      
                      ?>


                      Any improvements are welcome!