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.

Latest news

  • ProcessWire Weekly #554
    In the 554th issue of ProcessWire Weekly we'll check out the latest core updates, introduce a couple of new third party modules, and more. Read on!
    Weekly.pw / 21 December 2024
  • Custom Fields Module
    This week we look at a new ProFields module named Custom Fields. This module provides a way to rapidly build out ProcessWire fields that contain any number of subfields/properties within them.
    Blog / 30 August 2024
  • Subscribe to weekly ProcessWire news

“…building with ProcessWire was a breeze, I really love all the flexibility the system provides. I can’t imagine using any other CMS in the future.” —Thomas Aull