custom-tpl
A lightweight object template. Recursively interpolates all string properties.
Last updated 5 months ago by trezub .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install custom-tpl 
SYNC missed versions from official npm registry.

custom-tpl

A lightweight object template. Recursively interpolates all string properties.

npm i custom-tpl

If a string contains only a tag, and no more text, the string will be replaced by the object value. Else, the object's string representation will be used instead.

Usage example:

const Interpolate = require('./tpl.js')('${', '}');
var tpl = {
    text: 'Var: ${Text} ${Text2}',
    print: function () {
        console.log(this);
    },
    array: ['${Name}', '${LastName}'],
    obj: {
        innertext: '${Text2}',
        innertext2: '${Text3} Parameter: ${Object}', // String representation of a object
        parameter: '${Object}', // Object reference.
        innerobj: {
            innertext: '${Text2} ${example} ${DateTime}'
        }
    }
};
var obj = {
    Text: 'First text',
    Text2: 'Example',
    Text3: 'This is a test.',
    Name: 'Joe',
    LastName: 'Smith',
    DateTime: new Date().toLocaleString(),
    Object: {
        a: 1,
        b: 2
    }
};
let v = Interpolate(tpl, obj);
v.print();

Output:

{
  text: 'Var: First text Example',
  print: [Function: print],
  array: [ 'Joe', 'Smith' ],
  obj: {
    innertext: 'Example',
    innertext2: 'This is a test. Parameter: [object Object]',
    parameter: { a: 1, b: 2 },
    innerobj: { innertext: 'Example ${example} 10/03/2020 08:42:58' }
  }
}

Current Tags

  • 2.0.1                                ...           latest (5 months ago)

3 Versions

  • 2.0.1                                ...           5 months ago
  • 2.0.0                                ...           5 months ago
  • 1.0.1                                ...           5 months ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (0)
None
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |