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

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");
$smarty->assign("buttons",
array(
array(
"label"=>"Corporate Overview",
"text"=>file_get_contents($smarty->get_template_vars("content")."/page1.htm"),
),
array(
"label"=>"Carbon Based Mtls",
"link"=>$smarty->get_template_vars("pages")."/page2.php",
"text"=>file_get_contents($smarty->get_template_vars("content")."/page2.htm"),
),
)
);
$smarty->assign("body",
array(
"label"=>"News",
"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",
),
"image"=>$smarty->get_template_vars("images")."/earth.jpg",
"top"=>"155",
"left"=>"283",
)
);


This next line is the important part. It renders the HTML to the browser.
$smarty->display("template4.tpl");


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!

5 comments:

  1. By the way, I would prefer not to use the the "global" declaration, but I feel that this is one of the few cases where it is necessary. If you find a way to avoid it, let me know in a comment.

    ReplyDelete
  2. in the "Quick Information" section. I also removed you email from your post. I don't want you to get data scraped. Thanks for being interested enough to risk being a spam victim!

    ReplyDelete
  3. i want build or do a project of making a masterpage(homepage) with the help of smarty template. i am fresher to smarty template. give me an idea or source code so that helps me my project.

    ReplyDelete
  4. nilesh tighare,
    There are more articles here: http://bronosky.com/?cat=9 and they all include or link to source code.

    One link that is often overlooked is http://bronosky.com/pub/binaryGeek/ which is one of the most advanced examples of Smarty usage I've ever seen. It was more of an academic venture than anything else, but it demonstrates most of the advanced topics of Smarty. Best of all, is that all of the source code is linked in the navigation.

    I hope that helps.

    ReplyDelete

Followers