We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37111 ☆ A M B ☆
    • 31 Posts
    Hey guys,
    I'm searching for a code to save a completely rendered (no more MODx-Tags are included in the cached value) version of a chunk to a custom cache.
    I need this, to cache prerendered versions of a chunk, which includes a lot of modx-tags, that really slow down the system, but are needed to get the endresult.
    I want to parse a whole chunk with all tags included and after the rendering, it should be saved only for the actual url (like 'preRender//ACTUAL-URL'.

    With this snippet down there, I can already save the url-specified chunk correctly to the cache and read it from the cache after it was saved. But the cached value includes
    all the modx-tags - and so I have no benefit from caching, because the modx-parser has to do his job too with the cached version.

    I think, I have to do it with a plugin, which saves the rendered chunk after MODX has rendered the whole template / chunks / snippets, but I don't know, how to do it exactly,
    because I need the prerendered version of the chunk also for displaying on the actual site. Its a little hard to unerstand I think, so I will try to explain it easier:

    The user loads the site www.example.com/test
    The Plugin/Snippet renders the the chunk "mainContent" completely, without leaving any MODX-Tags in it
    The Plugin/Snippet saves the prerendered chunk to the cache as "preRender///test" - according to the acutal url
    And now the Plugin/Snippet has to return/set a Placeholder the prerendered chunk for displaying it on the correct location in the template

    The user reloads the site www.example.com/test
    The Plugin looks if it can find a prerendered version of the actual MainContent-Chunk for the URL "/test" (CacheName = preRender///test)
    It finds the prerendered version from above and just returns/set it as as Placeholder for displaying it on the correct location in the template

    If the user now goes to another page like www.example.com/test2 all starts from the beginning and on the first load all gets rendered, displayed and cached,
    after the second reload of the same page, it just loads and displays the prerendered version of it.

    <?php
    // generate a unique cache-name for the acutal URL
    $cacheName = 'preRender//'.$_SERVER["REQUEST_URI"];
    
    // try to get a prerendered chunk from the cache
    $fromCache = $modx->cacheManager->get($cacheName);
    
    // if the result is empty, the mainContent-Chunk has to be parsed completely (without leaving a MODX tag inside) and has to be cached - AND returned as placeholder (not included yet)
    if(empty($fromCache)){
     echo "<strong style='color:#bb0000;'>NOT FROM CACHE :(</strong><hr> ";
     
     // get the mainContent-Chunk
     $str = $modx->getChunk('mainContent');
    
    // parse the chunk -> http://rtfm.modx.com/display/revolution20/modX.getChunk (last example)
    // but it is not working for what i need.. It only replaces tags, I set directly in $chunk->process(HERE, $str)
    $uniqid = uniqid();
    $chunk = $modx->newObject('modChunk', array('name' => $cacheName));
    $chunk->setCacheable(false);
    $str= $chunk->process(array(), $str);
    
    // save the prerendered chunk to the cache 
     $modx->cacheManager->set($cacheName, $str);
    
    // show the cached value
     echo "<textarea>".$str."</textarea>";
    
    }else{
     echo "<strong style='color:#00bb00;'>FROM CACHE :)</strong><hr>";
    
    // show the value from the cache
      echo "<textarea>".$fromCache."</textarea>";
    }
    // if you now let the script die, all tags inside the mainContent-Chunk are NOT parsed - so the cached version also includes all the MODX-Tags too
    die();
    


    if you comment the die(); out, it seems, that all worked correctly, because all tags get rendered after the snippet call - so thats, why I think
    this must be done in a plugin... But how can I get the complete prerendered version of the chunk? And how can I place the prerendered version for the actual (first) user?

    Thanks for all your ideas! smiley

    Greetings Jens [ed. note: vamporio last edited this post 13 years, 6 months ago.]
      • 45681
      • 8 Posts
      I apologize for the necroposting, but this is the exact same issue I'm having. I'd like to cache the parsed result of a getChunk() call, but from the way MODx apparently parses the placeholders this is not possible - as explained by the post above.
      Is there a way to force complete chunk rendering?