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

    I’ve been creating a resource list snippet by following the documentation here http://rtfm.modx.com/display/revolution20/Templating+Your+Snippets

    I’m keen to store my snippets, chunks and templates in external files so I can version them. How do I do this when I’ve used getChunk?

    The chunk name i pass to getChunk is trying to include an external file by using a snippet which looks like this:

    <?php
    $tpl = $config['base_path'] . $file;
    ob_start();
    include($tpl);
    return ob_get_clean();


    The external chunk is included but the variables in it are not replace with those passed to getChunk.

    Is it possible to do this?

    Thanks in advance.






      • 22303 MODX Staff
      • 10,725 Posts
      MODX does not yet support using external files as Chunks in the core yet, so your Snippets will have to decide whether to use getChunk() to actually get and process a chunk from the database, or use your custom method of loading the chunk from file (and processing it). That said, here is a better way to get your chunk and process it with the properties being passed to the Snippet (or whatever you would pass as properties to the getChunk() call):
      <?php
      $tpl = file_get_contents($tplFile);
      $chunk = $modx->newObject('modChunk');
      $chunk->setCacheable(false);
      $output = $chunk->process($scriptProperties, $tpl);
      

      With this approach, until we have native support for file-based Elements, you can at least get the same behavior as using a modChunk instance from the database.
        • 6535
        • 2 Posts
        Thanks, that works.

        I thought I was pushing it a bit by having a snippet call a chunk which contained a snippet which included a chunk!
          • 17499 ☆ A M B ☆
          • 872 Posts
          I see you use the method ->setCache(false), does it mean that the chunk is never put in cache and there always fetch from the filesystem submitted path ?
          Don’t we lose perfomance for rendering and caching using this method ?

          Can we use analogic method for snippets ?
            • 22303 MODX Staff
            • 10,725 Posts
            Quote from: lossendae at Mar 11, 2011, 07:42 AM

            I see you use the method ->setCache(false), does it mean that the chunk is never put in cache and there always fetch from the filesystem submitted path ?
            Don’t we lose perfomance for rendering and caching using this method ?

            Can we use analogic method for snippets ?
            It means the output is not cacheable, which you don’t want if being used multiple times on a page.

            This is not possible with Snippets this way, but all you need to do is create one include Snippet if you want to put your Snippet code on the filesystem. Just realize you will not benefit at all from caching strategies and will be adding an extra include anytime you run the Snippet (especially bad if you use it multiple times on a page).
              • 17499 ☆ A M B ☆
              • 872 Posts
              That’s what i thought!

              To resume, Chunk from filesystem is possible without losing much performance (if none) using a similar method to yours, and snippet are better in the database to benefit from the caching system.
                • 22303 MODX Staff
                • 10,725 Posts
                Well, let’s just say using external files is simply going to be slightly slower in most situations and is not going to be able to take advantage of all the internal optimizations and caching strategies for those Elements, regardless of Snippets or Chunks or Templates. The native support will solve this.
                  • 30138
                  • 11 Posts
                  I’m using the code suggested above to process a chunk stored in a file

                  <?php
                  $tpl = file_get_contents($tplFile);
                  $chunk = $modx->newObject('modChunk');
                  $chunk->setCacheable(false);
                  $output = $chunk->process($scriptProperties, $tpl);
                  


                  but I have chunks with element tags in them e.g.
                  
                  Some stuff up here
                  [[!Login]]
                  
                  


                  when $chunk->process is called the [[!Login]] element is not processed and the text [[!Login]] is just displayed in the output.

                  I’ve looked into a little bit and made a change in modchunk->process

                  I changed this line:
                  $this->xpdo->parser->processElementTags($this->_tag, $this->_output, false, false, '[[', ']]', array(), $maxIterations);
                  


                  to this:
                  $this->xpdo->parser->processElementTags($this->_tag, $this->_output, true, true, '[[', ']]', array(), $maxIterations);
                  


                  So now its processing uncachable elements and removing unprocessed elements.

                  This of course is just hacking and I have no idea of the wider impacts of this change. Is there a better way around this problem??

                    • 22303 MODX Staff
                    • 10,725 Posts
                    Quote from: tjjjohnson at Mar 14, 2011, 10:50 PM

                    So now its processing uncachable elements and removing unprocessed elements.

                    This of course is just hacking and I have no idea of the wider impacts of this change. Is there a better way around this problem??
                    Those uncached tags will now be cached if you do that. That will not work. The Login snippet and any other element tags should be getting processed on the next pass of the parser, so you must be doing something else wrong.
                      • 30138
                      • 11 Posts
                      I was retrieving the chunk in an ajax call (maybe I should have said that smiley so the complete parsing process wasn’t taking place.

                      My code was like this:
                      $('#AuthenticatorLoginFormWrapper').load('assets/services/getAuthenticatorLoginForm.php', function() {
                        //do stuff
                      });
                      


                      I setup an element and snippet to handle the request instead of just a bare php file.
                      $('#AuthenticatorLoginFormWrapper').load('services/getauthenticatorloginform', function() {
                        //do stuff
                      });
                      


                      It’s a little annoying having to setup an element and snippet for each service, which I then have to create these on the server when I deploy it ... is there a way to make this process easier?