infinity.template¶
Provides extended HTML templating functionality with variables, conditional execution, and loops by using a special syntax and tag names.
Module: infinity.template
Example:
main.ts:
infinity.loadModule('infinity.http');
let myServer = new infinity.http.server(false, false, false, false, 80, 1024, 64, 104857600, 5.0);
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/');
myTemplate.loadFromFile('index.html');
myTemplate.vars['content'] = 'Loaded variable content';
myTemplate.vars['muchdata'] = ['Data1', 'Data2', 'Data3'];
myTemplate.vars['muchinfo'] = [{title:'Title1', key:'Info1'}, {title:'Title2', key:'Info2'}, {title:'Title3', key:'Info3'}];
infinity.http.response.contentText = myTemplate.render();
index.html:
<html>
<head>
<title>INFINITY.JS Template</title>
</head>
<body>
<if:write if:set-content="vars['content']">Default content</if:write>
<if:if if:condition="vars['muchdata'] && Array.isArray(vars['muchdata'])">
<ul>
<if:loop if:items="vars['muchdata']" if:element="item" if:info="info">
<li>
<if:write if:set-content="item">Default Data</if:write>
</li>
</if:loop>
</ul>
</if:if>
<if:if if:condition="vars['muchinfo'] && Array.isArray(vars['muchinfo'])">
<table border="1" cellspacing="0">
<if:loop if:items="vars['muchinfo']" if:element="item" if:info="info">
<tr>
<td>
<if:write if:set-content="item['title']">Default Title</if:write>
</td>
<td>
<if:write if:set-content="item['key']">Default Info</if:write>
</td>
</tr>
</if:loop>
</table>
</if:if>
</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(documentRoot);
Properties
caching¶
Type: boolean
Specifies whether template caching should be used.
debug¶
Type: boolean
Prints out the template file content directly to the output
expire¶
Type: number
Gets or sets the cache expiration time in seconds.
html¶
Type: object
Holds the compiled HTML output of a template.
vars¶
Type: object
This property acts as a global array across templates. It can be filled with data and used for output throughout the templates.
documentRoot¶
Type: string
Gets the path to the root folder for the template files.
Methods
constructor()¶
Lets you create an INFINITY.JS template object instance.
Signature:
constructor( documentRoot: string )
Parameters:
- documentRoot:
string
The path to the root folder for the template files.
Example:
infinity.loadModule('infinity.template');
let myTemplate = new infinity.template(infinity.current.root + '../templates/');
clear()¶
Clears the current loaded template from memory.
Signature:
clear(): void
Example:
myTemplate.clear();
free()¶
Frees the memory previously occupied by the template object instance.
Signature:
free(): void
Example:
myTemplate.free();
invalidate()¶
Clears the template cache for the specified template.
Signature:
invalidate( templateName: string ): void
Parameters:
- templateName:
string
The templateName which should be invalidated inside the cache.
Example:
myTemplate.invalidate('index.html');
isCached()¶
Reports whether there is a cached template for the specified script.
Signature:
isCached( templateName: string ): boolean
Parameters:
- templateName:
string
The script for which the template cache should be checked.
Return type: boolean
Example:
myTemplate.isCached('index.html');
loadFromFile()¶
Loads a template from the specified file into memory.
Signature:
loadFromFile( templateName: string ): boolean
Parameters:
- templateName:
string
The templateName to be loaded.
Return type: boolean
Example:
myTemplate.loadFromFile('index.html');
loadFromStream()¶
Loads a template from the specified stream into memory.
Signature:
loadFromStream( stream: infinity.stream, scriptName: string ): boolean
Parameters:
-
stream:
infinity.stream
The stream object to load from.
-
scriptName:
string
The scriptName to be loaded.
Return type: boolean
Example:
myTemplate.loadFromStream(stream, scriptName);
render()¶
Outputs the template contents to the client with its instructions carried out.
Signature:
render(): string
Return type: string
Example:
myTemplate.render();