On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • Hey modx crowd.

    I have a problem: in a snippet I get the code of a chunk via the getchunk method. When I use echo to show it on the screen, snippets which are in the chunk are not executed. Is there a way to parse the chunk instead of just "echoeing" it, so that snippets are executed?

    Many thanks,

    Sebastian
    • http://wiki.modxcms.com/index.php/API:parseChunk

      edit: doesn’t work as I thought it would sad
      • Using $modx->parseChunk() wouldn’t generate output for the snippet calls in the chunk - it’s only designed to parse placeholders into the chunk.

        So, you could use $modx->runSnippet() to manually get output for each of the snippets used in the chunk and then use $modx->parseChunk() to parse in the snippet output into the chunk.
          Garry Nutting
          Senior Developer
          MODX, LLC

          Email: support@modx.com
          Twitter: @garryn
          Web: modx.com
        • Many thanks for the fast reply!

          The runSnippet() method I also saw in the wiki. However, I will probably rather rethink my entire concept. The original plan was the following (not sure anybody finds that interesting but I write it off my mind now wink ): I have a column on my website where I want to put boxes containing content teasers, tag clouds, announcements, etc. Each box is saved as a chunk. Different sites contain different boxes.

          My solution: I define a TV and enter for each site a comma denominated list with the names of the chunks (boxes) I want in that column. A snippet reads out the TV and subsequently opens each of these chunks. So far quite elegant, I thought. The limit is the snippets in the chunks. Many of my boxes have dynamic content and thus require snippets to be executed...

          The code looked like this:

          $sbar_elements = explode(",",$modx->documentObject['GPGSidebar'][1]);
          foreach ($sbar_elements as $value) 
          {
            $chunkOutput = $modx->getChunk($value);
            if($chunkOutput)
            {
              echo "<p>".$chunkOutput."</p>";
            }
          }


          I will opt for a work around but maybe it would be interesting for the future to have a more advanced version of the function parseChunk() which treats the chunk as if it was regularly included into the site via {{...}} rather than just echoing it as text.

          Again, many thanks!!

          • Hello again,
            in case others have the same problem one day. I found the function I was looking for in the document.parser.class.inc.php file. It is called evalSnippets(). You simply send the code of a chunk to this function and all snippets in the chunk will be executed. The output is returned as a string. Awsome!

            (Important, the snippet needs to be formatted as [[...]] rather than [!...!])

            $chunkOutput = $modx->getChunk($nameOfChunk);
            $chunkOutputEvaluated = $modx->evalSnippets($chunkOutput);
            echo $chunkOutputEvaluated;
            

            • That’s an internal parser function, not really intended for API use. But if it works, I guess there’s no big issue with it.
                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
              • Susan is correct, it is not part of the public API and this function will be gone in future releases, so do not use this in components if you want to protect your upgrade path. Obviously, with the state of the current code, it may be the only solution, and if you must use it, try to isolate your usage to a custom class that you re-use and can easily refactor when the new parser is available for use and you decide you want to upgrade.

                FWIW, the future core has a fully recursive, source order parsing algorithm, that will avoid this issue, as well as a singular model of parser execution by which any MODx element (chunk, snippet, or otherwise) can be processed by simply instantiating the object and calling the process() function, as you can see from the implementation of the getChunk function for 0.9.7:
                <?php
                    function getChunk($chunkName, $properties= array ()) {
                        $output= '';
                        if ($chunk= $this->getObject('modChunk', array ('name' => $chunkName), true)) {
                            $output= $chunk->process($properties);
                        }
                        return $output;
                    }
                ?>

                There is also a new parameter called properties that allows you to pass in an associative array of replacement variables that will be prefixed with the name of the chunk, e.g. [[+chunkName.var1]] would be a valid placeholder which would return ’the value of var’ when calling the function like so:
                <?php
                $output = $modx->getChunk('chunkName', array('var' => 'the value of var'));
                ?>
                • The 0.9.6 parser is pseudo-recursive, so returning a string from a snippet that contains another snippet call should work. Try building everything into a string and then [tt]return[/tt] rather than echo it. However, the system is only recursive to 10 levels deep, so if you have a very complex site you may be reaching this limit.

                  Quote from: OpenGeek at Feb 18, 2008, 10:16 PM

                  There is also a new parameter called properties that allows you to pass in an associative array of replacement variables that will be prefixed with the name of the chunk, e.g. [[+chunkName.var1]] would be a valid placeholder which would return ’the value of var’ when calling the function like so:
                  <?php
                  $output = $modx->getChunk('chunkName', array('var' => 'the value of var'));
                  ?>

                  OpenGeek, is this something that I could implement for the next release of 0.9.6? It seems like an excellent feature that people would use.
                  • I know this has been here for a while, but I just ran into the same problem.
                    It turns out, that if you use non-cacheable snippet calls, the parser delays their processing
                    so it just echos the chunk. I racked my brain for a day and a half over this when it was just about changing a ! to a [.

                    I’m a newbie to the modx framework, but so far so good.

                    cheers
                    • donshakespeare Reply #10, 2 years ago
                      Yea, me too. Snippets with ! will not be parsed
                        TinymceWrapper: Complete back/frontend content solution.
                        Harden your MODX site by passwording your three main folders: core, manager, connectors and renaming your assets (thank me later!)
                        5 ways to sniff / hack your own sites; even with renamed/hidden folders, burst them all up, to see how secure you are not.