Hiya,
After making another plugin today I was wondering if there might be an easier way of doing something so I thought I would ask the fantastic people here in the forums 😊
In the plugin there is this kind of code shown below :
foreach ($TMPL->var_single as $key => $val)
{
if ($key == "days")
{
$tagdata = $TMPL->swap_var_single($key, $days_left, $tagdata);
}
if ($key == "hours")
{
$tagdata = $TMPL->swap_var_single($key, $hours_left, $tagdata);
}
if ($key == "minutes")
{
$tagdata = $TMPL->swap_var_single($key, $minutes_left, $tagdata);
}
if ($key == "event")
{
$tagdata = $TMPL->swap_var_single($key, $event, $tagdata);
}
}
All nice and simple and as there are only 4 variables being replaced not too difficult. Suppose now though I had a plugin that was replacing 30 or 40 so variables. There must be a much quicker way for me to do all of this surely?
I’m guessing perhaps if I could place all my names of my variables and what they should be replaced with into an array then that might do it perhaps?
Only thing is I’m not that fantastic with arrays so was wondering if that is indeed the best way to go about this or perhaps there is an even easier way?
I tried looking into the mod.weblog.php file to see if I could glean anything from that but I think I just confused myself even more as there are a lot of lines in that file!! 😊 😉 😊
So if anyone has an inkling of what I’m on about and can lend a hand then I would be very grateful. I’d really like to find some method so that I can exceptionally easily add in new variables at any time without having to write 3 lines of code every time not only to save a bit of time and code but also to make things a little neater in my plugins in the long run. I do have one plugin which I made which is replacing nearly 60 variables so you can guess what that part looks like! 😉
Best wishes,
Mark
well there’s always using switch/case instead of if/else… but that’s mostly just a syntax change and not really a solution to the long list of replacements.
in some cases it might work to create your variables in an associative array indexed by tag name:
$values["days"] = "2";
$values["hours"] = "48";
...
$tagdata = $TMPL->swap_var_single($key, $values[$key], $tagdata);
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.