Templates AEM6.3

Page Templates – Editable

Editable templates have been introduced to:

  • Allow specialized authors to create and edit templates.
    • Such specialized authors are called template authors
    • Template authors must be members of the template-authors group.
  • Provide templates that retain a dynamic connection to any pages created from them. This ensures that any changes to the template are reflected in the pages themselves.
  • Make the page component more generic so the core page component can be used without customization.

With editable templates, the pieces that make a page are isolated within components. You can configure the necessary combinations of components in a UI, thereby eliminating the need for a new page component to be developed for each page variation.

Note:

Static templates are also available.

This document:

  • Gives an overview of creating editable templates
  • Describes the admin/developer tasks required to create editable templates
  • Describes the technical underpinnings of editable templates

This document assumes that you are already familiar with creating and editing templates. See the authoring document Creating Page Templates, which details the capabilities of editable templates as exposed to the template author.

Note:

Following tutorial might also be of interest for setting up an editable page template in a new project:
Getting Started with AEM Sites Part 2 – Creating a Base Page and Template

Creating a New Template

Creating editable templates is primarily done with the template console and template editor by a template author. This section gives an overview of this process and follows with a description of what occurs at a technical level.

For information on how to use editable templates in an AEM project see Creating an AEM project using Lazybones.

When creating a new editable template you:

  • Create a folder for the templates. This is not mandatory, but is recommended best practice.

  • Select a template type. This is copied to create the template definition.

    Note:

    A selection of template types are provided out-of-the-box. You can also create your own site-specific template types if required.

  • Configure the structure, content policies, initial content, and layout of the new template.

    Structure

    • The structure allows you define components and content for your template.
    • Components defined in the template structure cannot be moved on a resulting page nor deleted from any resulting pages.
      • If you are creating a template in a custom folder outside of the We.Retail sample content, you can choose Foundation Components or use Core Components.
    • If you want page authors to be able to add and remove components, add a paragraph system to the template.
    • Components can be unlocked and locked again to allow you to define initial content.

    For details on how a template author defines the structure, see Creating Page Templates.

    For technical deatils of the structure, see Structure in this document.

    Policies

    • The content policies define the design properties of a component.
      • For example, the components available or minimum/maximum dimensions.
    • These are applicable to the template (and pages created with the template).

    For details on how a template author defines policies, see Creating Page Templates.

    For technical deatil of policies, see Content Policies in this document.

    Initial Content

    • Initial Content defines content that will appear when a page is first created based on the template.
    • Initial content can then be edited by page authors.

    For details on how a template author defines the structure, see Creating Page Templates.

    For technical details on initial content, see Initial Content in this document.

    Layout

    • You can define the template layout for a range of devices.
    • Responsive layout for templates operates as it does for page authoring.

    For details on how a template author defines the template layout, see Creating Page Templates.

    For technical details on template layout, see Layout in this document.

  • Enable the template, then allow it for specific content trees.

    • A template can be enabled or disabled to make it available or unavailable to page authors.
    • A template can be made available or unavailable for certain page branches.

    For details on how a template author enables a template, see Creating Page Templates.

    For technical deatils on enabling a template, see Enabling and Allowing a Template for Use in this document

  • Use it to create content pages.

    • When using a template to create a new page there is no visible difference and no indication between static and editable templates.
    • For the page author, the process is transparent.

    For details on how a page author uses templates to create a page, see Creating and Organizing Pages.

    For technical details on creating pages with editable templates, see Resultant Content Pages in this document.

Note:

The editor client library assumes the presence of the cq.shared namespace in content pages, and if it is absent the JavaScript error Uncaught TypeError: Cannot read property ‘shared’ of undefined will result.

All sample content pages contain cq.shared, so any content based on them automatically includes cq.shared. However, if you decide to create your own content pages from scratch without basing them on sample content, you must make sure to include the cq.sharednamespace.

See Using Client-Side Libraries for further information.

Caution:

Never enter any information that needs to be internationalized into a template.

Template Folders

For organizing your templates you can use the following folders:

  • global
  • Site-specific
    The site-specific folders that you create to organize your templates are created with an account holding admin priviliges.

Note:

Even though you can nest your folders, when the user views them in the Templates console they are presented as a flat structure.

In a standard AEM instance the global folder already exists in the template console. This holds default templates and acts as a fallback if no policies and/or template-types are found in the current folder. You can add your default templates to this folder or create a new folder (recommended).

Note:

It is best practice to create a new folder to hold your customized templates and not to use the global folder.

Caution:

