Template class

Template is a Page’s connection to fields (via a Fieldgroup), access control, and output via a template file.

Template objects also maintain several properties which can affect the render behavior of pages using it.


Click any linked item for full usage details and examples. Hookable methods are indicated with the icon. In addition to those shown below, the Template class also inherits all the methods and properties of: WireData and Wire.

Show class?             Show args?        

Identification

NameReturnSummary 
Template::flags int Flags (bitmask) assigned to this template. See the flag constants.  
Template::getIcon()
stringReturn the icon name used by this template 
Template::getLabel()
stringReturn template label for current language, or specified language if provided 
Template::getLanguages()
PageArray Languages nullGet languages allowed for this template or null if language support not active. 
Template::getNumPages()
intReturn the number of pages used by this template. 
Template::getPageClass()
stringGet class name to use for Page objects using this template 
Template::id int Numeric database ID.  
Template::label string Optional short text label to describe Template.  
Template::name string Name of template.  
Template::ns string Namespace found in the template file, or blank if not determined.  
Template::pageClass string Class for instantiated page objects. Page assumed if blank, or specify class name.  
Template::setIcon(string $icon)
$thisSet the icon to use with this template 

Family

NameReturnSummary 
Template::allowNewPages()
boolAllow new pages that use this template? 
Template::childNameFormat string Name format for child pages. when specified, the page-add UI step can be skipped when adding children. Counter appended till unique. Date format assumed if any non-pageName chars present. Use 'title' to pull from title field.  
Template::childTemplates()
TemplatesArrayGet or set child templates (templates allowed for children of pages using this template)
Can also be used as property: Template::childTemplates
 
Template::getParentPage()
Page NullPage nullReturn the parent page that this template assumes new pages are added to 
Template::getParentPages()
PageArrayReturn all possible parent pages for this template 
Template::noChildren int Set to 1 to cancel use of childTemplates.  
Template::noParents int Set to 1 to cancel use of parentTemplates, set to -1 to only allow one page using this template to exist.  
Template::parentTemplates()
TemplatesArrayGet or set parent templates (templates allowed for parent pages of pages using this template)
Can also be used as property: Template::parentTemplates
 
Template::sortfield string Field that children of templates using this page should sort by (leave blank to let page decide, or specify "sort" for manual drag-n-drop).  

URLs

NameReturnSummary 
Template::allowPageNum int Allow page numbers in URLs? (0=no, 1=yes)  
Template::https int Use https? (0 = http or https, 1 = https only, -1 = http only)  
Template::isValidUrlSegmentStr(string $urlSegmentStr)
boolIs the given URL segment string allowed according to this template’s settings? 
Template::slashPageNum string int Should PageNum segments have a trailing slash? (0=either, 1=yes, -1=no) applies only if allowPageNum!=0.  
Template::slashUrlSegments string int Should last URL segment have a trailing slash? (0=either, 1=yes, -1=no) applies only if urlSegments!=0.  
Template::slashUrls int Page URLs should have a trailing slash? 1 = yes, 0 = no  
Template::urlSegments()
array intGet or set allowed URL segments
Can also be used as property: Template::urlSegments
 

Access

NameReturnSummary 
Template::addRoles array Array of Role IDs that may add pages using this template.  
Template::createRoles array Array of Role IDs that may create pages using this template.  
Template::editRoles array Array of Role IDs that may edit pages using this template.  
Template::getRoles()
PageArrayGet the role pages that are part of this template 
Template::guestSearchable int Pages appear in search results even when user doesnt have access? (0=no, 1=yes)  
Template::hasRole($role)
boolDoes this template have the given Role? 
Template::noInherit int Disable role inheritance? Specify 1 to prevent edit/create/add access from inheriting to children, or 0 for default inherit behavior.  
Template::redirectLogin int Redirect when no access: 0 = 404, 1 = login page, url = URL to redirect to, int(>1) = ID of page to redirect to.  
Template::roles PageArray Roles assigned to this template for view access.  
Template::rolesPermissions array Override permissions: Array indexed by role ID with values as permission ID (add) or negative permission ID (revoke).  
Template::setRoles($value)
(nothing)Set roles for this template 
Template::useRoles int bool Whether or not this template defines access.  

Files

NameReturnSummary 
Template::altFilename string Alternate filename for template file, if not based on template name.  
Template::appendFile string File to append to template file (separate from $config->appendTemplateFile).  
Template::contentType string Content-type header or index (extension) of content type header from $config->contentTypes  
Template::filename()
stringReturn corresponding template filename including path, or set template filename
Can also be used as property: Template::filename
 
Template::filenameExists()
boolDoes the template filename exist? 
Template::noAppendTemplateFile int bool Disabe automatic append of $config->appendTemplateFile (if in use).  
Template::noPrependTemplateFile int bool Disable automatic prepend of $config->prependTemplateFile (if in use).  
Template::pagefileSecure int Use secure pagefiles for pages using this template? 0=No/not set, 1=Yes (for non-public pages), 2=Always (3.0.166+)  
Template::prependFile string File to prepend to template file (separate from $config->prependTemplateFile).  

