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

Set the locals variable context.setLocals(name, value)

Example:

context.setLocals('a', 123);

Template a={{a}} will output a=123

Set the locals variable, will get the value from call the function context.setSyncLocals(name, fn)

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

Set the sync filter context.setFilter(name, fn)

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

Set the async filter context.setAsyncFilter(name, fn)

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

Set the include file handler context.onInclude(fn)

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);
  });
});

Get the template output buffer context.getBuffer()

Example:

var html = context.getBuffer();
console.log(html);

Returns the template output buffer, and clear context.getBuffer()

Example:

var html = context.clearBuffer();
console.log(html);

Get the locals value, this is useful in your custom tag context.fetchLocals(list, callback)

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"
});

Call the filter, this is useful in your custom filter context.callFilter(name, args, callback)

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"
});