We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4676
    • 10 Posts
    Hi,

    I’m trying to output a listing of documents with Ditto, which is working great. However I would like to switch a class on a DIV every third item (because of how Blueprints CSS works). Is there any kind of counting mechanism. Also, is it possible to use conditional statements in ModX to test for a variable (if, else, etc.).

    This is the chunk that I am using with Ditto:

    <div class="span-7  append-1">
    		  <a href="[~[+id+]~]"><img src="[+ProjectThumb+]" alt="[+title+]" class="projThumb">
    		  <h6>[+title+]</a></h6>
    		  <p>[+introtext+]</p>
    </div>
    


    Basically, I want to change append-1 to last every third item.

    Thanks!
      • 7231
      • 4,205 Posts
      1) As of Ditto 2.1 there is the ditto_iteration placeholder that will display the loop count. You can use this either with a snippet or with PHx to change the output according to the iteration value.

      2) PHx is available to use within Ditto tpl chunks and you can use that for 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]
        • 4676
        • 10 Posts
        Thanks Shane! That worked perfectly.

        My solution is a little ugly, but it will do the job for the modest portfolio site I am working on...

        [+phx:if=`[+ditto_iteration+]`:is=`2`:or:is=`5`:or:is=`8`:or:is=`11`:or:is=`14`:then=`last`:else=`append-1`+]


        It doesn’t really scale, but I don’t plan on having more than 15 items in a section...
          • 25663 MODX Staff
          • 12,272 Posts
          If PHx supports the modulus (%) operator, you can have a solution that does scale; I’m not sure it does though. Something along the lines of the following should hopefully work:

          [+phx:math=`[+ditto_iteration+]%3`:is=`0`:then=`last`:else=`normal`+]
          
            Ryan Thrash, MODX Co-Founder
            Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me