Just getting started with ProcessWire and aren't totally clear on what template files are? The good news is that template files aren't anything other than regular HTML or PHP files, and you can use them however you want!
If you know enough to create an HTML or PHP document, then you already know how to use ProcessWire template files. The only difference is that ProcessWire provides your template files with certain variables that you may choose to use, or not use. Most notable is the
$page variable, which contains all the fields of text or other information contained by the page being viewed.
$page->title contains the text contained in the Title field of the current page, and
$page->body contains the text for the Body field of the current page. You can choose to output those wherever you want. A really simple template file might look like a regular HTML document except for where you want to output the dynamic portions (like title and body). Here's an example:
<html> <head> <title><?= $page->title ?></title> </head> <body> <h1><?= $page->title ?></h1> <?= $page->body ?> </body> </html>
That's all that a template file is. But when we're building something for real, we like to save ourselves as much work as possible and avoid writing the same HTML markup in multiple places. In order to do that we'll usually isolate the repetitive markup into separate files or functions so that we don't have to write it more than once. That's not required of course, but it's a good idea to save you time and make it easier to maintain your site further down the road.
Template file strategies
The default site profile uses a strategy called Delayed Output, but you should use whatever strategy you prefer (or make up your own). The two most popular strategies for template files are:
- Direct Output is the simplest strategy and the one used by the Beginner Default Site Profile (described on the next page). While it doesn't scale as well as other strategies, it is a very good point to start from (and it may be all you ever need). If you've ever worked with WordPress templates, chances are you already know how Direct Output works. If you'd like to get started with this strategy, be sure to install the Beginner Default Site Profile (or the Classic Site Profile also uses this strategy).
- Delayed Output is the strategy used by the Intermediate Default Site Profile. It is also quite simple but involves populating content to placeholder variables rather than outputting directly. As a result it may take a few more seconds to understand than direct output, but the result can be more scalable and maintainable. By the time you finish reading this tutorial, we think you'll have a good sense of how delayed output works.
Next: Beginner version »
- Introduction to template files
- Beginner version
- Intermediate version
- Multi-language version
- More template file resources
Your e-mail is kept confidential and not included with your comment. Website is optional.