We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14951
    • 151 Posts
    Hello there,

    I have went through the documentation pretty extensively on calling a chunk from within a snippet. I wrote a simple snippet to display a chunk based upon the current day/time. It seems to work fine if I just echo out a string so I assume the rest of my code is at least functional. Could someone perhaps give me a little insight on how to get it to process a chunk? Also, I’m very much a PHP novice...about 4 chapters into my PHP book wink so if you happen to have any tips on how I can make this code more efficient, I’d really appreciate any insight in that department. Thanks in advance for any assistance! Alas...my snippet:

    //EDIT: One other kicker, I forgot to mention.... each chunk that I’m calling contains a snippet call. If there is a better way to do this...let me know smiley
    <?php
    // Define Variables
    $dfDay = date('N');
    $dfTime = date('G');
    
    // Main Functions
    
    function dfWeekDay($dfTime) {
    	global $modx;
    	if ($dfTime >= 1 && $dfTime <= 15) {
    		$dfChunk = $modx->parseChunk('lnFeature', $chunkArr, '{{', '}}');
    	} else if ($dfTime > 15 && $dfTime <= 24) {
    		$dfChunk = $modx->parseChunk('dnFeature', $chunkArr, '{{', '}}');
    	} else {
    		echo "You have serious time issues!";
    	}
    	return $dfChunk;
    }
    
    function dfWeekEnd($dfTime) {
    	global $modx;
    	if ($dfTime >= 1 && $dfTime <= 11) {
    		$dfChunk = $modx->parseChunk('bfFeature', $chunkArr, '{{', '}}');
    	} else if ($dfTime > 11 && $dfTime <= 15) {
    		$dfChunk = $modx->parseChunk('lnFeature', $chunkArr, '{{', '}}');
    	} else if ($dfTime > 15 && $dfTime <= 24) {
    		$dfChunk = $modx->parseChunk('dnFeature', $chunkArr, '{{', '}}');
    	} else {
    		echo "You have serious time issues!";
    	}
    	return $dfChunk;
    }
    
    // Determine Day of week and call appropriate function
    
    if ($dfDay < 6) {
    	$result = dfWeekDay($dfTime);
    } else if ($dfDay > 5) {
    	$result = dfWeekEnd($dfTime);
    } else {
    	echo "You have serious day issues!";
    }
    return $result;
    ?>
    

      • 28042 ☆ A M B ☆
      • 24,524 Posts
      No, a snippet loaded via getChunk or parseChunk is not processed through the parser, so snippets, TVs and other MODx tags won’t be evaluated.

      parseChunk lets you use placeholders set by the calling snippet; it merges the placeholders in the chunk with their values. So you would need to have the snippet code that’s in the chunk snippet now in your calling snippet, and set placeholders.
        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
        • 14951
        • 151 Posts
        Although I have no intention of contradicting your knowledge, the snippet that you wrote using getchunk seems to work just fine with a snippet nested in the chunk. smiley

        In case anyone else is interested in something similar do you mind if I post it here?
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          That’s not because the snippet was processed by the getChunk function, it’s because the document went through the main parser again after the chunk content containing snippet tags was inserted. There may be circumstances where it’s reached the max number of passes through the parser, although it probably usually will go through again. Just don’t be too surprised someday if such a getChunk function call doesn’t produce the expected results.
            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
            • 29796
            • 91 Posts
            i’m new in modx and i have some wuestions about snipets and parsing chunks .. I wanted to do somethig but without success so i need a litle help ..

            I want to call a snippet in a template, and that snippet will display a chunk in a loopt ... so i want that the snippet do a loop of chunks, and fill it each loop with different data .. i want to create some kind of list ...

            in this example i wanted to send count $i data to the chunk

            i tryed in this way but didn’t worked:

            TEMPLATE:
            [[test_snippet]]

            SNIPPET test_snippet
            for ($i = 1; $i <= 5; $i++) {
            $modx->setPlaceholder(’xxx’,$i);
            echo ’{{test}}’;
            }

            CHUNK {{test}}
            <div> [+xxx+] </div>


            RESULT: i get chunk displayed 5 times with value 5 .. i didnt get each loop right no. but i got the last one ..

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

            Then i tryed:

            SNIPPET test_snippet
            for ($i = 1; $i <= 5; $i++) {
            $x = ’{{test}}’;
            $x = str_replace(’[+xxx+]’,$i,$x);
            echo $x;
            }

            no success ...so i tryed

            SNIPPET test_snippet
            for ($i = 1; $i <= 5; $i++) {
            $chunkArr = array(’xxx’=>’aaa’);
            $test = $modx->parseChunk(’test’, $chunkArr, ’[+’, ’+]’);
            echo $test;
            }

            ..still no success ..



            How i can do this ? .. a snippet wich gather data and for displaying it use a chunk ?
              • 3749
              • 24,544 Posts
              Build the output in your snippet:

              <?php
              $output = '';
              for ($i = 1; $i <= 5; $i++) {
                   $temp= $modx->getChunk('test');
                   $temp = $str_replace('[+xxx+]',$i,$temp);
                   $output .= $temp;
              }
              return $output;
              


                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting