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.

For instance, $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:

  1. 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).
  2. 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 »


  1. Introduction to template files
  2. Beginner version
  3. Intermediate version
  4. Multi-language version
  5. More template file resources

Comments

  • Andy

    Andy 5 months ago 11

    Hi Ryan

    Many languages have more plural forms than two. You can find it here
    http://docs.translatehouse.org/projects/localization-guide/en/latest/l10n/pluralforms.html?id=l10n/pluralforms

    But in russian languige we have 3 - one (1), two (2,3,4), many (5,6,7,8,9,0)

    For example php code for russian count

    class datext{
    public static function proceedTextual( $numeric, $one, $two, $many )
    {
    $numeric = (int) abs($numeric);
    if ( (numeric % 100 == 1 || ($numeric % 100 > 20) && ( $numeric % 10 == 1 ) ) return $one;
    if ( $numeric % 100 == 2 || ($numeric % 100 > 20) && ( $numeric % 10 == 2 ) ) return $two;
    if ( $numeric % 100 == 3 || ($numeric % 100 > 20) && ( $numeric % 10 == 3 ) ) return $two;
    if ( $numeric % 100 == 4 || ($numeric % 100 > 20) && ( $numeric % 10 == 4 ) ) return $two;

    return $many;
    }
    }

Post a Comment

Your e-mail is kept confidential and not included with your comment. Website is optional.