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

  • New TinyMCE 6.2.0 Inputfield released for ProcessWire— More
    28 October 2022
  • This week we take a look at a new rich text editor for ProcessWire, why we chose it, some highlights, screenshots, and why we think you’ll like it— More
    21 October 2022
  • This week we have ProcessWire 3.0.206 on the dev branch and a new version of the ProDevTools UserActivity module, which we'll take a closer look at in this post— More
    7 October 2022

Latest news

  • ProcessWire Weekly #447
    In the 447th issue of ProcessWire Weekly we'll share the latest core updates, introduce a new third party module called RockVideoThumbnailGrabber, and more. Read on!
    Weekly.pw / 3 December 2022
  • New rich text editor for ProcessWire
    This week we take a look at a new rich text editor for ProcessWire, why we chose it, some highlights, screenshots, and why we think you’ll like it.
    Blog / 21 October 2022
  • 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