We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

How do you add in date formatting to variables output in a custom plugin?

Development and Programming

Mark Bowen's avatar
Mark Bowen
12,637 posts
14 years ago
Mark Bowen's avatar Mark Bowen

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

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
14 years ago
Mark Bowen's avatar Mark Bowen

Never mind.

After taking a look at the weblog entries tag and almost fainting 😉 I took a look at the query module tag and found what I needed.

Nice and easy.

Best wishes,

Mark

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.