-
Notifications
You must be signed in to change notification settings - Fork 15
The Context Instance
leizongmin edited this page Jan 19, 2016
·
4 revisions
When you render the templates, you need a context instance. The context instance contains the data and filters for rendering.
// 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);
Example:
context.setLocals('a', 123);
Template a={{a}}
will output a=123
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
Set the locals variable, will get the value from call the async function context.setAsyncLocals(name, fn)
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
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
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
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);
});
});
Example:
var html = context.getBuffer();
console.log(html);
Example:
var html = context.clearBuffer();
console.log(html);
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"
});
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"
});