We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37099
    • 338 Posts
    I have a chunk that contains a snippet

    When I include the chunk in a document the snippet works fine, but if I include it in a module the snippet doesn’t work.

    eg:
    The code in the module:
    $params[’message’] = ’hello’;
    $form = $modx->parseChunk(’myChunk’,$params,’[+’,’+]’);
    echo $form;


    myChunk:

    [+message+]
    [!mySnippet? &value=`true`!]

    When i view the code the i get:

    hello
    [!mySnippet? &value=`true`!]


    So the param replaced ok but the snippet doesn’t get parsed/called/executed


    Any ideas
    Thanks
    Mike
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Module code doesn’t get passed through the document parser, so snippets won’t get evaluated. All the parseChunk does is replace placeholders as specified in its parameters. You would have to write your own code to parse the chunk for snippet tags, then run them through the runSnippet function.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 37099
        • 338 Posts
        OK, thanks, will give that a go.

        Cheers
        Mike
          • 37099
          • 338 Posts
          Here’s my solution,

          Get your chunk then pass it to runSnippetFromChunkForModule which should return the chunk containing the called snippets results . I’ve tested it with a chunk containing one snippet, and it works but not tried with more than 1.


          function runSnippetFromChunkForModule($string){
             global $modx;
             $snippets = array();
             $snippetPair = array();
             $snippetCallComponents = array();
          
             $explodedChunk = explode('[[',$string);
             foreach($explodedChunk as $snippet){
          
                $length = strpos($snippet, ']]');
                if($length){
                   $snippet = substr($snippet,0,$length);
                   $snippetCallComponents=parseSnippetCall($snippet);
                   $snippetOutput = $modx->runSnippet($snippetCallComponents[0] ,$snippetCallComponents[1]);
                   $newChunk = str_replace('[['.$snippet.']]',$snippetOutput,$string);
                }
             }
          
             return $newChunk;
          }
          
          function parseSnippetCall($string){
             $snippetCallBits = array();
             $snippetCallComponents = array();
             $snippetArgumentPairs = array();
             $argumentsBits = array();
             $arguments = array();
          
             $snippetCallBits = explode('?',$string);
             $snippetName = trim($snippetCallBits[0]);
             //$snippetName = substr($snippetName, 0, strlen($snippetName)-1);
             array_push($snippetCallComponents, $snippetName);
          
             $snippetArgumentPairs = explode('&', $snippetCallBits[1]);
          
             foreach($snippetArgumentPairs as $argumentPair){
               if(strpos($argumentPair,'=')){
                  $argumentBits = explode('=',$argumentPair);
                  $argumentName = trim($argumentBits[0]);
                  $argumentValue = substr($argumentBits[1],1,strlen($argumentBits[1])-2);
                  $arguments[$argumentName] = $argumentValue;
               }
             }
             array_push($snippetCallComponents, $arguments);
          
             return $snippetCallComponents;
          
          }