Working 'Smarter' with Templates - A Typical Dynamic Web Page without Templates
(Page 3 of 6 )
To understand the value of Smarty, we first look at how a simple task would be carried out in traditional PHP scripts without using a template system.
<html>
<head>
<title>Show IP</title>
</head>
<body>
<?
// ShowIP.php
echo $_SERVER['REMOTE_ADDR'];
?>
</body>
</html>
As you see, we simply create a basic web page and add the necessary php to display the IP address of the visitor. This is a fine method for such a simple task, but as you'll see, this method can become very cumbersome when you begin to introduce HTML into the php output. Take a look at the following example and notice how much more confusing the code becomes when we introduce some html into the mix.
<html>
<head>
<title>Show IP</title>
</head>
<body>
<?
// ShowIP.php
echo "<a target=\"_Blank\" class=\"linktext\" href=\"http://" . $_SERVER['REMOTE_ADDR'] . "\">Click Here to view server</a>";
</body>
</html>
Escaping those quotes and concatenating for the variable makes for some messy code. This is just the beginning – imagine the carnage if we add tables, more classes, loops, more variables, etc.
Next: Putting Smarty to Work - A Dynamic Template-driven Web Page >>
More Search Optimization Articles
More By Bill Sterzenbach