We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4172
    • 5,888 Posts
    My Test-snippet parsechunk:
    <?php
    $daten=array('ph1'=>'erster','ph2'=>'zweiter','ph3'=>'dritter');
    return $modx->parseChunk('parsetest', $daten);
    ?>

    the chunk parsetest:
    [[+ph1]]<br/>
    [[+ph2:ucase]]<br/>
    [[+ph3]]<br/>

    snippet call:
    [[!parsechunk]]

    the output:
    erster
    
    dritter
    


    Phx-Filter seems not to work here, shouldn’t it work this way?

    Bruno
      -------------------------------

      you can buy me a beer, if you like MIGX

      http://webcmsolutions.de/migx.html

      Thanks!
      • 4172
      • 5,888 Posts
      added the parameter $chunkArr to
      if ($chunk= $this->getChunk($chunkName)) 

      and did work on my first test.
      function parseChunk($chunkName, $chunkArr, $prefix=’[[+’, $suffix=’]]’) {
      $chunk= ’’;
      if ($chunk= $this->getChunk($chunkName,$chunkArr)) {
      if(is_array($chunkArr)) {
      reset($chunkArr);
      while (list($key, $value)= each($chunkArr)) {
      $chunk= str_replace($prefix.$key.$suffix, $value, $chunk);
      }
      }
      }
      return $chunk;
      }
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 4172
        • 5,888 Posts
        Is it possible to have @FILE-Binding for $modx->parseChunk in revolution like this:
        $modx->parseChunk('@FILE'.$filename, $daten);

        I think it would be very useful to have @FILE and @CODE - Bindings here or is there a way to do this already?
          -------------------------------

          you can buy me a beer, if you like MIGX

          http://webcmsolutions.de/migx.html

          Thanks!
          • 4172
          • 5,888 Posts
          had only a short Test for this, but couldn’t we do it this way?:
          so we can get a filecontent before calling parseChunk and can send it with @CODE................................. in $chunkName
          for this test I need a chunk named ’empty’.

           
          <?php
          
              /**
               * Process and return the output from an HTML chunk by name.
               *
               * @param string $chunkName The name of the chunk.
               * @param array $properties An associative array of properties to process
               * the chunk with.
               * @return string The processed output of the chunk.
               */
              function getChunk($chunkName, $properties= array ()) {
                  $output= '';
                  if(substr($chunkName, 0, 5) == "@CODE") {
          			$content = trim(substr($chunkName, 5));
          			$chunk= $this->newObject('modChunk');
          			$output= $chunk->process($properties,$content);
          			//$chunkName='empty';
          		}			
                  elseif ($chunk= $this->getObject('modChunk', array ('name' => $chunkName), true)) {
                      $chunk->setCacheable(false);
                      $output= $chunk->process($properties);
                  }
                  return $output;
              }
          
              /**
               * Parse a chunk using an associative array of replacement variables.
               *
               * @param string $chunkName The name of the chunk.
               * @param array $chunkArr An array of properties to replace in the chunk.
               * @param string $prefix The placeholder prefix, defaults to [[+.
               * @param string $suffix The placeholder suffix, defaults to ]].
               * @return string The processed chunk with the placeholders replaced.
               */
              function parseChunk($chunkName, $chunkArr, $prefix='[[+', $suffix=']]') {
                  $chunk= '';
                  if ($chunk= $this->getChunk($chunkName,$chunkArr)) {
                      if(is_array($chunkArr)) {
                          reset($chunkArr);
                          while (list($key, $value)= each($chunkArr)) {
                              $chunk= str_replace($prefix.$key.$suffix, $value, $chunk);
                          }
                      }
                  }
                  return $chunk;
              }
          ?>
          
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 22303 MODX Staff
            • 10,725 Posts
            Quote from: Bruno17 at Dec 08, 2008, 07:35 AM

            had only a short Test for this, but couldn’t we do it this way?:
            so we can get a filecontent before calling parseChunk and can send it with @CODE................................. in $chunkName
            No, this needs to be done at the modElement level for all elements, and needs to be supported outside of the modX class functions like parseChunk() or getChunk(). That is the beauty of an OO API though, as the modElement class has getContent() and setContent() functions that can be used to implement similar behavior (though I do not think @CODE or @FILE should be used personally; this should be simply specified as the content in the database entry for the chunk), then you can do stuff like:
            [[!MySnippet? &tplFromChunk=`[[$MyChunk]]` &tplFromFile=`[[GetFileContent? &filename=`foo/bar/`]]`]]



            As for $modx->parseChunk(), this is a legacy function. Calling $modx->getChunk(’foo’, array(’bar’ => ’value’)) is now the preferred way to replace placeholders in a chunk, especially if you want to use filter commands or modifiers (i.e. like phX). It does not work with the MODx filters in parseChunk because the tag replacement is arbitrary and not processed by the parser which is responsible for handling the filter commands and modifiers. I think I can solve this by running it through the parser rather than doing a str_replace() there... more on this soon.
              • 4172
              • 5,888 Posts
              Ok, thanks for reply to this.
              So, if I understand, every snippet which handles with templates and/or chunks like Ditto, wayfinder, maxigallery and so on needs its own template-processor-class? I was hoping there is an easy way in revolution where you can send your placeholders and your template/chunk name or code to a given function and it would return the processed and PHx-filtered output-string as in my little experiment with
              $modx->parseChunk(’parsetest’, $daten);
              or
              $modx->parseChunk(’@CODE[[+ph1]]
              [[+ph2]]
              [[+ph3]]
              ’, $daten);
              or
              $modx->parseChunk(’@CODE’.$filecontent, $daten);

              [EDIT]

              Aah, I see, sorry for my stupidness. I can do it simple this way what I mean. Sometimes I need a little bit longer.

              <?php
              $filecontent='[[+ph1]],[[+ph2]],[[+ph3]]';
              $daten=array('ph1'=>'first','ph2'=>'second','ph3'=>'third');
              $chunk= $modx->newObject('modChunk'); 
              $chunk->setCacheable(false);
              $output= $chunk->process($daten,$filecontent);
              return $output;
              ?>
              

              or
              <?php
              $daten=array('ph1'=>'first','ph2'=>'second','ph3'=>'third');
              return $modx->getChunk('parsetest', $daten);
              ?>
              

                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 22303 MODX Staff
                • 10,725 Posts
                Quote from: Bruno17 at Dec 08, 2008, 03:02 PM

                So, if I understand, every snippet which handles with templates and/or chunks like Ditto, wayfinder, maxigallery and so on needs its own template-processor-class?
                Not at all. The problem is they now do all sorts of things internally to deal with input themselves (i.e. @CODE was made up by one of those snippets I believe, and has never been part of the core); they just need to accept the content as input, since that is now possible. You can pass content directly in tag strings now, so how it is retrieved is irrelevant. In addition, you can nest tags infinitely, so there is no limit to how you decide to get content in and out of your various elements.

                Quote from: Bruno17 at Dec 08, 2008, 03:02 PM

                I was hoping there is an easy way in revolution where you can send your placeholders and your template/chunk name or code to a given function and it would return the processed and PHx-filtered output-string as in my little experiment
                There is, for chunks:
                $modx->getChunk('parsetest', $daten);

                [[!$parsetest? &ph1=`value1` &ph2=`value2` &ph3=`value3`]]

                To parse your own content, you could create a transient modChunk instance at runtime and pass properties and content directly to the process() function:
                $content = file_get_contents(MODX_BASE_PATH . 'path/to/file/content.tpl');
                $chunk= $modx->newObject('modChunk');
                $output = $chunk->process($properties, $content);
                return $output;

                Snippetize that and you can easily use that anywhere, via API or tag.
                Or you can even use the parser directly, with full control over what it does with properties, non-cacheable and/or unprocessed tags, etc.:
                <?php
                $content = isset($content) ? $content : '[[+ph1]]<br/>[[+ph2]]<br/>[[+ph3]]<br/>';
                /* turn the script properties into placeholders and keep overwritten values for restoration */
                $restore = array();
                $placeholders = $scriptProperties;
                if (is_array($placeholders) && !empty($placeholders)) {
                    $restoreKeys = array_keys($placeholders);
                    foreach ($restoreKeys as $phKey) {
                        if (isset($modx->placeholders[$phKey])) $restore[$phKey]= $modx->getPlaceholder($phKey);
                    }
                    $modx->toPlaceholders($placeholders);
                }
                
                /* collect element tags in the output and process them */
                $maxIterations= isset ($modx->config['parser_max_iterations']) ? intval($modx->config['parser_max_iterations']) : 10;
                $modx->parser->processElementTags('', $content, false, false, '[[', ']]', array(), $maxIterations);
                
                /* remove the placeholders set from the properties of this script and restore global values */
                $modx->unsetPlaceholders(array_keys($scriptProperties));
                if ($restore) $modx->toPlaceholders($restore);
                return $content;
                ?>

                With a little imagination, you can create all kinds of general purpose snippets to manage parsing content from anywhere, including outside of core MODx elements (i.e. chunks, TV’s, etc.).

                And soon, Revolution will support file-based content for all chunks directly, so if you want the content managed in an external file, you will be able to use it without doing anything special; it’ll just be an element that references it’s source content from the file-system. But this feature will likely be in a release after the initial one.
                  • 4172
                  • 5,888 Posts
                  Thanks for your explanation. Now I’m hoping, have gotten it. This all is really very well elaborated and makes now sense to me.

                  Quote from: OpenGeek at Dec 08, 2008, 04:33 PM

                  And soon, Revolution will support file-based content for all chunks directly, so if you want the content managed in an external file, you will be able to use it without doing anything special; it’ll just be an element that references it’s source content from the file-system. But this feature will likely be in a release after the initial one.

                  Looking forward to see this feature in one of the coming releases.
                  Thank you!
                    -------------------------------

                    you can buy me a beer, if you like MIGX

                    http://webcmsolutions.de/migx.html

                    Thanks!