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

    I have been facing a series of problems on how to make a snippet call a chunk that contains a snippet. The snippet in the chunk does not get processed. MODx displays the snippet call as it is.

    e.g.
    [[MajorSnippet &tpl=`tplWithAnotherSnippetCall`]]
    


    tplWithAnotherSnippetCall is a chunk with the following contents:
    Name:[+name+]
    Profession:[+profession+]
    Subscription Choices:
    [!TheOtherSnippet!]
    


    TheOtherSnippet is not processed. So Instead MODx shows the call as it is. E.g. it renders the following output:

    Name: John Doe
    Profession: Bugger
    Subscription Choices:
    [!TheOtherSnippet!]

    I thought maybe this could be solved by doing the following:
    -I make TheOtherSnippet output the template and then also insert its processed values

    So the call would then become
    [[MajorSnippet &tpl=`[!TheOtherSnippet!]`]]
    

    I think it can work but I dont know how to approach it using the API.

    Hope someone can help

    Thanks for letting me nag wink



      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Nested snippets have to be called the other way; the outer snippet uncached and the inner snippet cached.

      This is because the snippets are processed as they are found, so the outer snippet will get processed before the inner one, so the parameter is not yet available to the outer one. If it’s called uncached, it doesn’t get processed right away (its tags get converted to cached tags on this pass through the parser), while the inner one will. On the next pass through the parser, the outer snippet now gets processed, and since the inner one was processed earlier, the proper parameter value is now available as the outer snippet is evaluated.

      The other way to handle this would be to use $modx->runSnippet() in the major snippet to evaluate the other snippet.

      It might also work to call the second snippet before the major snippet if the second snippet’s output were stored as a placeholder via $modx->setPlaceholder, and use that placeholder in your chunk.

      You could also just make the code of the second snippet a function in the first one; remember that you must declare $modx as a global in functions.
        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
        • 7155
        • 160 Posts
        Quote from: sottwell at Jul 18, 2009, 07:15 AM

        Nested snippets have to be called the other way; the outer snippet uncached and the inner snippet cached.

        This is because the snippets are processed as they are found, so the outer snippet will get processed before the inner one, so the parameter is not yet available to the outer one. If it’s called uncached, it doesn’t get processed right away (its tags get converted to cached tags on this pass through the parser), while the inner one will. On the next pass through the parser, the outer snippet now gets processed, and since the inner one was processed earlier, the proper parameter value is now available as the outer snippet is evaluated.

        The other way to handle this would be to use $modx->runSnippet() in the major snippet to evaluate the other snippet.

        It might also work to call the second snippet before the major snippet if the second snippet’s output were stored as a placeholder via $modx->setPlaceholder, and use that placeholder in your chunk.

        You could also just make the code of the second snippet a function in the first one; remember that you must declare $modx as a global in functions.

        thanks for the info, susan.

        just to be clear. are you referring to my original problem? meaning that I will have to correct and make an uncached call of MajorSnippet and my template will have a cached call for TheOtherSnippet. Like so...

        [!MajorSnippet &tpl=`tplWithAnotherSnippetCall`!]
        


        tplWithAnotherSnippetCall is a chunk with the following contents:
        Name:[+name+]
        Profession:[+profession+]
        Subscription Choices:
        [[TheOtherSnippet]]
        


        OR

        were you talking about this one:

        [[MajorSnippet &tpl=`[!TheOtherSnippet!]`]]

        Though I havent implemented it yet(sigh...), according to u it should be [!MajorSnippet &tpl=`[[TheOtherSnippet]]`!]

        I guess they were both wrong,either way smiley

        I was just getting into the runSnippet() alternative.hopefully it or one of them will work.

        setPlaceholder hadnt worked. I had used it initially, but it wouldnt output anything. I posted a question on it yesterday http://modxcms.com/forums/index.php/topic,37749.0.html I had made the call before the Major snippet, but nothing showed. Just cant remember whether I had done the caching arrangements well.

        Will think about it and work it through. Hopefully I will solve it. Its been a bit painful but I’ve learnt alot on modx.
          • 7155
          • 160 Posts
          I ended up using the $modx->runSnippet method. It worked rather well