For Programmers: Free Programming Magazines  


Home > Archive > PHP Smarty Templates > January 2005 > need help with executing own function









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author need help with executing own function
Zuhaimi Zainal

2005-01-18, 9:01 am

hi folks,

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.

tia,
zuhaimi

--
When you signup for PayPal, you can start accepting credit card
payments instantly. As the world's number one online payment service,
PayPal is the fastest way to open your doors to over 40 million member
accounts worldwide.
Best of all, it's completely free to sign up!
To sign up or learn more, click here:
https://www.paypal.com/row/mrb/pal=ET694X3JAP9X2
Matthew Weier O'Phinney

2005-01-18, 4:09 pm

* 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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com