Page Templates API

Last modified by Pierre Jeanjean on 2026/11/24 14:20

Reference

XWiki 18.10.0+

The org.xwiki.platform:xwiki-platform-pagetemplate-api module reads template providers, from Java and from scripts. Its API is unstable.

Java

The org.xwiki.pagetemplate.TemplateProviderManager component role:

MethodReturns
Optional<TemplateProvider> getTemplateProvider(DocumentReference reference)The template provider defined in that Page, or an empty Optional when the Page holds no XWiki.TemplateProviderClass Object. Throws TemplateProviderException when the Page cannot be loaded.
List<TemplateProvider> getTemplateProviders()The template providers of the current wiki, ordered by the full name of their Page, without checking the rights of the current user. XWiki.TemplateProviderTemplate, which new template providers are created from, is left out, and so is a template provider that cannot be loaded. Throws TemplateProviderException when they cannot be listed.
@Inject
private TemplateProviderManager templateProviderManager;

public Optional<DocumentReference> getTemplate(DocumentReference providerReference) throws TemplateProviderException
{
    return this.templateProviderManager.getTemplateProvider(providerReference)
        .flatMap(TemplateProvider::getTemplateReference);
}

Template Provider

A org.xwiki.pagetemplate.TemplateProvider holds the values of the template provider fields:

GetterReturnsObject property
DocumentReference getReference()The reference of the Page holding the template providerN/A
String getName()The name, as plain text or a translation key, or nullname
String getDescription()The description, as plain text or a translation key, or nulldescription
String getIcon()The name of the icon, or nullicon
Optional<DocumentReference> getTemplateReference()The reference of the template, or an empty Optionaltemplate
String getAction()edit, saveandedit or saveandview, or nullaction
boolean isTerminal()Whether the created Pages are terminalterminal
List<SpaceReference> getVisibilityRestrictions()The spaces the template provider is offered in, empty when offered everywherevisibilityRestrictions
List<SpaceReference> getCreationRestrictions()The spaces Pages can be created in, empty when they can be created anywherecreationRestrictions
boolean areCreationRestrictionsSuggestions()Whether the creation restrictions are only suggestionscreationRestrictionsAreSuggestions

Script Service

$services.templateProvider.getTemplateProviders() returns the same list as its Java counterpart, keeping only the template providers the current user can view. When they cannot be listed, it logs a warning and returns an empty list.

{{velocity}}
#foreach ($provider in $services.templateProvider.getTemplateProviders())
  #if ("$!provider.name" != '')
    #set ($name = $services.localization.render($provider.name))
  #else
    #set ($name = $xwiki.getDocument($provider.reference).plainTitle)
  #end
  * $services.rendering.escape($name, 'xwiki/2.1')
#end
{{/velocity}}

Related

Get Connected