The Context Instance - leizongmin/tinyliquid GitHub Wiki
When you render the templates, you need a context instance. The context instance contains the data and filters for rendering.
1. Create new context:
// empty context
var context = tinyliquid.newContext();
// with initialized data
var context = tinyliquid.newContext({
locals: {}, // optional, you can call context.setLocals() to add one by one
syncLocals: {}, // optional, context.setSyncLocals()
asyncLocals: {}, // optional, context.setAsyncLocals()
filters: {}, // optional, context.setFilter
asyncFilters: {} // optional, context.setFilter
});
// clone from other context instance
var context = tinyliquid.newContext();
context.from(otherContext);
2. Methods
context.setLocals(name, value)
Set the locals variable Example:
context.setLocals('a', 123);
Template a={{a}}
will output a=123
context.setSyncLocals(name, fn)
Set the locals variable, will get the value from call the function Example:
context.setSyncLocals('a', function (name, context) {
// the first argument is "a"
// the second argument is the current context instance
return name + ':123';
});
Template a={{a}}
will output a=a:123
context.setAsyncLocals(name, fn)
Set the locals variable, will get the value from call the async function Example:
context.setAsyncLocals('a', function (name, callback, context) {
// the first argument is "a"
// the secode argument is a callback function
// the third argument is the current context instance
setTimeout(function () {
callback(null, name + ':123';
}, 100);
});
Template a={{a}}
will output a=a:123
context.setFilter(name, fn)
Set the sync filter Example:
context.setFilter('max', function (a, b, context) {
// a and b is the filter arguments
// the last argument is the current context instance
return Math.max(a, b);
});
Template max={{ 1 | max: 2 }}
will output max=2
context.setAsyncFilter(name, fn)
Set the async filter Example:
context.setAsyncFilter('max', function (a, b, callback, context) {
// a and b is the filter arguments
// the last but one argument is a callback function
// the last argument is the current context instance
setTimeout(function () {
callback(null, Math.max(a, b));
}, 100);
});
Template max={{ 1 | max: 2 }}
will output max=2
context.onInclude(fn)
Set the include file handler Example:
context.onInclude(function (name, callback) {
// the first argument is the file name, if the tag is "{% include "abc" %}" then name="abc"
// you should resolve the file name firstly
// the secode argument is a callback function
// 1. read the file
fs.readFile(resolveTemplatePath(name), 'utf8', function (err, text) {
if (err) return callback(err);
// 2. compile the template
var ast = tinyliquid.parse(text);
// 3. callback
callback(null, ast);
});
});
context.getBuffer()
Get the template output buffer Example:
var html = context.getBuffer();
console.log(html);
context.getBuffer()
Returns the template output buffer, and clear Example:
var html = context.clearBuffer();
console.log(html);
context.fetchLocals(list, callback)
Get the locals value, this is useful in your custom tag Example:
context.setLocals('a', 123);
context.setAsyncLocals('b', function (name, callback) {
callback(null, 456);
});
// get single
context.fetchLocals('a', function (err, v) {
if (err) throw err;
console.log('a=%d', v); // will output "a=123"
});
// get list
context.fetchLocals(['a', 'b'], function (err, v) {
if (err) console.error(err);
console.log('a=%d, b=%d', v[0], v[1]); // will output "a=123, b=456"
});
context.callFilter(name, args, callback)
Call the filter, this is useful in your custom filter Example:
context.setAsyncFilter('max', function (a, b, callback) {
callback(null, Math.max(a, b));
});
context.callFilter('max', [1, 2], function (err, v) {
if (err) console.error(err);
console.log('max=%d', v); // will output "max=2"
});