-
☆ A M B ☆
- 91 Posts
Made a simple case snippet for when you would have too many "If" snippet calls. Currently only supports "equals".
Example call: [[Case? &subject=`[[*color]]` &case1=`blue` &then1=`Violets are Blue` &case2=`red` &then2=`Roses are Red` &default=`Not red or blue!`]]
<?php
/**
* Simple case (conditional) snippet
*/
$output = '';
if (!empty($debug)) {
$output = print_r($scriptProperties,1);
if (!empty($die)) die();
}
$modx->parser->processElementTags('',$subject,true,true);
$subject = !isset($subject) ? '' : $subject;
$caseArray = array();
$thenArray = array();
$resortToDefault = 1;
for($i=1;;$i++) {
$caseVar="case".$i;
$thenVar="then".$i;
if (empty(${$caseVar}) && empty(${$thenVar})) {break;}
else {
$caseArray[$i]=isset(${$caseVar}) ? ${$caseVar} : '';
$thenArray[$i]=isset(${$thenVar}) ? ${$thenVar} : '';
}
if ($subject == $caseArray[$i]) {
$output .= $thenArray[$i];
$resortToDefault = 0;
break;
}
}
if ($resortToDefault) {$output.=isset($default) ? $default : '';}
if ($debug) {
$output .= "\n<br />\n".print_r($caseArray,1)."\n<br />\n".print_r($thenArray,1);
}
unset($subject);
return $output;
WebsiteZen.com - MODX and E-Commerce web development in the San Francisco Bay Area
Thanks! Great. I will try it soon.
I think, thererfor I am! But what I am, and why...?
Nice!
You probably already know that you can do something similar with a checkbox or drop-down list TV with the Input Option Values set to:
blue==Violets are Blue||red==Roses are Red
and a default value of "neither red nor blue"
I can see where your snippet would be great in certain situations, though.
Just a thought, you could allow the user to prefix the values with a token that would make the snippet retrieve a chunk using the value (minus the token) as the chunk name with $modx->getChunk() instead of using the literal values.
-
☆ A M B ☆
- 91 Posts
I am actually working on a form-generating system that would make it easy to make huge, complex forms using the Loop snippet (
http://modxcms.com/forums/index.php/topic,60559.0.html). I made the Case snippet to avoid having to use a different chunk for every single field type: [[Case? &subject=`[[+type]]` &case1=`text` etc...]].
By the way, it’s working amazingly well, with just a minor caching issue (I am a big fan of caching):
http://modxcms.com/forums/index.php/topic,61050.0.html
Forgive my noobishness, but what is a token?
WebsiteZen.com - MODX and E-Commerce web development in the San Francisco Bay Area
Quote from: Oleg at Feb 13, 2011, 11:48 PM
I am actually working on a form-generating system that would make it easy to make huge, complex forms using the Loop snippet (http://modxcms.com/forums/index.php/topic,60559.0.html). I made the Case snippet to avoid having to use a different chunk for every single field type: [[Case? &subject=`[[+type]]` &case1=`text` etc...]].
By the way, it’s working amazingly well, with just a minor caching issue (I am a big fan of caching): http://modxcms.com/forums/index.php/topic,61050.0.html
Forgive my noobishness, but what is a token?
I just meant to have some character at the beginning of the parameter value; e.g., &red=`#chunkName` or &red=`$chunkName`. The snippet would just check for the token and, if found, remove it and use the rest of the value as the chunk name.
-
☆ A M B ☆
- 91 Posts
Great idea! I’ll add that in.
WebsiteZen.com - MODX and E-Commerce web development in the San Francisco Bay Area