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 #523
    In the 523rd issue of ProcessWire Weekly we'll check out what's new in the core this week, share some new module related news, and more. Read on!
    Weekly.pw / 18 May 2024
  • ProFields Table Field with Actions support
    This week we have some updates for the ProFields table field (FieldtypeTable). These updates are primarily focused on adding new tools for the editor to facilitate input and management of content in a table field.
    Blog / 12 April 2024
  • Subscribe to weekly ProcessWire news

“We chose ProcessWire because of its excellent architecture, modular extensibility and the internal API. The CMS offers the necessary flexibility and performance for such a complex website like superbude.de. ProcessWire offers options that are only available for larger systems, such as Drupal, and allows a much slimmer development process.” —xport communication GmbH