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.

Monday, March 14, 2005

Many of you have never seen my wife.

This is why I am always in a hurry to get home. This is my wife Christy. She is my best friend, and the reason I strive to be excellent at everything I do. We were married on July 13th of 2002. I married over my head, but am a much better man for it.

I think ski clothes are sexier than swim clothes!She was caught kissing that guy shortly after this photo was taken.

Attention fellow geeks! How to get your wife to talk about Linux daily.

Name your dog Linux.

No she's not a mutant, that's a Tongue Ball
No, she's not a mutant, that's her Tongue Ball that she got for Christmas.

Linux is a 2 year old Miniature American Eskimo dog. She is full grown but looks like a 15lb sled-dog puppy. She's a great lap dog, but sheds like it's nobody's business.

Do I Yahoo!? Of course!

"So do you Yahoo!?", he asked.

"Do I Yahoo!? Of course I do. Would I be applying for this job if I didn't?", I replied.

"How do you Yahoo!?", he challenged.

"Well today I used maps dot Yahoo! dot com to get directions to find this place. I like that it associates my search history with my Yahoo! ID so that I can access it from a computer that doesn't have my cookies in the cache and still get quick access to My Yahoo! Searches. And, the navigation algorithms are much more efficient than those of competing web sites. I have My Yahoo! customized with weather from Powder Springs where I live and Atlanta where I spend a lot of my time. I have show times from the movie theaters which I frequent, as well as the news categories that interest me. When I used to carry a PDA I would keep it synced with My Yahoo!. I use Yahoo messenger to chat with colleagues to maximize our programming efficiency. Yes, Yahoo! is a lifestyle and I Yahoo!", I confirmed.

In the next hour I did everything in my power to sell myself. Ten years of paid professional programming skills and business communication skills were my product. But were they buying? I did not have the luxury of time, and I made it clear that in the next 29 hours I had 4 more interviews. Each were followup interviews and I was told that 3 were going to present an offer. Yahoo! was not coy about it's interest in me, and I had received a phone call by the time I could make it to the interstate. They wanted me to put all offers on hold until my background check could clear, and they could make their offer which they were sure I would be pleased to accept.

Today I received the offer. I am pleased to say that I am the newest Technical Yahoo!, Senior in the organization. I am sure that I will bring value to the organization, and I will be challenged and educated greatly. If I'm going to work for a Dot Com company it's going to be Yahoo! If I'm going to work for a computer hardware or software company it's going to be Apple. I strive to be the best. But to be the best, you must serve the best. In context of God, Country, and Career this holds true. Today I have taken a very important step.

Sunday, March 13, 2005

My first day at Apple.

What drives a programmer who makes $45 per hour to leave the safety of his home office for a $10 per hour retail job in a mall? Passion! Those of you who haven't experienced OS X and the new Apple Computing Experience will never understand this. Unless of course you visit me at the Apple Store in Lenox Mall, Atlanta Georgia. Cause if you do, you will be walking out of that store with a Mac Mini and a new found respect for an old computer company.

I haven't gone completely crazy. I still consult and that is where I draw my primary income, but what I do for a living and what I do to live are very different. I will be working at the Apple Store evenings and weekends. So if you are in the area, come see me.
This is not the Active Uniform shirt, just a gift in my welcome packet.

Tuesday, March 8, 2005

You should be using Smarty Templates!

There are plenty of articles out there telling you why you should be using Smarty Templates. So go read one of those if you what to know why you need it. I'm going to use my time to tell you how to make the most of Smarty. This will need to be a series of articles if I am to help you make the most, so in this article I will try to help you make more of your Smarty.

First of all Smarty helps a developer eliminate redundant code related to HTML presentation. This is the simplest way I can think of to sum up what Smarty is about. But the code to instantiate Smarty, and to render the HTML with the Smarty object becomes redundant on every page. Most developers accept this since they still get the benefit of having one template to modify to update the HTML presentation of the dozens of pages that may use the template. This is a great benefit, but we want to take it a step farther.

Your basic Smarty instantiation will look something like this:

require "Smarty.class.php";
$smarty = new Smarty;

$smarty->template_dir = $_SERVER["TEMPLATE_PATH"];
$smarty->compile_dir = $_SERVER["COMPILE_PATH"];
$smarty->compile_check = 1;
$smarty->debugging = 0;

$smarty->assign("images", "path/to/images");
$smarty->assign("pages", "path/to/pages");
$smarty->assign("css", "path/to/css");
$smarty->assign("js", "path/to/js");
$smarty->assign("content", "path/to/content");

You then have code for doing all of your PHP processing and assigning data into the Smarty object. To close you must call the display method. This may look like: (Don't let this code overwhelm you. I'm just trying to give a real-world example.)
$smarty->assign("subtitle", "Thermal / Ablative");
"label"=>"Corporate Overview",
"label"=>"Carbon Based Mtls",
"text"=> array(
"MTLS is now part of Science and Technology Objective (STO) Team at US Army's ERDC in Vicksburg",
"MTLS signs agreement with Masureel (Belgium) to serve as sole distributor for basalt fiber in the Americas",

This next line is the important part. It renders the HTML to the browser.

First of all, it should be instinctive for you to put that first section (lines 1-13) in an include file. If it wasn't, then you need to always be asking yourself "How can I clean and organize my code?" The tricky part is that last line (line 40). You can actually add it to the include file that you will require() at the beginning of each file. This is done with a "little known, even littler used" function called register_shutdown_function(). The purpose of this function is to tell your script to execute a given function as the last thing it does before terminating. The trick to it is that you can only specify a function, and not any parameters for the function. It is also not possible to call methods from an object reference. So you must use an even less known function called create_function(). This function creates an anonymous (lambda-style) function. You can combine them like this:

register_shutdown_function(create_function('','global $smarty; $smarty->display("template4.tpl");'));

Putting this line after the first 13 lines quoted above will make an include file that you can reference with one line at the beginning of all pages that use "template4.tpl" That gives you minimum redundancy, which is the reason for using SmartyTemplates in the first place. Want cleaner code? You should be using Smarty Templates!