Intermediate version

How this differs from the beginner version

The intermediate version of the default site profile is identical in function to the beginner version, except that it uses a different output strategy. Rather than splitting the markup into _head.php and _foot.php files, all the markup is instead contained in a _main.php file, which represents an entire HTML document.

In the beginner version, the template files focused on outputting what went in between _head.php and _foot.php. In the intermediate version, the template files instead focus on populating placeholders that will be output by the _main.php file.

How the intermediate version of the default site profile works

The intermediate default site profile uses the Delayed Output strategy mentioned on the first page. Here's how it works:

  1. The initialization file is loaded: _init.php
    We use it to define placeholder variables for content regions.
  2. The template file is loaded: i.e. home.php or another
    We use it to populate values into the placeholder variables.
  3. The main output file is loaded: _main.php
    It is an HTML document that outputs the placeholder variables.

Below are more details on exactly what takes place and in the three steps outlined above:

1. The initialization file is loaded (_init.php)

We define placeholder variables for the regions in our page in the _init.php file. These placeholders can be anything that you like (and in any quantity) and usually depends entirely on the needs of your site design and content.

In this default site, our needs are simple so we've defined placeholders for just 3 regions on the page. We usually name these regions something consistent with the HTML tag, id or class attribute just for ease of readability, but that's not required. These are the three placeholder variables we've defined in this site:

  • $title - The headline or title (we use for <title> and <h1>)
  • $content - The main page content (we use for <div id='content'>)
  • $sidebar - Sidebar content (we use for <div id='sidebar'>)

The leading "$" is what designates them as placeholder variables. We do this in a file called _init.php. ProcessWire knows to load this _init.php file first, before our actual template file. We define these placeholder variables simply giving each a default value, or by just making them blank. Go ahead and take a look at the _init.php file now if you can. But to summarize, here's how you define a blank placeholder variable:

$content = '';

And here's how you define a placeholder variable with an initial/default value:

$content = "<p>Hello World</p>"; // assign static value…
$content = $page->body; // …or assign dynamic value from $page

The last thing we want to mention about _init.php is that we might also use it to load any shared functions. You'll see a line in this site's _init.php the includes a file called _func.php. That file simply contains a shared function (used by multiple template files) for generating navigation markup. This part is not so important for now, so come back to it once you understand how everything else works. But the point to understand now is that the _init.php file initializes everything that may be used by the site's template files.

2. The template file is loaded (i.e. home.php or another)

Next, ProcessWire loads the template file used by the page being viewed. For example, the homepage uses home.php. We use our template file to populate those placeholder variables we defined in _init.php with the values we want.

For instance, most often we populate our $content variable with the body copy from the current page:

$content = $page->body;

But we might also do something more like append some navigation under the body copy or prepend a photo... the sky is the limit.

$content = "<img src='/photo.jpg'>" . $page->body;

Our search.php template file for example, populates $content with a list of search results.

Because our placeholder variables were already defined in the _init.php file with default values, our template file (like home.php or basic-page.php) need only focus on populating the placeholder variables that you want to modify. It does not even need to mention those placeholder variables that it doesn't need or doesn't need to change.

3. Everything gets output by _main.php

After ProcessWire has loaded our template file (i.e. home.php) it then knows to load the _main.php file last. In the case of this site, our _main.php file is an entire HTML document that outputs our placeholder variables in the regions where they should appear. For example, the $content variable gets output in #content <div> like this:

<div id='content'>
  <?= $content ?>

Please go ahead and take a look at the _main.php file for context.

Note that our _main.php uses <?php echo $content; ?> style PHP syntax, rather than <?= $content ?> syntax (as shown above), just in case you happen to be running an older version of PHP. But more than likely you can use the shorter syntax when preferred, as the two are functionally equivalent. We find the shorter syntax preferable unless there's a chance the site will have to run on an older PHP version.

Benefits of this delayed output strategy

An obvious benefit to this approach is that our template files can more easily populate output for any regions on a page, rather than just what goes in between _head.php and _foot.php (as in the beginner edition). Furthermore, there are no limits to the number of regions you can choose to setup, and your template files can focus only on the regions that you want them to affect.

Delayed output also tends to lend itself better to scale and change. That's because the individual template files don't need to know where everything goes… that's the job of just one file: _main.php. In fact, those template files don't need to mention anything other than the regions they want to modify. Like mentioned in the previous point, adding a new region to your layout doesn't mean you have to go change all your template files… just the ones that need to affect that new region differently.

Another benefit of delaying output in this manner means that we can make changes to our placeholders along the way. Nothing is set in stone till our main output file (_main.php) takes over.

For example, lets say that one of the things we need to populate is the <title> tag in the document header. In the beginner edition, this lives in the _head.php file and it outputs the page title before we've output our main content. That's fine, but lets say that something about our main content affects the page title. Maybe we're using pagination (like this page you are reading now) or maybe it generates some kind of search results, and we want that to be reflected in the <title> tag… We might like it to say "Products 1-10 of 70" somewhere in the <title> tag. If we were using direct output–as in the beginner profile–it would be too late, because our <title> had already been output by the time we got around to retrieving search results and outputting them in the body copy region. To put it simply, delaying output gives you more control over what the final output will be since nothing is final till the end.

Next: Multi-language version »

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


  • Andy

    Andy 1 year ago 21

    Hi Ryan

    Many languages have more plural forms than two. You can find it here

    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.