Tip Details Add a Related Tip

Rate as: Positive Negative

Using functions in conditionals in Smarty

Smarty is the templating engine of choice for PHP. One of the nice features of Smarty is that you can create your own PHP functions that can then be called from the Smarty template. I recently ran into an interesting situation though where I wanted to use a function in a conditional statement in the template. Turns out this doesn't quite work. It took a while to figure out how to do this, so I figured I'd share.

In Smarty, template functions are usually created and registered in the following manner:

function canDoSomething($params) {
    // Some code goes here
    return $result;
}
$smarty->register_function('canDoSomething', 'canDoSomething');

Once this code is registered, it can be used in a Smarty template like so:

{canDoSomething param1=$param1}

however this can not be used in a conditional statement in Smarty. If you try to use it like this:

{ if canDoSomething param1=$param1 } 
    // Do something
{ /if }

you'll get the following error:

syntax error: unidentified token '='

So the answer lies in the way the function is constructed and registered. Instead of registering the function with the register_function call, you register the function as a modifier with the register_modifier call.

In addition, when you register a function in Smarty, the function itself has a single parameter which is an associative array with key/value pairs corresponding to the parameters defined when the function is called. When you register a modifier, it needs to call out all of the expected parameters, they don't come in as an associative array. Here's an example of how the previous function would now be declared and registered:

function canDoSomething($param1) {
    // Some code goes here
    return $result;
}
$smarty->register_modifier('canDoSomething', 'canDoSomething');

When defined in this manner, the function (a modifier now really) can be used in a Smarty conditional in this manner:

{ if $param1|canDoSomething } 
    // Do something
{ /if }

Note the difference in the way the parameter is passed to the function. If your function is expecting more than one parameter, here's how additional parameters are passed in the call from Smarty:

{ if $param1|canDoSomething:$param2:$param3: ...and so on... } 
    // Do something
{ /if }

By registering your PHP functions using register_modifier instead of through register_function, you can use your PHP function as a modifier in your Smarty templates.
Rating: 100% positive, 3 total Votes
Categories: php programming Smarty web
Added: on Aug 10, 2007 at 1:02 pm
Added By: rlansky

Comments on this Tip

Add a Comment
brilliant!
– Added by an anonymous user on Aug 11, 2007 at 3:16 pm
This is really good... the one quirk I found is the case where the function you are calling does not expect any parameters. In that case, you can not just call it like so:

{ if canDoSomething } 
    // Do something
{ /if }

You must always send a parameter for this to work. I got around this by just sending an empty value to it, I did not have to update the function to accept this value. So, to get this to work, I used:

{ if ''|canDoSomething } 
    // Do something
{ /if }
– Added by an anonymous user on Sep 13, 2007 at 10:48 am
er.. this doesnt work :s

I have a function :
function askapache_captcha($type=1,$numletters=4,$fontsize=22){
...
}
it is called by this :
askapache_captcha();

i used :

$smarty->register_function('askapache_captcha', 'askapache_captcha');

and in template :

{askapache_captcha}

it gave me this error :

Fatal error: Smarty error: [in submit_form.tpl line 98]: syntax error: unrecognized tag 'askapache_captcha' (Smarty_Compiler.class.php, line 590) in D:\xampp\htdocs\linkexchangesystem\smarty\Smarty.class.php on line 1095


anyhelp ?? :s
– Added by an anonymous user on May 21, 2008 at 1:36 pm
Your Comment:
(how to format)
Rate This Tip:

Verify Humanity:
Sorry, we know it's annoying, but please enter the characters shown in the image to the left so that we know you're an actual person and not an evil spammer. Thanks.
       

Related Tips

Copyright date on sites

Categories: web programming php Smarty
100% positive, 0 comments
– Tip added by an anonymous user on Mar 11, 2008 at 8:14 am

Something to consider before switching platforms

100% positive, 0 comments
– Tip added by an anonymous user on Sep 25, 2007 at 7:31 pm

Static variables in Javascript

100% positive, 2 comments - last added on May 08, 2008 at 4:19 pm
– Tip added by an anonymous user on Aug 03, 2007 at 4:19 pm

Scrolling HTML elements using the keyboard

100% positive, 0 comments
– Tip added by rlansky on Jun 22, 2007 at 9:34 am

How to Find a Random Date in PHP

40% positive, 0 comments
– Tip added by Walkere on Feb 07, 2008 at 8:25 pm

compressing javascript

100% positive, 0 comments
– Tip added by an anonymous user on Jun 12, 2008 at 3:02 pm

Telling when an iframe is done loading

100% positive, 0 comments
– Tip added by an anonymous user on Apr 18, 2008 at 8:05 am

How to Syndicate an RSS Feed On Your Site

100% positive, 0 comments
– Tip added by Walkere on Feb 09, 2008 at 5:48 pm

How to Send E-Mail in PHP: A “Share This” Form

50% positive, 0 comments
– Tip added by Walkere on Feb 06, 2008 at 6:45 pm

How to Create a Random Password in PHP

100% positive, 0 comments
– Tip added by Walkere on Feb 03, 2008 at 9:14 am

Avoiding facebook timeout errors

Categories: Facebook programming web
100% positive, 0 comments
– Tip added by an anonymous user on Aug 21, 2007 at 5:21 pm

Answering a tricky interview question on swapping

100% positive, 1 comment - last added on Aug 20, 2007 at 8:55 am
– Tip added by an anonymous user on Aug 20, 2007 at 7:26 am

Learn Python in 10 minutes

Categories: programming Python web
100% positive, 0 comments
– Tip added by Marcos84 on Jul 20, 2007 at 7:42 am

Tutorial for creating Facebook application

Categories: Facebook programming web
50% positive, 0 comments
– Tip added by an anonymous user on Jul 04, 2007 at 8:31 am

Referencing methods in PHP usort

Categories: php coding programming
no ratings, 0 comments
– Tip added by an anonymous user on Jun 13, 2008 at 11:18 am

CakePHP: Working with Associations

no ratings, 0 comments
– Tip added by an anonymous user on May 08, 2008 at 4:09 am

Javascript variable naming

no ratings, 0 comments
– Tip added by an anonymous user on Sep 30, 2007 at 7:28 am

Code for creating Javascript CSS charts

no ratings, 0 comments
– Tip added by an anonymous user on Aug 21, 2007 at 12:23 pm

Efficient building of large strings in Javascript

no ratings, 0 comments
– Tip added by an anonymous user on Aug 08, 2007 at 12:52 pm

Avoid invalid title names when opening javascript windows

no ratings, 0 comments
– Tip added by an anonymous user on Aug 07, 2007 at 9:36 am