Hiya,
Just wondering if anyone can give a really quick example of how you go about formatting variables output by a custom plugin?
Say you have this sort of thing :
{exp:my_custom_plugin}
{my_custom_field format="%Y"}
{/exp:my_custom_plugin}At the moment let’s say that all the plugin does is brings back the join_date for a member. Currently that would come back as as Unix timestamp such as 1305127256.
I’d therefore like to be able to output using the format=”“ parameter but not really too sure how to go about it.
Also how do you check for each variable in a plugin to see if you need to do this or not? Let’s say you can specify in the plugin items to bring back and some may be date fields and some may not. How would you go about just performing the formatting to those that have the format=”“ parameter in them?
In other words if you had this kind of thing :
{exp:my_custom_plugin}
{custom_field_one}
{custom_field_two format="%d %m %Y"}
{/exp:my_custom_plugin}In the example above {custom_field_one} would just be some text to output whereas {custom_field_two} would be a date field that needs to be formatted as shown.
At the moment in plugins I’ve been using this sort of code :
foreach($query->result as $row)
{
$tagdata = $TMPL->tagdata;
foreach ($TMPL->var_single as $key => $val)
{
if ($key == "count")
{
$tagdata = $TMPL->swap_var_single($key, $count, $tagdata);
}
if (isset($row[$val]))
{
$tagdata = $TMPL->swap_var_single($val, $row[$val], $tagdata);
}
}
$count ++;
$this->return_data .= $tagdata;
}
return $tagdata;As you can see from this part of the code shown above :
if (isset($row[$val]))
{
$tagdata = $TMPL->swap_var_single($val, $row[$val], $tagdata);
}This programatically swaps out the variables for me but how do I now go about adding in there if I need to format some of them?
Any help would be greatly appreciated.
Best wishes,
Mark
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.