Folders must be created by a user with admin rights.

Template types and policies are inherited across all folders according to the following order of precedence:

  1. The current folder.
  2. Parent(s) of the current folder.
  3. /conf/global
  4. /apps
  5. /libs

A list of all allowed entries is created. If any configurations overlap (path/label), only the instance closest to the current folder is presented to the user.

To create a new folder, you can either do this:

  • Programmatically or with CRXDE Lite
  • Using the Configuration Browser

Using CRXDE Lite

  • A new folder (under /conf) can be created for your instance either programmatically or with CRXDE Lite.

    The following structure must be used:

    1
    2
    3
    4
    5
    6
    /conf
        <your-folder-name> [sling:Folder]
            settings [sling:Folder]
                wcm [cq:Page]
                    templates [cq:Page]
                    policies [cq:Page]
  • You can then define the following properties on the folder root node:

    <your-folder-name> [sling:Folder]

    Name: jcr:title

    • Type: String
    • Value: The title (for the folder) you want to appear in the Templates console.
  • In addition to the standard authoring permissions and privileges (e.g. content-authors) you now need to assign group(s) and define the required access rights (ACLs) for your authors to be able to create templates in the new folder.

    The template-authors group is the default group that needs to be assigned. See the following section ACLs and Groups for details.

    See Access Right Management for full details on managing and assigning access rights.

Using the Configuration Browser

  • Go to Global Navigation -> Tools > Configuration Browser.

    The existing folders are listed to the left including the global folder.

     

  • Click Create.

  • In the Create Configuration dialog the following fields need to be configured:

    • Title: Provide a title for the configuration folder
    • Editable Templates: Tick to allow for editable templates within this folder
  • Click Create

Note:

In the Configuration Browser, you can edit the global folder and activate the Editable Templates option if you wish to create templates within this folder, however this is not recommended best practice.

ACLs and Groups

Once your template folders are created (either via CRXDE or with the Configuration Browser), ACLs must defined for the appropriate groups for the template folders to ensure proper security.

The template folders for the We.Retail reference implementation can be used as an example.

The template-authors Group

The template-authors group is the group used to manage access to templates and comes standard with AEM, but is empty. Users must be added to the group for the project/site.

Caution:

The template-authors group is only for users that must be able to create new templates.

Editing templates is very powerful and if not done properly exiting templates can be broken. Therefore this role should be focused and only include qualified users.

The following table details the necessary permissions for template editing.

Path Role / Group Permissions Description
/conf/<your-folder>/settings/wcm/templates Template Authors read, write, replicate Template authors that create, read, update, delete, and replicate templates in site specific /confspace
Anonymous Web User read Anonymous Web User must read templates while rendering a page
Content Authors replicate replicateContent authors need to activate the templates of a page when activating a page
/conf/<your-folder>/settings/wcm/policies Template Author read, write, replicate Template authors that create, read, update, delete, and replicate templates in site specific /conf space
Annonymous Web User read Anonymous Web User must read policies while rendering a page
Content Authors replicate Content authors need to activate the policies of a template of a page when activating a page
/conf/<site>/settings/template-types Template Author read Template author creates a new template based on one of the predefined template types.
Anonymous Web User none Anonymous Web User must not access the template types

This default template-authors group only covers the project setups, where all template-authors members are allowed to access and author all templates. For more complex setups, where multiple template authors groups are needed to separate access to templates, more custom template authors groups must be created. However the permissions for the template authors groups would still be the same.

Legacy Templates under /conf/global

Templates should no longer be stored in /conf/global, however for some legacy installations there may still be templates in this location. ONLY in such legacy situations should the following /conf/global paths be explicitly configured.

Path Role / Group Permissions Description
/conf/global/settings/wcm/templates Template Authors read, write, replicate Template authors that create, read, update, delete, and replicate templates in /conf/global
Anonymous Web User read Anonymous Web User must read templates while rendering a page
Content Authors replicate Content authors need to activate the templates of a page when activating a page
/conf/global/settings/wcm/policies Template Author read, write, replicate Template authors that create, read, update, delete, and replicate templates in /conf/global
Anonymous Web User read Anonymous Web User must read policies while rendering a page
Content Authors replicate Content authors need to activate the policies of a template of a page when activating a page
/conf/global/settings/wcm/template-types Template Author read Template author creates a new template based on one of the predefined template types
Anonymous Web User none Anonymous Web User must not access the template types

Template Type

