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

Followers

About Me

My photo

Believe it or not, I've never been to jail. Not even for a night. I quit going to bars by the time my fake ID was unnecessary, and I quit hacking into businesses and governments once they started paying me.

Now I just provide for my family and try not to notice politics. I get too angry when I watch how much of my income is taken from me, how it is used, and who is doing it. For the same reason, I can't bear to acknowledge celebrities.

After high school, I went to U of K but the outdated computer engineering curriculum and the seemingly endless amounts of money to be made with my internet specific skills drew me to larger tech markets. I have worked in Silicon Valley, New York City, and have settled in Atlanta.