PHP and Template Functions

There is a debate going on in the world of PHP development of whether to use additional templating engines such as Smarty. While I've not really participated much in the debates myself I do understand both sides of the argument.

The primary arguments for using a templating engine is faster time to market, and removal of business logic from within the HTML or presentation layer. The primary arguments against using such an engine are PHP is already a lightweight scripting language, why bother with the overhead of a templating language; invetibly, even with templating engines, some business logic is stored in the template.

I agree with all the arguments above. However, I tend to go the way of developing my own templating functions rather than relying on an engine to do so for me. Using <?=$somVar?> inside of html is generally powerful enough for me to get accomplished what I need to do. Recently, while working on a website with a business partner of mine I found myself wanting similar templating functions that are available in Smarty. Spefically I wanted to dynamically create a select box. Not just a single select box, but many select boxes, with various information.

I took it upon myself to build a function that would do this for me. For the purposes of this article/howto I have used the states select box as an example. For any ecommerce site you're going to need to list the 50 states, preferably in a select box. I created two functions to accomplish this.

The first function simply creates and returns an array of states. Notice that the two character state abbreviation is the array key.

Function to create States Array

  function getStatesArray() {
  	$statesArray = array(
		"AL" => "Alabama",
		"AK" => "Alaska",
		"AZ" => "Arizona",
		"AR" => "Arkansas",
		"CA" => "California",
		"CO" => "Colorado",
		"CT" => "Connecticut",
		"DE" => "Delaware",
		"DC" => "District of Columbia",
		"FL" => "Florida",
		"GA" => "Georgia",
		"HI" => "Hawaii",
		"ID" => "Idaho",
		"IL" => "Illinois",
		"IN" => "Indiana",
		"IA" => "Iowa",
		"KS" => "Kansas",
		"KY" => "Kentucky",
		"LA" => "Louisiana",
		"ME" => "Maine",
		"MD" => "Maryland",
		"MA" => "Massachusetts",
		"MI" => "Michigan",
		"MN" => "Minnesota",
		"MS" => "Mississippi",
		"MO" => "Missouri",
		"MT" => "Montana",
		"NE" => "Nebraska",
		"NV" => "Nevada",
		"NH" => "New Hampshire",
		"NJ" => "New Jersey",
		"NM" => "New Mexico",
		"NY" => "New York",
		"NC" => "North Carolina",
		"ND" => "North Dakota",
		"OH" => "Ohio",
		"OK" => "Oklahoma",
		"OR" => "Oregon",
		"PA" => "Pennsylvania",
		"RI" => "Rhode Island",
		"SC" => "South Carolina",
		"SD" => "South Dakota",
		"TN" => "Tennessee",
		"TX" => "Texas",
		"UT" => "Utah",
		"VT" => "Vermont",
		"VA" => "Virginia",
		"WA" => "Washington",
		"DC" => "Washington D.C.",
		"WV" => "West Virginia",
		"WI" => "Wisconsin",
		"WY" => "Wyoming",
		"AB" => "Alberta",
		"BC" => "British Columbia",
		"MB" => "Manitoba",
		"NB" => "New Brunswick",
		"NF" => "New Foundland",
		"NT" => "Northwest Territories",
		"NS" => "Nova Scotia",
		"ON" => "Ontario",
		"PI" => "Prince Edward Island",
		"PQ" => "Quebec",
		"SA" => "Saskatchewan",
		"YT" => "Yukon Territory");
	return $statesArray;
  }

The next function takes a mutli-demensional array such as the one returned by getStatsArray() and generates the actual \<\/option\> statements.

Function to taking a multi-demensional array as well as the value to be selected

  function buildSelectOpts($arrayOpts, $selected) {
  	$options = "";
  	$arrayKey = array_keys($arrayOpts);
  	for ($i = 0; $i < count($arrayOpts); $i++) {
  		if ($arrayKey[$i] == $selected && !empty($selected)) {
  			$options.="\n";
  		} else {
  			$options.="\n";
  		}
  	}
  	return $options;
  }

So in a PHP file that will prepare the data for display you'll have something like:

  $statesOptions = buildSelectOpts(getStatesArray(), 'MD');
  include('template.tpl');

The contents of the relavent portions of template.tpl are:

  <select name="state"><?=$statesOptions?></select>

The above function uses the actual array keys to grab the selected value as well as assigning a value to the options.
We have plans to use this for much more than states. We will be using this same function to populate a select box for a list of users which will be gathered using data from a MySQL Table.

Enjoy!

Comments