API: engines
Source:
template/engines.ts
An internal wrapper class that adapts a native template engine implementation. It manages the engine instance and optional custom start/end markers.
Usage
import { engines } from "sdk/template"
import { response } from "sdk/http";
let mustache = engines.getMustacheEngine();
let generated = mustache.generate('Hello {{name}}', [['name', 'John Smith']]);
response.println(generated);
response.flush();
response.close();Classes
TemplateEngine
An internal wrapper class that adapts a native template engine implementation.
It manages the engine instance and optional custom start/end markers.
Methods
generate
generate (location:string, template:string, parameters:{[key:string]:any}):stringGenerates the final output by executing the template with the provided parameters.
Note: Parameters are internally serialized to JSON before being passed to the native engine.
@param location A string identifying the template (used for error reporting/caching, often a file path).
@param template The raw template string content to process.
@param parameters An object containing the context data to be used in the template.
@returns The processed output string.
setSm
setSm (sm:any):any)Sets a custom start marker for the template engine. This is primarily useful for Mustache.
@param sm The new start marker string.
setEm
setEm (em:any):any)Sets a custom end marker for the template engine. This is primarily useful for Mustache.
@param em The new end marker string.
TemplateEngines
Provides access to various server-side template engines (Velocity, Mustache, JavaScript).
It offers utility methods for generating content from templates directly or from files
stored in the registry.
Methods
getDefaultEngine
getDefaultEngine ():TemplateEngineRetrieves the default template engine, which is currently the Velocity engine.
@returns The default template engine instance.
getMustacheEngine
getMustacheEngine ():TemplateEngineRetrieves the Mustache template engine instance.
Mustache is often used for logic-less templating and uses ' and ' as default markers.
@returns The Mustache template engine instance.
getVelocityEngine
getVelocityEngine ():TemplateEngineRetrieves the Velocity template engine instance.
Velocity is often used for complex templating with directives (e.g., #set, #foreach).
@returns The Velocity template engine instance.
getJavascriptEngine
getJavascriptEngine ():TemplateEngineRetrieves the JavaScript template engine instance (usually used for server-side evaluation).
@returns The JavaScript template engine instance.
generate
generate (location:string, template:string, parameters:{[key:string]:any}):stringGenerates output by processing a raw template string using the default template engine (Velocity).
@param location A string identifying the template (used for error reporting/caching, often a file path).
@param template The raw template string content to process.
@param parameters An object containing key-value pairs to be used as context variables in the template.
@returns The processed output string.
generateFromFile
generateFromFile (location:string, parameters:{[key:string]:any}):string|undefinedLoads a template from the public registry, selects an appropriate engine, and generates output.
It uses the Mustache engine if the file extension is.mustache, otherwise it uses the default (Velocity).
@param location The path to the template file within the/registry/public/directory (e.g., 'templates/email.mustache').
@param parameters An object containing key-value pairs to be used as context variables in the template.
@returns The processed output string, orundefinedif the resource does not exist.