Skip to content

Instantly share code, notes, and snippets.

@lector1982
Forked from 64j/switch
Created May 19, 2021 06:49
Show Gist options
  • Select an option

  • Save lector1982/39b8e2ddd297daaa755cc42ece073158 to your computer and use it in GitHub Desktop.

Select an option

Save lector1982/39b8e2ddd297daaa755cc42ece073158 to your computer and use it in GitHub Desktop.
Snippet switch
<?php
$switch = !empty($switch) ? $switch : '';
$default = !empty($default) ? $default : '';
$out = '';
if (!empty($params)) {
$out = $default;
foreach($params as $k => $v) {
$value = mb_substr($k, 5);
if (mb_substr($k, 0, 5) === 'case:' && $value === $switch) {
$out = $v;
continue;
}
}
return $out != '' ? $this->parseText($modx->getTpl($out)) : '';
}
/*
[[switch?
&switch=`[*content*]`
&case:=`@CODE: --- CONTENT IS EMPTY ---`
&case:123=`@CODE: -- CONTENT IS 123 ---`
&default=`@CODE:<div>[*content*]</div>`
]]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment