1.7.6 • Published 7 months ago

ut-function.template v1.7.6

Weekly downloads
9
License
Apache-2.0
Repository
github
Last release
7 months ago

ut-function.template

Fast and tiny template engine

Usage

function template(string, params, ut, escape)

Parameters:

  • string: Template to render
  • params: Pass one of:
    • object: Template is evaluated, properties of the passed object are available as variables in the template, using ${...} expressions
    • array: Template rendering function is returned, with arguments named after the strings in tha passed array.
  • ut: object, exposed as a global variable named ut, which can be accessed inside ${...} expressions. It is usually helpful to expose utility functions. By default the following functions are available:
    • ut.join(delimiter): Joins the arguments with the delimiter and bypass escaping
    • ut.xml: Tag a template to escape XML characters in placeholders
    • ut.html: Tag a template to escape HTML characters in placeholders
    • ut.json: Tag a template to use JSON.stringify() in placeholders
  • escape: default escaping to apply. Can be one of:
    • 'html': HTML escaping
    • 'xml': XML escaping
    • 'json': JSON escaping
    • anything else: means no escaping

Result:

  • The passed string with ${...} expressions evaluated

Example:

const template = require('ut-function.template')
const format = value => (
    value instanceof Date ? value : new Date(value)
).toUTCString();
const templateString = 'UTC time: ${ut.format(time)}';

template(templateString, {time: 0}, {format});
// => UTC time: Thu, 01 Jan 1970 00:00:00 GMT

const time = template(templateString, ['time'], {format});
time(0);
// => UTC time: Thu, 01 Jan 1970 00:00:00 GMT

const loop = template(
  '<items>${ut.join(items.map(item => ut.xml`<item>${item}</item>`))}</items>',
  ['items'],
  {},
  'xml'
);
loop(['a', 'b', 'c']);
// <items><item>a</item><item>b</item><item>c</item></items>

recursive rendering

The first argument of the template function can be an object instead of a string. In that case the engine will iterate through the object recursively and will render each string value separately treating it as a standalone template.

E.g.

const result = template(
  {
      a: ['${add(10, 20)}'],
      b: '${subtract(10, 20)}',
      c: {
          d: '${multiply(10, 20)}'
      }
  },
  {
      add: (x, y) => x + y,
      subtract: (x, y) => x - y,
      multiply: (x, y) => x * y
  }
);

/* result would be:
{
  "a": ["30"],
  "b": "-10",
  "c": {
    "d": "200"
  }
}
*/
1.7.6

7 months ago

1.7.5

9 months ago

1.7.4

10 months ago

1.7.3

1 year ago

1.7.2

1 year ago

1.7.1

2 years ago

1.7.0

2 years ago

1.6.4

2 years ago

1.6.7

2 years ago

1.6.6

2 years ago

1.6.5

2 years ago

1.6.3

4 years ago

1.6.2

4 years ago

1.6.1

4 years ago

1.6.0

4 years ago

1.5.1

4 years ago

1.5.0

4 years ago

1.4.0

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.1

5 years ago