Hi there,
I was just wondering if someone could possibly explain the following snippet of code to me (hope it’s okay to post as I have seen it in other people’s modules etc)
/** ----------------------------------------
/** parse {switch} variable
/** ----------------------------------------*/
if (preg_match("/^switch\s*=.+/i", $key))
{
$sparam = $FNS->assign_parameters($key);
$sw = '';
if (isset($sparam['switch']))
{
$sopt = explode("|", $sparam['switch']);
$sw = $sopt[($count + count($sopt)) % count($sopt)];
}
$tagdata = $TMPL->swap_var_single($key, $sw, $tagdata);
}
I would like to use the {switch="1|2|3|4"} variable within a plugin that I am currently writing but not really too sure where to place this code to get it to work correctly. At the moment my plugin is looping all correctly and spitting out all other data that I want but if I place :
{exp:my_plugin}
{switch="1|2|3|4"} - {my_plugin_variable}
{/exp:my_plugin}
into my template even though my plugin loops fine and spits out all the item information that I want I only ever get 1 being spat out multiple times from the switch variable.
Anyone any ideas perhaps?
Thanks in advance.
Best wishes,
Mark
Hmm!!
I really should look more carefully before posting!!
Seems if I do this :
/** ----------------------------------------
/** parse {switch} variable
/** ----------------------------------------*/
foreach ($TMPL->var_single as $key => $val)
{
if (preg_match("/^switch\s*=.+/i", $key))
{
$sparam = $FNS->assign_parameters($key);
$sw = '';
if (isset($sparam['switch']))
{
$sopt = explode("|", $sparam['switch']);
$sw = $sopt[($count + count($sopt)) % count($sopt)];
}
$tagdata = $TMPL->swap_var_single($key, $sw, $tagdata);
}
}
It now all works!
Just wondering though am I doing this all correctly. Don’t want to carry on if it is indeed the wrong way to do this so would love to know before carrying on. Also if anyone is feeling really really generous it would be excellent to understand in lay-mens terms exactly what the code is doing?
Best wishes,
Mark
It’s done like that in most of EE, so I would say you’re good to go. What part of the code do you have trouble with?
Very rough review:
foreach ($TMPL->var_single as $key => $val)
You probably know this, it loops through the array of single tags. $key is each variable’s name and $val is the full variable. So for each single variable it runs the checks inside.
if (preg_match("/^switch\s*=.+/i", $key))
Anything that doesn’t fit the pattern: switch[spaces]=[morethanonecharacter] does not get processed. Basically filters out all tags except for the switch one.
$sparam = $FNS->assign_parameters($key);
Gets the tag’s parameters and drops them into an array.
if (isset($sparam['switch']))
Makes sure that there is an actual switch parameter to process.
$sw = $sopt[($count + count($sopt)) % count($sopt)];
Take the $count (current iteration), divide it by the total possible and get the remainder. Then the remainder is used as the array key to get the switch value.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.