An occasional outlet for my thoughts on life, technology, motorcycles, backpacking, kayaking, skydiving...

Thursday, March 17, 2005

Make more of your Smarty Templates

In a previous article I tried to teach you to make better use of Smarty. Today I would like to show you an exercise that may help you expand your possibilities even further. Many people limit there imagination to what can be done by the existing functions and modifiers that come with Smarty. Your possibilities become endless when you master custom functions and custom modifiers. This looks much more complicated that it is. You don't even have to have access to you Smarty plugins directory. I prefer to not clog my plugins directory with functions and modifiers that are specific to a single template. I recommend using $smarty->register_function() and $smarty->register_modifier(). This is easily and neatly achieved using the include technique described in the post referenced above.

On simple example of a custom function is:
// Register the function "smarty_function_binrand"
$smarty->register_function("binrand", "smarty_function_binrand");

// Return [length] number of random 1's and 0's
function smarty_function_binrand($params)
// Call the rand() function [length] number of times, concatenating the results
for($i = 0; $i < $params["length"]; $i++){
$out .= rand(0,1);
return $out;

I use this function plus two modifiers to create a radically different template I call BinaryGeek.

No comments:

Post a Comment