The $templates API variable provides access to all of your site’s templates.

It provides the API functions available in the admin control panel under Setup > Templates. Use the $templates API variable to retrieve, modify, create or delete templates and control what fields are attached to them.

The $templates API variable is one you aren't likely to use often in typical site development. Instead, this variable is more likely to be used in the development of custom modules or plugins to ProcessWire. In general site development, you will usually interact with templates just from the Admin > Setup > Templates section. However, should you need it, the $templates API variable is always available for you to use just like the other API variables.

$templates properties and methods

See the $templates API reference for a full listing of properties and methods.

Iterating $templates

If you iterate the $templates API variable, it will return each template object in alphabetical order.

<?php
foreach($templates as $template) {
    echo "<li>$template->name</li>";
}

Template objects

You can retrieve an individual template object from the $templates API variable by name (as shown above), or you can retrieve a template assigned to a page like this:

$template = $page->template; 

Regardless of how you retrieve an individual template, all templates share the same API properties and functions. See the $template API reference for full details.

Accessing a template as a string

Accessing a template as a string returns the template's name.

Using Templates and Fields from the API

In the ProcessWire admin, fields are added directly to templates. But it's a little bit different on the API side, where groups of fields (called Fieldgroups) are assigned to templates, and fields are assigned to Fieldgroups rather than directly to templates. This is to allow for future expansion potential by increasing the utility of templates and fieldgroups. The result is that, if you are working with templates and fields in the API, then you will also need to work with Fieldgroups. The interface to Fieldgroups is very simple, and nearly identical to most other API variables. Following are examples that demonstrate how you would interact with templates and fields by way of fieldgroups.

Adding fields to an existing template

When we add fields to an existing template, we're technically adding them to the fieldgroup.

<?php namespace ProcessWire;
$template = $templates->get("some_template");
$template->fields->add("body"); // add some fields
$template->fields->add("summary");
$template->fields->add("images");
$template->fields->save(); 

Note that $template->fields and $template->fieldgroup are the same thing. Use whichever makes more sense with your syntax.

Creating a new template

To create a new template called "something", we first have to create a new Fieldgroup and add fields to it. Then we can assign that fieldgroup to the new template.

<?php namespace ProcessWire;
$fieldgroup = new Fieldgroup();
$fieldgroup->name = "something";
$fieldgroup->add("title"); // add some fields
$fieldgroup->add("body");
$fieldgroup->add("summary");
$fieldgroup->save();

$template = new Template();
$template->name = "something"; // must be same name as the fieldgroup
$template->fieldgroup = $fieldgroup;
$template->save(); 

Note that every template may also have a file associated with it in /site/templates/. When you create a new template, it will not create the file for you. If you want a file in your /site/templates/, then you should place it there manually and give it the same name as the template, but with the ".php" extension, i.e. /site/templates/something.php.

Twitter updates

  • ProcessWire 3.0.144 and 3.0.145 add improved field template context override settings and include a new Inputfields API, along with numerous other issue fixes, optimizations and improvements to the core. More
    8 November 2019
  • ProcessWire 3.0.144 core updates: Upgraded overrides section in the field editor, a new Inputfields JS API, and more— More
    1 November 2019
  • ProcessWire 3.0.143 core updates — 26 commits and 18 resolved issue reports, plus some other useful improvements and additions. More here: More
    25 October 2019

Latest news

  • ProcessWire Weekly #288
    In the 288th issue of ProcessWire Weekly we'll cover the latest core updates, introduce WireMailSendGrid, host a new weekly poll, and showcase a new site of the week. Read on!
    Weekly.pw / 16 November 2019
  • ProcessWire 3.0.145 core updates
    ProcessWire 3.0.144 and 3.0.145 add improved field template context override settings and include a new Inputfields API, along with numerous other issue fixes, optimizations and improvements to the core.
    Blog / 8 November 2019
  • Subscribe to weekly ProcessWire news

“I am currently managing a ProcessWire site with 2 million+ pages. It’s admirably fast, and much, much faster than any other CMS we tested.” —Nickie, Web developer