Wjst API
Object Type: WjstOpts
Source: https://github.com/WebArtWork/wjst/blob/master/lib/wjst.js#L17
Wjst Options Object. This object can be passed to many of the API-level Wjst methods to control various aspects of the engine. All keys are optional.
Properties
Name | Type | Description |
---|---|---|
autoescape |
boolean | Controls whether or not variable output will automatically be escaped for safe HTML
output.
Defaults to true . Functions executed in variable
statements will
not
be auto-escaped. Your application/functions should take care of their own auto-escaping.
|
varControls |
array | Open and close controls for variables. Defaults to ['{{', '}}'] . |
tagControls |
array | Open and close controls for tags. Defaults to ['{%', '%}'] . |
cmtControls |
array | Open and close controls for comments. Defaults to ['{#', '#}'] . |
locals |
object | Default variable context to be passed to all templates. |
cache |
CacheOptions | Cache control for templates. Defaults to saving in 'memory' . Send false to disable. Send an object with
get and set functions to customize. |
loader |
TemplateLoader | The method that Wjst will use to load templates. Defaults to wjst.loaders.fs . |
Object Type: CacheOptions
Source: https://github.com/WebArtWork/wjst/blob/master/lib/wjst.js#L34
Cache control for templates. Defaults to saving all templates into memory.
Examples
// Default
wjst.setDefaults({ cache: 'memory' });
// Disables caching in Wjst.
wjst.setDefaults({ cache: false });
// Custom cache storage and retrieval
wjst.setDefaults({
cache: {
get: function (key) { ... },
set: function (key, val) { ... }
}
});
Object Type: TemplateLoader
Source: https://github.com/WebArtWork/wjst/blob/master/lib/wjst.js#L53
Configure Wjst to use either the wjst.loaders.fs
or wjst.loaders.memory
template loader. Or, you can write your own!
For more information, please see the Template Loaders documentation.
Examples
// Default, FileSystem loader
wjst.setDefaults({ loader: wjst.loaders.fs() });
// FileSystem loader allowing a base path
// With this, you don't use relative URLs in your template references
wjst.setDefaults({ loader: wjst.loaders.fs(__dirname + '/templates') });
// Memory Loader
wjst.setDefaults({ loader: wjst.loaders.memory({
layout: '{% block foo %}{% endblock %}',
page1: '{% extends "layout" %}{% block foo %}Tacos!{% endblock %}'
})});
wjst.version
Source: https://github.com/WebArtWork/wjst/blob/master/lib/wjst.js#L15
Wjst version number as a string.
Examples
if (wjst.version === "1.4.2") { ... }
wjst.setDefaults (options)
Source: https://github.com/WebArtWork/wjst/blob/master/lib/wjst.js#L143
Set defaults for the base and all new Wjst environments.
Parameters
Name | Type | Optional | Default | Description |
---|---|---|---|---|
options |
WjstOpts |
✓ | {} |
Wjst options object. |
Returns
undefined
:
Examples
wjst.setDefaults({ cache: false });
// => Disables Cache
wjst.setDefaults({ locals: { now: function () { return new Date(); } }});
// => sets a globally accessible method for all template
// contexts, allowing you to print the current date
// => {{ now()|date('F jS, Y') }}
wjst.setDefaultTZOffset (offset)
Source: https://github.com/WebArtWork/wjst/blob/master/lib/wjst.js#L153
Set the default TimeZone offset for date formatting via the date filter. This is a global setting and will affect all Wjst environments, old or new.
Parameters
Name | Type | Optional | Default | Description |
---|---|---|---|---|
offset |
number |
undefined |
Offset from GMT, in minutes. |
Returns
undefined
:
wjst.Wjst (opts)
Source: https://github.com/WebArtWork/wjst/blob/master/lib/wjst.js#L171
Create a new, separate Wjst compile/render environment.
Parameters
Name | Type | Optional | Default | Description |
---|---|---|---|---|
opts |
WjstOpts |
✓ | {} |
Wjst options object. |
Returns
object
:New Wjst environment.
Examples
const wjst = require('wjst');
const mywjst = new wjst.Wjst({varControls: ['<%=', '%>']});
mywjst.render('Tacos are <%= tacos =>!', { locals: { tacos: 'delicious' }});
// => Tacos are delicious!
wjst.render('Tacos are <%= tacos =>!', { locals: { tacos: 'delicious' }});
// => 'Tacos are <%= tacos =>!'
wjst.invalidateCache
Source: https://github.com/WebArtWork/wjst/blob/master/lib/wjst.js#L270
Clears the in-memory template cache.
Returns
undefined
:
Examples
swig.invalidateCache () ;
wjst.setFilter (name, method)
Source: https://github.com/WebArtWork/wjst/blob/master/lib/wjst.js#L270
Add a custom filter for wjst variables.
Parameters
Name | Type | Optional | Default | Description |
---|---|---|---|---|
name |
string |
undefined |
Name of filter, used in templates. Will overwrite previously defined filters, if using the same name. | |
method |
function |
undefined |
Function that acts against the input. See Custom Filters for more information. |
Returns
undefined
:
Examples
function replaceMs(input) { return input.replace(/m/g, 'f'); }
wjst.setFilter('replaceMs', replaceMs);
// => {{ "onomatopoeia"|replaceMs }}
// => onofatopeia
wjst.setTag (name, parse, compile, ends, blockLevel)
Source: https://github.com/WebArtWork/wjst/blob/master/lib/wjst.js#L295
Add a custom tag. To expose your own extensions to compiled template code, see wjst.setExtension.
For a more in-depth explanation of writing custom tags, see Custom Tags.
Parameters
Name | Type | Optional | Default | Description |
---|---|---|---|---|
name |
string |
undefined |
Tag name. | |
parse |
function |
undefined |
Method for parsing tokens. | |
compile |
function |
undefined |
Method for compiling renderable output. | |
ends |
boolean |
✓ | false |
Whether or not this tag requires an end tag. |
parse |
function |
✓ | false |
If false, this tag will not be compiled outside of block
tags
when extending a parent template. |
Returns
undefined
:
Examples
const tacotag = require('./tacotag');
wjst.setTag('tacos', tacotag.parse, tacotag.compile, tacotag.ends, tacotag.blockLevel);
// => {% tacos %}Make this be tacos.{% endtacos %}
// => Tacos tacos tacos tacos.
wjst.setExtension (name, object)
Source: https://github.com/WebArtWork/wjst/blob/master/lib/wjst.js#L326
Add extensions for custom tags. This allows any custom tag to access a globally available methods via a
special globally available object, _ext
, in templates.
For a more in-depth explanation of writing custom tags, see Custom Tags.
Parameters
Name | Type | Optional | Default | Description |
---|---|---|---|---|
name |
string |
undefined |
Tag name. | |
object |
* |
false |
The method, value, or object that should be available via the given name. |
Returns
undefined
:
Examples
const tacotag = require('./tacotag');
wjst.setTag('tacos', tacotag.parse, tacotag.compile, tacotag.ends, tacotag.blockLevel);
// => {% tacos %}Make this be tacos.{% endtacos %}
// => Tacos tacos tacos tacos.
wjst.precompile (source, options)
Source: https://github.com/WebArtWork/wjst/blob/master/lib/wjst.js#L485
Pre-compile a source string into a cache-able template function.
When rendering a source string, a file path should be specified in the options object in order for extends
, include
, and import
to work properly. Do this by adding { filename: '/absolute/path/to/mytpl.html' }
to the
options argument.
Parameters
Name | Type | Optional | Default | Description |
---|---|---|---|---|
source |
string |
undefined |
Wjst template source string. | |
options |
WjstOpts |
✓ | {} |
Wjst options object. |
Returns
object
:Renderable function and tokens object.
Examples
wjst.precompile('{{ tacos }}');
// => {
// tpl: function (_wjst, _locals, _filters, _utils, _fn) { ... },
// tokens: {
// name: undefined,
// parent: null,
// tokens: [...],
// blocks: {}
// }
// }
In order to render a pre-compiled template, you must have access to filters and utils from Wjst. <var>efn</var> is simply an empty function that does nothing.
wjst.render (source, options)
Source: https://github.com/WebArtWork/wjst/blob/master/lib/wjst.js#L523
Compile and render a template string for final output.
When rendering a source string, a file path should be specified in the options object in order for extends
, include
, and import
to work properly. Do this by adding { filename: '/absolute/path/to/mytpl.html' }
to the
options argument.
Parameters
Name | Type | Optional | Default | Description |
---|---|---|---|---|
source |
string |
undefined |
Wjst template source string. | |
options |
WjstOpts |
✓ | {} |
Wjst options object. |
Returns
string
:Rendered output.
Examples
wjst.render('{{ tacos }}', { locals: { tacos: 'Tacos!!!!' }});
// => Tacos!!!!
wjst.renderFile (pathName, locals, cb)
Source: https://github.com/WebArtWork/wjst/blob/master/lib/wjst.js#L547
Compile and render a template file for final output. This is most useful for libraries like Express.js.
Parameters
Name | Type | Optional | Default | Description |
---|---|---|---|---|
pathName |
string |
undefined |
File location. | |
locals |
object |
✓ | {} |
Template variable context. |
cb |
Function |
✓ | undefined |
Asyncronous callback function. If not provided, compileFile will run syncronously. |
Returns
string
:Rendered output.
Examples
wjst.renderFile('./template.html', {}, function (err, output) {
if (err) {
throw err;
}
console.log(output);
});
wjst.renderFile('./template.html', {});
// => output
wjst.compile (source, options)
Source: https://github.com/WebArtWork/wjst/blob/master/lib/wjst.js#L592
Compile string source into a renderable template function.
Parameters
Name | Type | Optional | Default | Description |
---|---|---|---|---|
source |
string |
undefined |
Wjst template source string. | |
options |
WjstOpts |
✓ | {} |
Wjst options object. |
Returns
function
:Renderable function with keys for parent, blocks, and tokens.
Examples
const tpl = wjst.compile('{{ tacos }}');
// => {
// [Function: compiled]
// parent: null,
// tokens: [{ compile: [Function] }],
// blocks: {}
// }
tpl({ tacos: 'Tacos!!!!' });
// => Tacos!!!!
When compiling a source string, a file path should be specified in the options object in order for <var>extends</var>, <var>include</var>, and <var>import</var> to work properly. Do this by adding <code class="JavaScript">{ filename: '/absolute/path/to/mytpl.html' }</code> to the options argument.
wjst.compileFile (pathname, options, cb)
Source: https://github.com/WebArtWork/wjst/blob/master/lib/wjst.js#L653
Compile a source file into a renderable template function.
Parameters
Name | Type | Optional | Default | Description |
---|---|---|---|---|
pathname |
string |
undefined |
File location. | |
options |
WjstOpts |
✓ | {} |
Wjst options object. |
cb |
Function |
✓ | undefined |
Asyncronous callback function. If not provided, compileFile will run syncronously. |
Returns
function
:Renderable function with keys for parent, blocks, and tokens.
Examples
const tpl = wjst.compileFile('./mytpl.html');
// => {
// [Function: compiled]
// parent: null,
// tokens: [{ compile: [Function] }],
// blocks: {}
// }
tpl({ tacos: 'Tacos!!!!' });
// => Tacos!!!!
wjst.compileFile('/myfile.txt', { varControls: ['<%=', '=%>'], tagControls: ['<%', '%>']});
// => will compile 'myfile.txt' using the var and tag controls as specified.
wjst.run (tpl, locals, filepath)
Source: https://github.com/WebArtWork/wjst/blob/master/lib/wjst.js#L715
Run a pre-compiled template function. This is most useful in the browser when you've pre-compiled your templates with the Wjst command-line tool.
Parameters
Name | Type | Optional | Default | Description |
---|---|---|---|---|
tpl |
function |
undefined |
Pre-compiled Wjst template function. Use the Wjst CLI to compile your templates. | |
locals |
object |
✓ | {} |
Template variable context. |
filepath |
string |
✓ | undefined |
Filename used for caching the template. |
Returns
string
:Rendered output.
Examples
$ wjst compile ./mytpl.html --wrap-start="var mytpl = " > mytpl.js
<script src="mytpl.js"></script>
<script>
wjst.run(mytpl, {});
// => "rendered template..."
</script>