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:
Once this code is registered, it can be used in a Smarty template like so:
however this can not be used in a conditional statement in Smarty. If you try to use it like this:
you'll get the following error:
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:
When defined in this manner, the function (a modifier now really) can be used in a Smarty conditional in this manner:
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:
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.
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 |

