We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 11371
    • 11 Posts
    I need to put some code after every N repeated items. How to do that? Thanks in advance.
      • 18397
      • 3,250 Posts
      This would best be accomplished with conditional templating, but as it is still not finished you’ll need to use the following workaround:

      Append &save=`3` to your Ditto call and then use the following in your document content:

      [+item[0]+]

      [+item[1]+]

      Your code

      [+item[2]+]

      Repeat where the number in the brackets increases until your &display number
        • 11371
        • 11 Posts
        Hmm.. So if I need to print 100 items on one page I must repeat that code 100 times?

        What if the number of items will be less than 100? Only empty chunks of code will be printed?
          • 11371
          • 11 Posts
          This doesn’t work as I want it because I don’t know how many items will be at the page. Is there another way to fix?
            • 28436
            • 242 Posts
            Hi

            Another workaround:

            1. You need a placeholder with the number of iteration in your Template.

            2. You need PHx and a PHx modifier


            1. Ditto manipulation line ~ 933

            Beginn of the loop
            for ($x=$start;$x<$stop;$x++) { 
                $template = $ditto->template->determine($templates,$x,0,$stop,$resource[$x]["id"]);
                // choose the template to use and set the code of that template to the template variable
               ...
            
            insert
            $resource[$x]['iteration'] = $x;
            End of the loop
                ....
                $output .= $renderedOutput;
                // send the rendered output to the buffer
            }


            Now you have the placeholder "[+iteration+]" filled with the current number of the Iteration

            2. "install" PHx and create this snippet

            phx:modulo
            <?php
            /**
             * [+iteration:modulo=`n($options)`:is=`1`:then=`juhu`+] 
             */
            if ( ( (int)$output + 1 ) % (int)$options === 0 )
            {
                return 1;
            }
            else
            {
                return 0;
            }
            ?>


            adjust it for a nicer looking and use it by this way in your template

             [+iteration:modulo=`n`:is=`1`:then=`n-th interation`+]

            example to clear floats after every fourth iteration
            [+iteration:modulo=`4`:is=`1`:then=`<div class="clearboth"></div>`+]


            if you give "2" as parameter for the modulo math, you can simulate the cycle method smarty.
            [+iteration:modulo=`2`:is=`1`:then=`cycle_1`:else=`cycle_2`+]


            example:
            <div style="float:[+iteration:modulo=`2`:is=`0`:then=`left`:else=`right`+]"> left or right? </div>



            Thats it.

            right, false, whatever, no idea, it work for me and i’m happy

            thank you
            ciao, Stefan
              • 11371
              • 11 Posts
              Wow!! Thanks a lot! wink
                • 28436
                • 242 Posts
                your welcome

                i had the same problem two days ago and found no answer.
                I think its a good deal between phx and ditto. ditto for listing, phx for the template. why not. no idea.

                hope it works well in your case.

                ciao, Stefan
                  • 18397
                  • 3,250 Posts
                  This has been implemented as the [+ditto_iteration+] placeholder in the development version.

                  See the following changeset:
                  http://mirror3.cvsdude.com/trac/ditto/codebase/changeset/1443

                  You can get the development version here:
                  http://www.modxcms.com/ditto_download.html
                    • 28436
                    • 242 Posts
                    Thank you!
                      • 24532
                      • 5 Posts
                      To output a row-/colspan in the first iteration I need the complete number of elements. Is there a way to determine this?