OxyScripts.com
Menu spacer Home Tutorials Articles Code Forums irc.freenode.net #oxyscripts
Main (PHP)
Home Forums PHP News PHP Tutorials Articles PHP Code Snippets Contact Us Sysadmin Resources Books Template Shop
3rd Party Streams
SlashDot PHPDeveloper.org PHP.Net
Resources
PHP Manual MySQL Manual Smarty Manual PEAR Manual PHP-GTK Manual Symfony Manual
Code Snippets
Authentication Database Graphics HTTP Miscellaneous Time/Date
Affiliates
Scripts TutorialMan TutorialGuide CodingForums.com PHP Scripts Cheap Web Hosting Affordable Web Hosting Dreamweaver Templates

Search This Site :     PHP Function Reference :
 

Formatting a Field in Smarty

By admin (2006-12-26. 8394 views.)
I have slid by avoiding using field formatting in Smarty templates for some time. However, I just ran across a case where a dollar value HAD to be formatted correctly for display.

It turns out it is pretty easy to use formatting. In the actual template you have code that looks like the template code below. We have a field called bid that is being formatted according to the number_format function.

In the smarty/libs/plugins directory we create the file modifier_number_format.php. This tells smarty (when it loads) to pull in the modifier (formatting) function as defined in the file. That way our template can address it at will.

As you recall, all we are doing is calling the php number_format function with a bunch of arguments. It helps to remember that Smarty really is a bunch of PHP code under the covers.

So, in the specific case we are formatting a number ($number), with 2 decimals (2), a decimal point (".") and a thousands separator (",").

Using this as a model you can move on to some really interesting formatting using all kinds of PHP tools at you disposal.


<?php
// smarty template snippet
...${$BIDS[thebid].bid|number_format:2:".":","}...

// modifier_number_format.php
function smarty_modifier_number_format($number$decimals false$dec_point false$thousands_sep false

   return 
number_format($number$decimals$dec_point$thousands_sep); 
}
?>
 

 
   Print this page

Top Sponsor

Sponsors
CA
Sponsors
AdWords Dominator 125*125
Advertisting

Affiliates
VertexTemplates PHPFreaks CodeWalkers StarGeek DevScripts CGI & PHP Scripts PHP CMS

Shopping Rebates   Sell It 4 You   Flash Page Counters   Get Insured
GPS Tracking Service   Charity Donate Info   Web Site Hosting   VOIP Service

Privacy Policy | Links | Site Map | Advertising

All content on OxyScripts.com is (©)2002-2007

 
Powered by Adrastea - Version 1.0.0. Copyright © Rune Solutions, 2004-2005