We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 11927
    • 105 Posts
    This may be a dumb question. But is there a way to call a chunk from inside another chunk? Because what I want to do is have one chunk that outputs some code, and then another chunk that calls the first chunk to output some code, but then with a little bit more code at the end.

    ie.
    chunk A output:
    ’this is some code from chunk A’

    chunk B output:
    ’this is some code from chunk A’
    ’and this is the code from chunk B’

    That way I wouldn’t have to edit two chunks if I wanted to change ’this is some code from chunk A’.

    I tried it using Ditto and it didn’t work.
      You may or may not want to use the code I write. It's probably all against the syntax rules of php and MODx. smiley

      Carpet Cleaning
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Chunks can be nested:

      ChunkA
      <p>This is some code from chunk A</p>
      {{ChunkB}}
      


      ChunkB
      <p>This is some code from chunk B</p>
      


      A chunk can contain any MODx 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
        • 11927
        • 105 Posts
        sottwell, thank you. You got me thinking.

        I thought I had nested chunks before, but it was because I was using Ditto that it didn’t work.

        I’ll use your example because it is easier to understand. ChunkA didn’t have any placeholders so I got this error because it was Ditto calling the chunk:
        ’&tpl either does not contain any placeholders or is an invalid chunk name, code block, or filename. Please check it.’

        So to fix it I just put this in ChunkA to satisfy Ditto looking for placeholders.
        <!--
        By <strong>[+author+]</strong>
        on [+date+].
        -->


        Now I have one chunk that outputs the post ChunkB (BlogTemplate), and another chunk ChunkA (BlogTemplate_All_Posts_But_Last_One) that outputs ChunkB then outputs a divider for every post except the last one.

        Here is my Ditto call.
        [!Ditto? &parents=`5` &display=`10` &total=`12` &commentsChunk=`CommentsBlog` &tpl=`BlogTemplate_All_Posts_But_Last_One` &tplLast=`BlogTemplate`!]



        The reason I did it this way is because I wanted to only edit one chunk, if I wanted to change how the posts were displayed. Instead of having to edit multiple chunks with similar code in each.


        Related posts/questions that got me to my final answer.
        http://ditto.modxcms.com/files/snippet-ditto-php.html#tpl
        http://ditto.modxcms.com/files/snippet-ditto-php.html#tplLast
        http://modxcms.com/forums/index.php/topic,54299.msg313534.html#msg313534
          You may or may not want to use the code I write. It&#39;s probably all against the syntax rules of php and MODx. smiley

          Carpet Cleaning