Re: need help with executing own function

From: Date: Tue, 18 Jan 2005 16:38:05 +0000
Subject: Re: need help with executing own function
References: 1  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-12172@lists.php.net to get a copy of this message
* Zuhaimi Zainal <zuhaimi@gmail.com>: > i've just started using smarty. i think its great but i do find some > difficulty adjusting to it. right now i'm stuck. i'm sure its quite a > simple one so please bear with me. > > what do i need to do in smarty so that i can execute my own function > on a template var? > > take a look at this situation.. let say in my template: > --begin code-- > <td>Car Type: </td><td>{$car->car_type}</td> > <td>Displacement: </td><td>{$car->car_cc}</td> > --end code-- > > $car is being assign value from a db query result (car_type, car_cc > are table columns) > > now, car_type are integers.. it does not contain the actual car type. > before i use smarty, this can easily done by creating a function to > translate the car_type to its respective type. ie: car_type 1 is for > sedan, car_type 2 is for hatchback etc etc.. like this > --begin code-- > <td>Car Type: </td><td><?php echo getCarType($car->car_type) > ?></td> > --end code-- > with the function getCarType returning the actual car type. > > but how do i do it (the getCarType function) now in the template with > smarty? fyi, all my functions are in one separate file. Don't. Pass an associative array of car types: $types = array( 1 => 'Sedan', 2 => 'Hatchback' ); and pass that to the template. Then, when you need the string version: <td>Car Type:</td><td>{$types[$car->cartype]}</td> That way you don't need to muck about with creating Smarty functions or modifiers. -- Matthew Weier O'Phinney | mailto:matthew@garden.org Webmaster and IT Specialist | http://www.garden.org National Gardening Association | http://www.kidsgardening.com 802-863-5251 x156 | http://nationalgardenmonth.org

« previous php.smarty.general (#12172) next »