# ut-function.template

> Fast and tiny template engine

Latest version **1.7.6** (published 2023-10-19) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install ut-function.template
pnpm add ut-function.template
yarn add ut-function.template
bun add ut-function.template
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.7.6 |
| Published | 2023-10-19 |
| First published | 2019-09-11 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 35.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Maintainers | kalin.krustev |

## Links

- npm: https://www.npmjs.com/package/ut-function.template
- Repository: https://github.com/softwaregroup-bg/ut-function
- Homepage: https://github.com/softwaregroup-bg/ut-function#readme
- Issues: https://github.com/softwaregroup-bg/ut-function/issues
- npm.io page: https://npm.io/package/ut-function.template

## Dependencies (1)

- [acorn](https://npm.io/package/acorn.md) ^8.10.0

## Recent versions

- 1.7.6 (latest) — 2023-10-19
- 1.7.5 — 2023-08-01
- 1.7.4 — 2023-07-21
- 1.7.3 — 2022-12-19
- 1.7.2 — 2022-11-23
- 1.7.1 — 2022-09-08
- 1.7.0 — 2022-09-07
- 1.6.7 — 2022-06-30
- 1.6.6 — 2022-06-10
- 1.6.5 — 2022-06-10
- 1.6.4 — 2022-05-23
- 1.6.3 — 2020-02-25
- 1.6.2 — 2020-01-15
- 1.6.1 — 2020-01-15
- 1.6.0 — 2020-01-14
- … 8 more at https://npm.io/package/ut-function.template/versions

## README

# 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:

```js
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.

```js
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"
  }
}
*/

```

---
_Source: https://npm.io/package/ut-function.template · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
