RandomChunk is a MODX snippet that provides a simple and efficient way to randomly generate one chunk from a list of chunks. The snippet only takes one parameter which is equal to a comma delineated list of chunk names. An example call is below.

[!RandomChunk? &chunks=`chunk1, chunk2`!]

Here is the code for the snippet.

<?php
	//Salvatore Sodano
	//http://salscode.com
	//[!RandomChunk? &chunks=`chunk1, chunk2`!]
	//Randomly generates a chunk from a comma delineated list of chunks.
	$chunks = str_ireplace(" ","",$chunks);
	$charray = explode(",",$chunks);
	$count = count($charray) - 1;
	$rand = rand(0,$count);
	$result = $charray[$rand];
	echo "{{".$result."}}";
?>

Download

If you would like to download a text file of the above code, click here.

Instructions

Copy and paste the snippet code into a new snippet called "RandomChunk". Then simply call the snippet anywhere you would like to generate a chunk randomly. Since the snippet delivers content randomly, an un-cached snippet call is the way to go.


Hosted By:

SkyToaster, Simple Quality Hosting