We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19534
    • 183 Posts
    Can someone tell me why a chunk is being cached after processing it, although cachableis set to false?

    Here is the code where $this->cache(); is called without checking the value of $this->_cacheable

        public function process($properties= null, $content= null) {
            parent :: process($properties, $content);
            if (!$this->_processed) {
                /* copy the content into the output buffer */
                $this->_output= $this->_content;
                if (is_string($this->_output) && !empty ($this->_output)) {
                    /* turn the processed properties into placeholders */
                    $scope = $this->xpdo->toPlaceholders($this->_properties, '', '.', true);
    
                    /* collect element tags in the output and process them */
                    $maxIterations= intval($this->xpdo->getOption('parser_max_iterations',null,10));
                    $this->xpdo->parser->processElementTags($this->_tag, $this->_output, false, false, '[[', ']]', array(), $maxIterations);
    
                    /* remove the placeholders set from the properties of this element and restore global values */
                    if (isset($scope['keys'])) $this->xpdo->unsetPlaceholders($scope['keys']);
                    if (isset($scope['restore'])) $this->xpdo->toPlaceholders($scope['restore']);
                }
                $this->filterOutput();
                $this->cache();
                $this->_processed= true;
            }
    
            /* finally, return the processed element content */
            return $this->_output;
        }
    
      Add-On to easily manage your multilingual sites: Babel
      • 28215
      • 4,149 Posts
      The method you refer to, in modelement.class.php:

          public function cache() {
              if ($this->isCacheable()) {
                  $this->xpdo->elementCache[$this->_tag]= $this->_output;
              }
          }
      


      ;)
        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 19534
        • 183 Posts
        Thanks for your answer.
        Yes, you’re right. I didn’t explain my problem correctly (or I am missunderstanding the caching system). My problem is related to $this->_processed which seems to be connected somehow to the caching of a chunk’s output.

        Here is what I want to do:

        1. Load an existing chunk via $chunk = $modx->getObject(’modChunk’,...)
        2. Set it to non cacheable: $chunk->setCacheable(false);
        3. Loop through an array which contains different values for the placeholders defined in the chunk and proccess the chunk for each array element. Something like this:

        <?php
        $chunk->setCacheable(false);
        $output = '';
        foreach($xyzs as $xyz) {
          $output .= $chunk->process($xyz);
        }
        


        This doesn’t work because in the process method of modChunk $this->_processed is set to true without checking/invoking isCacheable() before (but this is done in modElement).

        There is no public method available to set $this->_processed = false in order to recreate the output with the new placeholder values. So you are not able to process a single chunk object more than once with different placeholders(?). I was only able to do this by creating a new chunk object with the some content data:
        <?php
        $output = '';
        $chunkData = $chunk->toArray('', true);
        foreach($xyzs as xyz) {
          $chunk = $modx->newObject('modChunk');
          $chunk->fromArray($chunkData, '', true, true, true);
          $output .= $chunk->process($xyz);
        }
        


        I’m wondering if this is a clean way or if I’m not understanding the caching system....

        I hope this explanation is clearer.

        Thanks,
        Jakob
          Add-On to easily manage your multilingual sites: Babel
          • 22303 MODX Staff
          • 10,725 Posts
          Great analysis Jakob and thanks for detailing the issue—could you file an improvement ticket in our bug tracker so we can address this issue ASAP. I think it would be best to have a public method to manage the $_processed field for Elements and we can consider having the cacheable flag determine if _processed is automatically reset somehow.
            • 19534
            • 183 Posts
            You can find the ticket here: http://bugs.modx.com/issues/3784
              Add-On to easily manage your multilingual sites: Babel