custom-tpl

2.0.1 • Public • Published

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' }
  }
}

Package Sidebar

Install

npm i custom-tpl

Weekly Downloads

3

Version

2.0.1

License

MIT

Unpacked Size

6.08 kB

Total Files

5

Last publish

Collaborators

  • trezub