Cache

NameReturnSummary 
Template::cacheExpire int Expire the cache for all pages when page using this template is saved? (1 = yes, 0 = no- only current page)  
Template::cacheExpirePages array Array of Page IDs that should be expired, when cacheExpire == Template::cacheExpireSpecific  
Template::cacheExpireSelector string Selector string matching pages that should be expired, when cacheExpire == Template::cacheExpireSelector  
Template::cacheTime int Number of seconds pages using this template should cache for, or 0 for no cache. Negative values indicates setting used for external caching engine like ProCache.  
Template::noCacheGetVars string GET vars that trigger disabling the cache (only when cache_time > 0)  
Template::noCachePostVars string POST vars that trigger disabling the cache (only when cache_time > 0)  
Template::useCacheForUsers int Use cache for: 0 = only guest users, 1 = guests and logged in users  

Page editor

NameReturnSummary 
Template::errorAction int Action to take when published page missing required field is saved (0=notify only, 1=restore prev value, 2=unpublish page)  
Template::getNameLabel()
stringReturn the overriden "page name" label, or blank if not overridden 
Template::getTabLabel(string $tab)
stringReturn page tab label for current language (or specified language if provided) 
Template::nameContentTab int Pages should display the name field on the content tab? (0=no, 1=yes)  
Template::nameLabel string Optional replacement for the default "Name" label on pages using this template  
Template::tabChildren string Optional replacement for default "Children" label  
Template::tabContent string Optional replacement for default "Content" label  

Behaviors

NameReturnSummary 
Template::allowChangeUser int Allow the createdUser/created_users_id field of pages to be changed? (with API or in admin w/superuser only). 0=no, 1=yes  
Template::noChangeTemplate int Don't allow pages using this template to change their template? (0=template change allowed, 1=template change not allowed)  
Template::noGlobal int Template should ignore the global option of fields? (0=no, 1=yes)  
Template::noLang int Disable multi-language for this template (when language support active).  
Template::noMove int Pages using this template are not moveable? (0=moveable, 1=not movable)  
Template::noSettings int Don't show a settings tab on pages using this template? (0=use settings tab, 1=no settings tab)  
Template::noShortcut int Don't allow pages using this template to appear in shortcut "add new page" menu.  
Template::noTrash int Pages using this template may not go in trash? (i.e. they will be deleted not trashed) (0=trashable, 1=not trashable)  
Template::noUnpublish int Don't allow pages using this template to ever exist in an unpublished state - if page exists, it must be published. (0=page may be unpublished, 1=page may not be unpublished)  

Other

NameReturnSummary 
Template::compile int Set to 1 to enable compilation, 2 to compile file and included files, 3 for auto, or 0 to disable.  
Template::pageLabelField string CSV or space separated string of field names to be displayed by ProcessPageList (overrides those set with ProcessPageList config).  

Common

NameReturnSummary 
Template::addPermissionByRole($permission, $role)
boolAdd a permission that applies to users having a specific role with pages using this template 
Template::addRole($role)
$thisAdd a Role to this template for view, edit, create, or add permission 
Template::editUrl()
stringURL to edit template settings (for administrator) 
Template::modified int Last modified time for template or template file 
Template::removeRole($role)
$thisRemove a Role to this template for view, edit, create, or add permission 
Template::revokePermissionByRole($permission, $role)
boolRevoke a permission that applies to users having a specific role with pages using this template 

Fields

NameReturnSummary 
Template::fieldgroup Fieldgroup Field[] The Fieldgroup used by the template. Can also be used to iterate a Template's fields.  
Template::fieldgroupPrevious Fieldgroup null Previous fieldgroup, if it was changed. Null if not.  
Template::hasField($name)
boolDoes this template have the given Field? 
Template::setFieldgroup(Fieldgroup $fieldgroup)
$thisSet this Template's Fieldgroup 

Tags

NameReturnSummary 
Template::addTag(string $tag)
$thisAdd tag 
Template::getTags()
arrayGet tags array 
Template::hasTag(string $tag)
boolDoes this template have given tag? 
Template::removeTag(string $tag)
selfRemove tag 
Template::tags string Optional tags that can group this template with others in the admin templates list.  

Additional methods and properties

In addition to the methods and properties above, Template also inherits the methods and properties of these classes:

API reference based on ProcessWire core version 3.0.236

Latest news

  • ProcessWire Weekly #543
    In the 543rd issue of ProcessWire Weekly we'll check out the latest weekly update from Ryan, introduce a couple of new third party modules — both authored by Bernhard Baumrock — and more. Read on!
    Weekly.pw / 5 October 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

“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