---
title: Template Engine
slug: template-js/engine
docTags: 
createdAt: 2022-06-15T20:42:28.000Z
---



The template engine allows easy access to rendering tools that are present in the Klevu library.

## Core template functions

- **setData&#xA0;**– sets the **data** variable of the template
- **getData&#xA0;**– gets the **data** variable of the template
- **getHelpers&#xA0;**– get a full list of available template helpers
- **setHelper&#xA0;**– set new template helper or override existing one dependent on the name – setHelper: function ( name , functionSource )
- **render&#xA0;**– compile the js template based on name and return html in string format – render: function ( name /\* ,  data , dataLocal \*/ ) 
- **list&#xA0;**–  get a full list of available template names
- **setTemplate&#xA0;**– set a new template string as a template, decoded is used as a format for the templateString if set to false it will decode the template sting from base64 encoding – setTemplate : function ( templateString , name , decoded = false )
- **convertTemplate&#xA0;**– transform html string to DOM tree – return is encapsulated in a div element
- **insertTemplate&#xA0;**– append as chield a DOM node element to a parent DON node – insertTemplate : function ( parent , child )

## Template render

Compiles JavaScript templates into functions that can be evaluated for rendering. Useful for rendering complicated bits of HTML from JSON data sources.Template functions can both interpolate values, using \<%= … %>, as well as execute arbitrary JavaScript code, with \<% … %>. If you wish to interpolate a value and have it be HTML-escaped, use \<%- … %>. 

## Global template variables

Inside the template, you have access to the following js variables:

- **data&#xA0;**– contains the returned processed data and settings of the current request in the format:
  - settings – containing settings set as part of the global context example term, landingUrl,termOriginal
  - suggestions – containing processed response of the suggestion type request, refer to the API v2 Docs for a complete format
  - query – containing processed response of the query type request, refer to the API v2 Docs for a complete format
- **dataLocal&#xA0;**– contains the object send when using micro templates. Refer to the template helper.render section
- **helper&#xA0;**– contains the list of functions defined as template functions. Core functions include:
  - **translator&#xA0;**– able to access the scope translation object in full 
  - **translate&#xA0;**– map to the scope traslator.translate 
  - **addTranslation&#xA0;**– map to the scope traslator.addTranslation
  - **getTranslation&#xA0;**– map to the scope traslator.getTranslation
  - **removeTranslation&#xA0;**– map to the scope traslator.removeTranslation
  - **setTranslations&#xA0;**– map to the scope traslator.setTranslations
  - **processCurrency&#xA0;**– map to the scope currency.processCurrency
  - **each&#xA0;**– map to klevu.each
  - **buildUrl&#xA0;**– accepts 3 arguments – example helper.buildUrl(data.settings.landingUrl,”q” , “test”)
    - uri – original url
    - key – key that needs to be changed
    - value – the value of the change
  - **stripHtml&#xA0;**– accepts 1 argument – strips any HTML tags from the input string
  - **render&#xA0;**– accepts 4 arguments
    - **name&#xA0;**– the name of the set template
    - **scope&#xA0;**– the scope the template will be rendered in
    - **data&#xA0;**– optional – will be taken from scope if not provided – data to be set in the data object of the rendered template
    - **dataLocal&#xA0;**– optional – will be defaulted to data – data to be set in the dataLocal of the rendered template

## Scope

The scope of the template engine is dependent on what Search Object Scope it is linked to. Each search object will contain as part of its scope a instance of the template engine. All data and templates are executed as part of that scope. If a user wants to use the same template in 2 or more search objects then the template has to be set for each of the template engines.

The template source can be any location where a full sting containing the template code can be extracted from , example: a element contents in main DOM , an AJAX response , an encoded variable.

Template engine accepts encoded templates in the base 64 encoding by parsing the third parameter to false(this is the default).











.

