Creating custom templates

Templates available from the Code tab are defined in an XML format that describes the code snippet that are available for the different nodes in the tree. To create a template, add a new file or copy an existing template to the templates folder: %localappdata%\Qlik\Templates. The templates are defined according an XML schema containing the following elements:

  • Top level template schema
  • Code elements
  • Snippet elements

Template format XML Schema for the full schema for the template format

Top level template schema

The top level XML element of the template must define a unique name for the template as well as the template version used. Currently only version 1.0 is supported. The following schema defines the structure of a template definition for version 1.0:

<xs:element name="template">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="description" type="xs:string" minOccurs="0"/>
      <xs:element name="code" type="codeType" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
    <xs:attribute name="name" type="xs:string" use="required"/>
    <xs:attribute name="templateVersion" type="xs:string" use="required" fixed="1.0"/>
  </xs:complexType>
</xs:element>
Note: The description element is optional and should provide information about the intended usage of the template.

Code elements

Each code element provides a collection of code snippets and information about which nodes in the tree those snippets are available for. The code elements are the main data-carrying elements of the template, and their structure is defined below.

<xs:complexType name="codeType">
  <xs:sequence>
    <xs:element name="snippet" type="snippetType" minOccurs="0" maxOccurs="unbounded" />
  </xs:sequence>
  <xs:attribute name="engineType"       type="xs:string" default=".*"/>
  <xs:attribute name="exceptEngineType" type="xs:string" default="$."/>
  <xs:attribute name="clientType"       type="xs:string" default=".*"/>
  <xs:attribute name="exceptClientType" type="xs:string" default="$."/>
</xs:complexType>

Engine type attributes

The "engineType" and "exceptEngineType" attributes are used to include or exclude node types in the tree. For example, a code element with "engineType" set to "app" provide snippets for app nodes in the tree, and a code element with "exceptEngineType" set to "app" provide snippets for all nodes except apps.

Engine type literals for a full list of the supported engine types.

Client type attributes

The "clientType" and "exceptClientType" attributes are used to bind snippets to generic objects of specific types.

For example, if a code element has "engineType" set to "genericobject" and "clientType" set to "sheet", then that code binds to sheets but not any other generic objects. Likewise, if "engineType" is set to "genericobject" and "exceptClientType" is set to "sheet", then the code binds to all generic objects except sheets

Client type literals for a full list of the supported client types.

Regular expressions as type selector

The template format supports the use of regular expressions for the engine type and client type attributes. This makes it possible to bind a code element to multiple types in the tree.

Note: Omitting the "engineType" or "clientType" attributes is semantically equivalent to setting them to a regular expression that matches everything.

Omitting the "exceptEngineType" or "exceptClientType" attributes is semantically equivalent to setting them to a regular expression that matches nothing.

Snippet elements

The content of snippet elements contain the text that is available for drag and drop from the code tab. The title attribute of a snippet is used as the header for that snippet in the GUI.

<xs:complexType name="snippetType">
  <xs:simpleContent>
    <xs:extension base="xs:string">
      <xs:attribute name="title" type="xs:string"/>
    </xs:extension>
  </xs:simpleContent>
</xs:complexType>

Node variables

Snippets can contain variables that are expanded based on the node the snippet is available for. The set of variables available for a node depends on the engine type of the tree node that the snippet is bound to. All nodes provide a title property that, when used, expands to the text available as the title of the node in the tree. Additionally, server nodes provide properties relating to the server URI, and all other nodes provides the engine ID of the node.

The following properties are available for the different engine types:

Engine type Available properties
server
  • title
  • id
  • uri
  • host
  • port
  • prefix
  • isSecure
  • properties
app
  • title
  • id
  • properties
field
  • title
  • id
  • properties
genericBookmark
  • title
  • id
  • properties
genericObject
  • title
  • id
  • properties

Variable expansion

Variables are accessed in snippets by including them using Mustache syntax. Qlik Analytics developer tool replaces the variables with the relevant information from the node for which the snippet is bound to.

Example: Define a snippet for the ClearAll action for apps

<code engineType="app">
  <snippet title="ClearAll" > <![CDATA[ <button type='button' onclick="qlikApps['{{id}}'].clearAll()"> Clear All </button> ]]> </snippet>
</code>

It is also possible to access information from a parent node by prefixing the variable with the engine type of the node from which the information is collected.

Example: Define a snippet providing code for applying a bookmark

In this example, the variable id expands to the ID of the genericbookmark that the snippet is bound to. The variable app.id expands to the ID of the app where the bookmark is defined.

<code engineType="genericbookmark">
  <snippet title="Apply bookmark"> <![CDATA[ <button type='button' onclick="qlikApps['{{app.id}}'].bookmark.apply('{{id}}')"> {{title}} </button> ]]>
  </snippet>
</code>

Engine type literals

The following list contains the supported engine type literals that a node in the tree can match:

  • server
  • app
  • field
  • genericbookmark
  • genericdimension
  • genericobject
  • genericmeasure
  • genericvariable

Client type literals

The following list contains the supported client type literals that a node in the tree can match:

  • barchart
  • combochart
  • filterpane
  • gauge
  • kpi
  • linechart
  • listbox
  • map
  • piechart
  • pivot-table
  • scatterplot
  • sheet
  • table
  • text-image
  • treemap

Template format XML Schema

The full schema for the template format is described here.

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="template">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="description" type="xs:string" minOccurs="0"/>
        <xs:element name="code" type="codeType" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
      <xs:attribute name="name" type="xs:string" use="required"/>
      <xs:attribute name="templateVersion" type="xs:string" use="required" fixed="1.0"/>
    </xs:complexType>
  </xs:element>
  <xs:complexType name="codeType">
    <xs:sequence>
      <xs:element name="snippet" type="snippetType" minOccurs="0" maxOccurs="unbounded" />
    </xs:sequence>
    <xs:attribute name="engineType"       type="xs:string" default=".*"/>
    <xs:attribute name="exceptEngineType" type="xs:string" default="$."/>
    <xs:attribute name="clientType"       type="xs:string" default=".*"/>
    <xs:attribute name="exceptClientType" type="xs:string" default="$."/>
  </xs:complexType>
  <xs:complexType name="snippetType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute name="title" type="xs:string"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:schema>

Did this information help you?

Thanks for letting us know. Is there anything you'd like to tell us about this topic?

Can you tell us why it did not help you and how we can improve it?