We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36805
    • 354 Posts
    I would like to use ditto to output say 3 elements for each <tr> in my table and each of them in a <td> like this:
    <tr>
      <td>
         element1 of row1
      </td>
      <td>
         element2 of row1
      </td>
      <td>
         element3 of row1
      </td>
    </tr>
    <tr>
      <td>
         element1 of row2
      </td>
      <td>
         element2 of row2
      </td>
      <td>
        element3 of row2
      </td>
    </tr>
    

    How can I achieve this using a Ditto template?
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      make your summary template:
      <tr>
        <td>
      [+element1+]
        </td>
        <td>
      [+element2+]
        </td>
        <td>
      [+element3]+
        </td>
      </tr>

      Put the Ditto call inside the <table> </table> tags.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 36805
        • 354 Posts
        Thank you for your reply but still i do not fully grasp it. How do i tell ditto what [+elementN+] is and how do i control it?

        Right now i access the title,id and a custom tv directly (one element at a time) in the following simple summary template:
        <a href="[~[+id+]~]">[+title+]</a>
        <a href="[~[+id+]~]"><img src="[(base_url)]/[+tvproduct-image-thumb+]" /></a>
        


        I didn’t know it is possible to describe the output of more than one element inside only one summary template. What exactly do i need to do to make that work?
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Isn’t that what you want to do with the table?
          <table>
          [[Ditto? &tpl=`myTpl`.... ]]
          </table>
          

          then the myTpl chunk would have the inner part:
          <tr>
            <td>
          <a href="[~[+id+]~]">[+title+]</a>
            </td>
            <td>
          <a href="[~[+id+]~]"><img src="[(base_url)]/[+tvproduct-image-thumb+]" /></a>
            </td>
          </tr>
          

          The <tr> ... </tr> part gets repeated for each document being processed.
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 7923
            • 4,213 Posts
            I believe he is talking about printing the whole ditto items in talbe cells three per row..

            You can use the [+item[x]+] placeholder where x is a number of the ditto item


              "He can have a lollipop any time he wants to. That's what it means to be a programmer."
              • 36805
              • 354 Posts
              Quote from: doze at Jan 05, 2007, 12:44 PM

              I believe he is talking about printing the whole ditto items in talbe cells three per row..

              You can use the [+item[x]+] placeholder where x is a number of the ditto item
              Indeed that is what i am trying to say. Using this approach I’d run into problems though as far as I can tell. Combining the suggestions of both your and susans post I’d get something like this:
              <tr>
                <td>
              [+item[1]+]
                </td>
                <td>
              [+item[2]+]
                </td>
                <td>
              [+item[3]+]
                </td>
              </tr>
              

              But this would not suffice since it would only refer to the first 3 documents. When I try thinking further I come to the conclusion that i would need to add some kind of offset based on the current [+id+] and the number of documents Ditto has allready processed. However I don’t know how I would achieve this since i php code included in chunks does not get evaluated. Am I missing something? Is there a way to make Ditto aware of the offset i need when i make it process 3 items each loop run?

              I hope I described the desired effect sufficiently now. I’d be grateful for further suggestions.
                • 7923
                • 4,213 Posts
                Sorry, [+item[1]+] etc tags can’t be used in Ditto templates, only in document content outside Ditto call.. Here’s how this could be done in current Ditto version..

                <?php
                $modx->runSnippet("Ditto", array("sortBy"=>"editedon", "sortDir"=>"asc", "displayArchive"=>"0", "summarize"=>"all", "paginate"=>"1"));
                $total = $modx->getPlaceholder("total");
                $output="";
                $count = 0;
                for($i=0;$i<$total;$i++) {
                   if($count == 0) {
                      $output .= "<tr>";
                   }
                   $output .= "<td>".$modx->getPlaceholder("item[$i]")."</td>";
                   if($count == 2 || $i==($total-1)) {
                      $output .= "</tr>";
                      $count = -1;
                   }
                   $count++;
                }
                return $output;
                ?>
                


                Ditto should get new parameter where you could use some [+item+] and Ditto should do that offset counting.. but until then, I think this is the only way without modifying Ditto..


                  "He can have a lollipop any time he wants to. That's what it means to be a programmer."
                  • 36805
                  • 354 Posts
                  Quote from: doze at Jan 07, 2007, 04:56 PM

                  Sorry, [+item[1]+] etc tags can’t be used in Ditto templates, only in document content outside Ditto call.. Here’s how this could be done in current Ditto version..

                  [...]

                  Ditto should get new parameter where you could use some [+item+] and Ditto should do that offset counting.. but until then, I think this is the only way without modifying Ditto..
                  And it worked indeed, for the website we are developing i’d say it is sufficient. I implemented it last wednessday and it took only minor changes to your code (only the Ditto parameters). Thanks a lot for your help!