When creating a new template you need to specify a template type:

  • Template types effectively provide templates for a template. When creating a new template the structure and initial content of the selected template type is used to create to the new template.
    • The template type is copied to create the template.
    • Once the copy has occurred, the only connection between the template and the template type is a static reference for information purposes.
  • Template types allow you to define:
    • The resource type of the page component.
    • The policy of the root node, which defines the components allowed in the template editor.
    • Optionally (but recommended), the breakpoints for the responsive grid and setup of the mobile emulator. This is optional, because the configuration could also be defined on the root page of the site.
  • AEM provides a small selection of out-of-the-box template types such as HTML5 Page and Adaptive Form Page.
    • Additional examples are provided as a part of the We.Retail sample content.
  • Template types are typically defined by developers.

The out-of-the box template types are stored under:

  • /libs/settings/wcm/template-types

Caution:

You must not change anything in the /libs path. This is because the content of /libs is overwritten the next time you upgrade your instance (and may be overwritten when you apply either a hotfix or feature pack).

Your site-specific template types should be stored in the comparable location of:

  • /apps/settings/wcm/template-types

Definitions for your customized templates types should be stored in user-defined folders (recommended) or alternatively in global. For example:

  • /conf/<my-folder-01>/<my-folder-02>/settings/wcm/template-types
  • /conf/<my-folder>/settings/wcm/template-types
  • /conf/global/settings/wcm/template-types

Caution:

The template types have to respect the correct folder structure (i.e. /settings/wcm/…), otherwise the template types will not be found.

Creating Template Types

If you have created a template that can serve as the basis of other templates, you can copy this template as a template type.

  • Create a template as you would any editable template as documented here, which will serve as the basis of your template type.

  • Using CRXDE Lite, copy the newly-created template from the templates node to the template-types node under the template folder.

  • Delete the template from the templates node under the template folder.

  • In the copy of the template that is under the template-types node, delete all cq:template and cq:templateType jcr:content properties.

You can also develop your own template type using an example editable template as a basis, available on GitHub.

CODE ON GITHUB

You can find the code of this page on GitHub

Template Definitions

Definitions for editable templates are stored user-defined folders (recommended) or alternatively in global. For example:

  • /conf/<my-folder>/settings/wcm/templates
  • /conf/<my-folder-01>/<my-folder-02>/settings/wcm/templates
  • /conf/global/settings/wcm/templates

The root node of the template is of type cq:Template with a skeleton structure of:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<template-name>
  initial
    jcr:content
      root
        <component>
        ...
        <component>
  jcr:content
    @property status
  policies
    jcr:content
      root
        @property cq:policy
        <component>
          @property cq:policy
        ...
        <component>
          @property cq:policy
  structure
    jcr:content
      root
        <component>
        ...
        <component>
      cq:responsive
        breakpoints
  thumbnail.png

The main elements are:

jcr:content

This node holds properties for the template:

  • Namejcr:title
  • Namestatus
    • TypeString
    • Valuedraftenabled or disabled

Structure

Defines the structure of the resultant page:

  • Is merged with the initial content (/initial) when creating a new page.
  • Changes made to the structure will be reflected in any pages created with the template.
  • The root (structure/jcr:content/root) node defines the list of components that will be available in the resulting page.
    • Components defined in the template structure cannot be moved on or deleted from any resultant pages.
    • Once a component is unlocked the editable property is set to true.
    • Once a component that already contains content is unlocked, this content will be moved to the initial branch.
  • The cq:responsive node holds definitions for the responsive layout.

Initial Content

Defines the initial content that a new page will have upon creation:

  • Contains a jcr:content node that is copied to any new pages.
  • Is merged with the structure (/structure) when creating a new page.
  • Any existing pages will not be updated if the initial content is changed after creation.
  • The root node holds a list of components to define what will be available in the resulting page.
  • If content is added to a component in structure mode and that component is subsequently unlocked (or vice versa), then this content is used as initial content.

Layout

Content Policies

The content (or design) policies define the design properties of a component. For example, the components available or minimum/maximum dimensions. These are applicable to the template (and pages created with the template). Content policies can be created and selected in the template editor of the touch-optimized UI.

  • The property cq:policy, on the root node
    /conf/<your-folder>/settings/wcm/templates/<your-template>/policies/jcr:content/root
    Provides a relative reference to the content policy for the page’s paragraph system.
  • The property cq:policy, on the component-explicit nodes under root, provide links to the policies for the individual components.
  • The actual policy definitions are stored under:
    /conf/<your-folder>/settings/wcm/policies/wcm/foundation/components

Note:

The paths of policy definitions depend on the path of the component. cq:policy holds a relative reference to the configuration itself.

