Displaying a snippet call above its output

Demo is a wrapper snippet that will display another snippet's call above the output from it. It takes as its parameters the normal parameters of the snippet being demonstrated, plus its two of its own: &name, the name of the snippet you want to run, and &cached, set to `1` if the snippet is being run cached. This parameter should reflect the cache status of Demo itself, so if Demo is run as [[Demo? ... ]], &cached should be set to 1, otherwise it should be set to 0 or omitted.

A typical Demo call might look like this: [!Demo? &name=`Ditto` &cached=`0` &parents=`66` &tpl=`news.tpl`!] This will run Ditto using the parameters supplied, inserting [!Ditto? &parents=`66` &tpl=`news.tpl`!] in a DIV above the Ditto output. Note the output of a space after the opening bracket and before the closing bracket of the snippet call in lines 5-9. This prevents the snippet from running, and is eliminated by the Brackify plugin for web page.

  1. <?php
  2. $cached = (isset($cached)) ? $cached : 1;
  3.  
  4. if ($cached == 1) {
  5.    $bracketLeft = '[ [';
  6.    $bracketRight = '] ]';
  7. } else {
  8.    $bracketLeft = '[ !';
  9.    $bracketRight = '! ]';
  10. }
  11.  
  12. $output = "\n".'<div class="snippetDemo">' . $bracketLeft.$name . '? ';
  13. foreach ($params as $paramName=>$paramValue) {
  14.    if ($paramName != 'name' && $paramName != 'cached') {
  15.      $output .= '&amp;'.$paramName.'=`'.$paramValue.'` ';
  16.    }
  17. }
  18.  
  19. $output .= $bracketRight . '</div>'."\n";
  20.  
  21. $output .= $modx->runSnippet($name,$params);
  22.  
  23. return $output;
  24. ?>