The first strategy is the easiest to understand (though also likely the least flexible). But even if you don't intend to use this strategy, read on, as it does help to establish the context that template files operate in. When a template file is used as direct output, the only difference between a template file and an HTML file is that you can use some PHP in there when you want to. Here is an example of a template file using direct output:
<html> <head> <title>Hello World</title> </head> <body> <h1>Hello World</h1> <p>How do you like my HTML document?</p> </body> </html>
There literally isn't any difference between that and a regular old HTML document. Lets go a little further and throw in some PHP so that the output of the
<h1> tag and body copy are dynamic, coming from the
$page being viewed:
<html> <head> <title><?php echo
$page->title; ?></title> </head> <body> <h1><?php echo $page->title; ?></h1> <?php echo $page->body; ?> </body> </html>
Simply by adding
<?php echo $page->title; ?> where we want to output the page's title, and
<?php echo $page->body; ?> where we want to output the page's body, are all that is necessary to have a template file that we could use for dynamic output of any page in our site. Note that you may also shorten that to just
<?=$page->body?> if you prefer (and the same goes for the rest of this tutorial), but we will stick to the longer syntax since there are still a few (rare) web servers out there that may not support these PHP short open/close tags.
Getting back to the real world, it's not likely our site will only need one template (and template file). And chances are our document markup will consist of a lot more than what this simple example shows. That leads us nicely into our next template file strategy…
Next: Direct Output with Includes »
Your e-mail is kept confidential and not included with your comment. Website is optional.