Note:

Pages created from editable templates do not offer a Design mode in the page editor.

The policies tree of an editable template has the same hierarchy as the design mode configuration of a static template under:

    /etc/designs/<my-site>/jcr:content/<component-name>

The design mode configuration of a static template was defined per page component.

Page Policies

Page policies allow you to define the content policy for the page (main parsys), in either the template or resultant pages.

Enabling and Allowing a Template for Use

  • Enable the Template

    Before a template can be used it must be enabled by either:

    • Enabling the template from the Templates console.
    • Setting the status property on the jcr:content node.
      • For example, on:
        /conf/<your-folder>/settings/wcm/templates/<your-template>/jcr:content
      • Define the property:
        • Name: status
        • Type: String
        • Value: enabled
  • Allowed Templates

    For example, with a value of:

    /conf/<your-folder>/settings/wcm/templates/.*

Resultant Content Pages

Pages created from editable templates:

  • Are created with a subtree that is merged from structure and initial in the template
  • Have references to information held in the template and template type. This is achieved with a jcr:content node with the properties:
    • cq:template
      Provides the dynamic reference to the actual template; enables changes to the template to be reflected on the actual pages.
    • cq:templateType
      Provides a reference to the template type.
chlimage_1

The above diagram shows how templates, content, and components interrelate:

  • Controller – /content/<my-site>/<my-page>
    The resultant page that references the template. The content controls the entire process. According to the definitions it accesses the appropriate template and components.
  • Configuration – /conf/<my-folder>/settings/wcm/templates/<my-template>
    The template and related content policies define the page configuration.
  • Model – OSGi bundles
    The OSGI bundles implement the functionality.
  • View – /apps/<my-site>/components
    On both the author and publish environments the content is rendered by components.

