Thoughts on Templates
Posted: Sat Jun 07, 2008 6:56 pm
I have some thoughts on "templates." My personal philosophy is that templates, where possible, should never contain PHP code. Let me explain.
PHP, in its roots, has the ability to enhance an HTML page by embedding code. So quite frequently, people will create a page that looks something like this (keeping it simple):
The other way to generate this page, is to use a template, and insert content, using a markup. I happen to like "@@" symbols as delimiters, since they are easy to see, but others can be used as well. So, here is an non-PHP (HTML-only) template.
So, to use this template, which is pure HTML, and can be validated, you need to do something like this:
Templates of elements can also be used.
So, elements can be put in a theme file, and included in your script.
There are a number of advantages to using templates like this.
1) Your template can be validated for HTML.
2) Your template can be edited with any HTML editor. You can edit your template independent of your code.
3) It is easy to view the complete layout. It is quite difficult when the layout is spread out in several files. Typically, your template would be more extensive.
4) It is much easier to create nice-looking HTML output. It can be difficult to figure out spacing and line breaks through a bunch of print statements in several files.
I have found when looking through the templates of many of the open-source projects, it can be quite horrendous to sort through the templates that are used.
PHP, in its roots, has the ability to enhance an HTML page by embedding code. So quite frequently, people will create a page that looks something like this (keeping it simple):
Code: Select all
<doctype ...
<html>
<head>
<title><?php echo $mytitle; ?></title>
</head>
<body>
<div id="header">
<?php echo $myheader; ?>
</div>
<div id="menu">
<?php echo $mymenu; ?>
</div>
<div id="content">
<?php echo $content; ?>
</div>
<div id="footer">
<?php echo $myfooter; ?>
</div>
</body>
</html>Code: Select all
<doctype ...
<html>
<head>
<title>@@TITLE@@</title>
</head>
<body>
<div id="header">
@@HEADER@@
</div>
<div id="menu">
@@MENU@@
</div>
<div id="content">
@@CONTENT@@
</div>
<div id="footer">
@@FOOTER@@
</div>
</body>
</html>Code: Select all
<?php
$page = file_get_contents($templatefile);
$OldText = array('@@TITLE@@', '@@HEADER@@', '@@MENU@@', '@@CONTENT@@', '@@FOOTER@@');
$NewText = array($mytitle, $myheader, $mymenu, $content, $myfooter);
echo str_replace($OldText,$NewText,$page);
?>Code: Select all
$MyTableRow = '<tr><th>@@THEAD@@</th><td>@@TVALUE1@@</td><td>@@TVALUE2@@</td></tr>' . "\n";There are a number of advantages to using templates like this.
1) Your template can be validated for HTML.
2) Your template can be edited with any HTML editor. You can edit your template independent of your code.
3) It is easy to view the complete layout. It is quite difficult when the layout is spread out in several files. Typically, your template would be more extensive.
4) It is much easier to create nice-looking HTML output. It can be difficult to figure out spacing and line breaks through a bunch of print statements in several files.
I have found when looking through the templates of many of the open-source projects, it can be quite horrendous to sort through the templates that are used.