infinity.template¶
Provides HTML templating functionality with variables, conditional execution, loops and includes by using a special syntax inside curly brackets.
Module: infinity.template
Template Syntax¶
A template is an HTML file containing expressions in curly brackets. The template is compiled into a script that is executed in a separate JavaScript engine when render() is called. All properties of vars and html are available as global variables inside the template expressions.
| Syntax | Description |
|---|---|
{%expression} | Outputs the result of the JavaScript expression (e.g. {%name} or {%i + 1}). The output is not encoded; string values of vars are HTML entity encoded before rendering, values of html are not. |
{#if condition} ... {:else if condition} ... {:else} ... {/if} | Conditional output. |
{#each items as item} ... {/each}{#each items as item, index} ... {/each} | Loop over the elements of an array (items must be a variable name). |
{@code statements} | Executes JavaScript statements (e.g. {@code var count = 0;}). |
{@include file.html}{@include file.html vars (a, b) html (c)} | Renders another template file from the document root folder (the file name may only contain letters, digits, _, . and -) at this position. The listed variables are passed to the vars or html object of the included template (a space is required between vars/html and the opening parenthesis). |
HTML comments (<!-- ... -->) are removed from the output and expressions inside comments are not evaluated. Inside the template, the global variables documentRoot, caching, expire and debug contain the settings of the rendering template object.
Example:
main.ts:
infinity.loadModule('infinity.http');
let myServer = new infinity.http.server(false, false, false, false, false, 80);
myServer.registerHandler(infinity.http.server.handler.custom, '/', '/custom.js');
myServer.start();
while (!infinity.terminated) {
infinity.event.processQueue();
infinity.sleep(25);
}
myServer.stop();
custom.ts:
infinity.loadModule('infinity.template');
let myTemplate = new infinity.template(infinity.current.root + '../templates/');
try {
let isCached = myTemplate.loadFromFile('index.html');
if (!isCached) {
myTemplate.vars['content'] = 'Loaded variable content';
myTemplate.vars['muchdata'] = ['Data1', 'Data2', 'Data3'];
myTemplate.vars['muchinfo'] = [{title: 'Title1', key: 'Info1'}, {title: 'Title2', key: 'Info2'}];
myTemplate.html['tag'] = '<b>bold</b>';
}
infinity.http.response.contentType = 'text/html; charset="UTF-8"';
infinity.http.response.encoding = infinity.encoding.utf8;
infinity.http.response.contentText = myTemplate.render();
} finally {
myTemplate.free();
}
index.html:
<html>
<head>
<title>INFINITY.JS Template</title>
</head>
<body>
<p>{%content}</p>
<p>Not encoded: {%tag}</p>
{#if muchdata.length > 0}
<ul>
{#each muchdata as item, i}
<li>{%i + 1}: {%item}</li>
{/each}
</ul>
{:else}
<p>No data</p>
{/if}
<table border="1" cellspacing="0">
{#each muchinfo as info}
<tr><td>{%info.title}</td><td>{%info.key}</td></tr>
{/each}
</table>
</body>
</html>
Folder structure:
+ example
+ ts
- main.ts
- custom.ts
+ templates
- index.html
template¶
The class for operating with HTML templates in INFINITY.JS.
Properties:
Methods:
Example:
infinity.loadModule('infinity.template');
let myTemplate = new infinity.template(infinity.current.root + '../templates/');
Properties
caching¶
Type: boolean
Specifies whether the rendered HTML output should be cached. If enabled, render() stores its result in a cache (shared by all template objects, identified by the template name), and loadFromFile() / loadFromStream() load the cached output instead of the template (and return true), as long as the cache entry has not expired. A cached output is returned by render() unchanged, regardless of the current variables. Default: false.
debug¶
Type: boolean
If set to true, render() returns the JavaScript source code generated from the template instead of the rendered output. Default: false.
documentRoot¶
Type: string
Gets the absolute path to the root folder for the template files (with trailing path delimiter).
expire¶
Type: number
Gets or sets the cache expiration time in seconds for the rendered output (see caching). 0 (the default) means that cached output does not expire. Negative values throw an error.
html¶
Type: object
Variables for the template that are not HTML entity encoded (e.g. HTML fragments). Each property of the object is available as global variable inside the template. Every template object has its own html object (up to 3.10 it was shared by all template objects of a runtime).
vars¶
Type: object
Variables for the template. Each property of the object is available as global variable inside the template. All string values (also inside nested objects and arrays) are HTML entity encoded before rendering. Every template object has its own vars object (up to 3.10 it was shared by all template objects of a runtime). Variables passed to an included template with @include ... vars(...) are encoded only once (up to 3.10 they were encoded twice).
Methods
constructor()¶
Lets you create an INFINITY.JS template object instance.
Signature:
constructor( documentRoot: string )
Parameters:
- documentRoot:
stringThe path to the root folder for the template files. Template names passed to the other methods are relative to this folder.
Example:
infinity.loadModule('infinity.template');
let myTemplate = new infinity.template(infinity.current.root + '../templates/');
clear()¶
Clears the currently loaded template from memory and resets vars and html to empty objects.
Signature:
clear(): void
Example:
myTemplate.clear();
free()¶
Frees the memory previously occupied by the template object instance. The instance must not be used afterwards.
Signature:
free(): void
Example:
myTemplate.free();
invalidate()¶
Removes the cached rendered output of the specified template (see caching).
Signature:
invalidate( templateName: string ): void
Parameters:
- templateName:
stringThe name of the template (relative to the document root) which should be invalidated inside the cache.
Example:
myTemplate.invalidate('index.html');
isCached()¶
Reports whether there is a non-expired cached rendered output for the specified template. Always returns false if caching is disabled.
Signature:
isCached( templateName: string ): boolean
Parameters:
- templateName:
stringThe name of the template (relative to the document root) for which the cache should be checked.
Return type: boolean
Example:
myTemplate.isCached('index.html');
loadFromFile()¶
Loads a template from the specified file (relative to the document root) into memory. Returns true if caching is enabled and a cached rendered output of the template was loaded instead; in this case, the variables do not need to be set before calling render(). Compiled templates are cached internally and recompiled when the file changes.
Signature:
loadFromFile( templateName: string ): boolean
Parameters:
- templateName:
stringThe name of the template file to be loaded.
Return type: boolean
Example:
myTemplate.loadFromFile('index.html');
loadFromStream()¶
Loads a template from the specified stream into memory. Returns true if caching is enabled and a cached rendered output for the given template name was loaded instead. Note that the compiled template is cached internally under the given template name: once a template has been compiled for a name, later calls with the same name use the compiled template and do not read the stream again.
Signature:
loadFromStream( stream: infinity.stream, templateName: string ): boolean
Parameters:
-
stream:
infinity.streamThe stream object to load from.
-
templateName:
stringThe name identifying the template (used for caching and error messages).
Return type: boolean
Example:
myTemplate.loadFromStream(stream, 'generated.html');
render()¶
Executes the loaded template with the current vars and html and returns the rendered output. Throws an error if the template contains a syntax or runtime error.
Signature:
render(): string
Return type: string
Example:
infinity.http.response.contentText = myTemplate.render();