When rendering a page:

  • Templates:

    • The cq:template property of its jcr:content node will be referenced to access the template that corresponds to that page.
  • Components:

    • The page component will merge the structure/jcr:content tree of the template with the jcr:content tree of the page.
    • The page component will only allow the author to edit the nodes of the template structure that have been flagged as editable (as well as any children).
    • When rendering a component on a page, the relative path of that component will be taken from the jcr:contentnode; the same path under the policies/jcr:content node of the template will then be searched.
      • The cq:policy property of this node points to the actual content policy (i.e. it holds the design configuration for that component).
      • This allows you to have multiple templates that re-use the same content policy configurations.

    Page Templates – Static

    A Template is used to create a Page and defines which components can be used within the selected scope. A template is a hierarchy of nodes that has the same structure as the page to be created, but without any actual content.

    Each Template will present you with a selection of components available for use.

    • Templates are built up of Components;
    • Components use, and allow access to, Widgets and these are used to render the Content.

    Note:

    Editable templates are also available.

    Properties and Child Nodes of a Template

    A template is a node of type cq:Template and has the following properties and child nodes:

    Name
    Type
    Description
    .  cq:Template Current template. A template is of node type cq:Template.
     allowedChildren  String[] Path of a template that is allowed to be a child of this template.
     allowedParents  String[] Path of a template that is allowed to be a parent of this template.
     allowedPaths  String[] Path of a page that is allowed to be based on this template.
     jcr:created  Date Date of creation of the template.
     jcr:description  String Description of the template.
     jcr:title  String Title of the template.
     ranking  Long Rank of the template. Used to display the template in the User Interface.
     jcr:content  cq:PageContent Node containing the content of the template.
     thumbnail.png  nt:file Thumbnail of the template.
     icon.png  nt:file Icon of the template.

    A template is the basis of a page.

    To create a page, the template must be copied (node-tree /apps/<myapp>/template/<mytemplate>) to the corresponding position in the site-tree: this is what happens if a page is created using the Websites tab.

    This copy action also gives the page its initial content (usually Top-Level Content only) and the property sling:resourceType, the path to the page component that is used to render the page (everything in the child node jcr:content).

    How Templates are structured

    There are two aspects to be considered:

    • the structure of the template itself
    • the structure of the content produced when a template is used

    The structure of a Template

    A Template is created under a node of type cq:Template.

    Various properties can be set, in particular:

    • jcr:title – title for the template; appears in the dialog when creating a page.
    • jcr:description – description for the template; appears in the dialog when creating a page.

    This node contains a jcr:content (cq:PageContent) node which be used as the basis for the content node of resulting pages; this references, using sling:resourceType, the component to be used for rendering the actual content of a new page.

    This component is used to define the structure and design of the content when a new page is created.

    The content produced by a Template

    Templates are used to create pages of type cq:Page (as mentioned earlier, a page is a special type of component). Each AEM Page has a structured node jcr:content. This:

    • is of type cq:PageContent
    • is a structured node-type holding a defined content-definition
    • has a property sling:resourceType to reference the component holding the sling scripts used for rendering the content

    Default Templates

    AEM comes with a number of default templates available out of the box. In some cases, you may want to use the templates as is. In that case, you need to ensure that the template is available for your web site.

    For example, AEM comes with several templates including a contentpage and home page.

    Title Component Location Purpose
    Home Page homepage geometrixx The Geometrixx home page template.
    Content Page contentpage geometrixx The Geometrixx content page template.

    Displaying Default Templates

    To see a list of all templates in the repository, proceed as follows:

    1. In CRXDE Lite, open the Tools menu and click Query.
    2. In the Query tab
    3. As Type, select XPath.
    4. In the Query input field, enter following string:
      //element(*, cq:Template)
    5. Click Execute. The list is displayed in the result box.

    In most cases, you will take an existing template and develop a new one for your own use. See Developing Page Templates for more information.

    To enable an existing template for your website and you want it to be displayed in the Create Page dialog when creating a page right under Websites from the Websites console, set the allowedPaths property of the template node to: /content(/.*)?

    Developing Page Templates

    AEM page templates are simply models used to create new pages. They can contain as little, or as much, initial content as needed, their role being to create the correct initial node structures, with the required properties (primarily sling:resourceType) set to allow editing and rendering.

    Creating a new Template (based on an existing template)

    Needless to say a new template can be created completely from scratch, but often an existing template will be copied and updated to save you time and effort. For example, the templates within Geometrixx can be used to get you started.

    To create a new template based on an existing template:

  • Copy an existing template (preferably with a definition as close as possible to what you want to achieve) to a new node.

    Templates are usually stored in /apps/<website-name>/templates/<template-name>.

    Note:

    The list of available templates depends on the location of the new page and the restrictions on placement specified in each template. See Template Availability.

  • Change the jcr:title of the new template node to reflect its new role. You can also update the jcr:description if appropriate. Be sure to change the template availability of the page as appropriate.

    Note:

    If you want your template to be displayed in the Create Page dialog when creating a page right under Websites from the Websitesconsole, set the allowedPaths property of the template node to: /content(/.*)?

    chlimage_1

  • Copy the component on which the template is based (this is indicated by the sling:resourceType property of the jcr:contentnode within the template) to create a new instance.

    Components are usually stored in /apps/<website-name>/components/<component-name>.

  • Update the jcr:title and jcr:description of the new component.

  • Replace the thumbnail.png if you want a new thumbnail picture to be shown in the template selection list (size 128 x 98 px).

  • Update the sling:resourceType of the template’s jcr:content node to reference the new component.

  • Make any further changes to the functionality or design of the template and/or its underlying component.

    Note:

    Changes made to the /apps/<website>/templates/<template-name> node will affect the template instance (as in the selection list).

    Changes made to the /apps/<website>/components/<component-name> node will affect the content page created when the template is used.

    You can now create a page within your website using the new template.

  • Note:

    The editor client library assumes the presence of the cq.shared namespace in content pages, and if it is absent the JavaScript error Uncaught TypeError: Cannot read property ‘shared’ of undefined will result.

    All sample content pages contain cq.shared, so any content based on them automatically includes cq.shared. However, if you decide to create your own content pages from scratch without basing them on sample content, you must make sure to include the cq.shared namespace.

    See Using Client-Side Libraries for further information.

    Note:

    The editor client library assumes the presence of the cq.shared namespace in content pages, and if it is absent the JavaScript error Uncaught TypeError: Cannot read property ‘shared’ of undefined will result.

    All sample content pages contain cq.shared, so any content based on them automatically includes cq.shared. However, if you decide to create your own content pages from scratch without basing them on sample content, you must make sure to include the cq.shared namespace.

    See Using Client-Side Libraries for further information.

    Making an Existing Template Available

    This example illustrates how to allow a template to be used for certain content paths. The templates that are available to the page author when creating new pages are determined by the logic defined in Template Availability.

  • In CRXDE Lite, navigate to the template you want to use for your page, for example, the Newsletter template.

  • Change the allowedPaths property and other properties used for template availabillity. For example, allowedPaths/content/geometrixx-outdoors/[^/]+(/.*)? means that this template is allowed in any path under /content/geometrixx-outdoors.

    chlimage_1

Leave a comment