A really simple introduction to templating using PHP
Posted by admin on 01 Nov 2008 | Tagged as: PHP, Web Design & Development
When developers talk about application development they often spout things like
“When developing an application it is beneficial to separate business logic from the presentation logic”
Which all sounds very technical and intellectual, but what are they going on about when they say things like this?
Imagine a simple website. Three pages liked together by hypertext links. Each of the pages contain text and images hard coded as HTML and the files have a .html extension. There’s no processing behind the scenes, no PHP/ASP or Java, the pages content never changes. These are called static pages. It would be fair to say that this website is almost completely made up of presentation logic. Logic, or code concerned with display and not processing any information.
Now, imagine a PHP scrpt that a form on this website posts it’s information to when someone clicks the submit button. Let’s call it ‘process_form.php’, and lets say this form takes the information posted to it, checks the information is usable, and then redirects you to another page depending on what you entered in the form. This processing of information would be an example of business logic.
Often In web applications, business logic and presentation logic, along with CRUD (database logic) are often fused into single scripts. Such as having a single PHP script that does some processing and outputs the result in HTML format. Though often useful, this can become problematic and cumbersome if you wanted to change the way your web page looked without changing the way it does any processing.
Along comes templating…
Templates allow you to seperate your static content from your processing. This way, you could easily change your static content without worrying about the processing code getting in the way and vice versa.
A simple HTML file could look like this:
<head>
<title>Title Text</title>
</head>
<body>
This is the content
</body>
</html>
If we wanted to output PHP in this file we could do something like this:
<head>
<title>Title Text</title>
</head>
<body>
<?php echo ‘This is the content’ ?>
</body>
</html>
This would be an example of single-tier, or fused logic. Which makes it difficult to just change our HTML because the HTML and PHP are intertwined in a single layer. Say we created a template file of our original HTML file that looked like this and gave it the extension .tpl
<head>
<title>{TITLE}</title>
</head>
<body>
{CONTENT}
</body>
</html>
In this example we have replaced the content withcontent placeholders that we can reference later on.
Now, we can produce a simple PHP function that will replace the placeholders with our content after our PHP business logic layer has finished with it. The function may look something like this:
$template = file_get_contents($template);
foreach ($array as $key=>$value){
$template = str_replace(’{’.strtoupper($key).’}',$value,$template);
}
echo $template;
}
This way, from our PHP business logic layer, we can easily produce our presentation layer with any data we like without having to alter the template in any way. An example of calling our template from the business logic layer would be something like this:
Released as a beta on the 2nd September, Google’s entrance into the User agent market doesn’t really come as a surprise. But the oucome may be!