# @zokugun/template

> Fast template engine forked from doT with extended and extendable tags & cache plugin

Latest version **0.1.1** (published 2026-04-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install @zokugun/template
pnpm add @zokugun/template
yarn add @zokugun/template
bun add @zokugun/template
```

## Health

**Score 60/100 (C)** — status: active.

Positive: esm support; no vulnerabilities; has provenance; high maintenance score.

Warnings: low downloads; no types; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.1.1 |
| Published | 2026-04-16 |
| First published | 2025-12-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 61.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 0 |
| Author | Baptiste Augrain |
| Maintainers | daiyam |
| Keywords | template, templating, zokugun |

## Links

- npm: https://www.npmjs.com/package/@zokugun/template
- Repository: https://github.com/zokugun/node-template
- Issues: https://github.com/zokugun/node-template/discussions/categories/issue-triage
- npm.io page: https://npm.io/package/@zokugun/template

## Recent versions

- 0.1.1 (latest) — 2026-04-16
- 0.1.0 — 2025-12-18

## README

[@zokugun/template](https://github.com/zokugun/node-template)
==========================================================

[![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](./LICENSE)
[![NPM Version](https://img.shields.io/npm/v/@zokugun/template.svg?colorB=green)](https://www.npmjs.com/package/@zokugun/template)
[![Donation](https://img.shields.io/badge/donate-ko--fi-green)](https://ko-fi.com/daiyam)
[![Donation](https://img.shields.io/badge/donate-liberapay-green)](https://liberapay.com/daiyam/donate)
[![Donation](https://img.shields.io/badge/donate-paypal-green)](https://paypal.me/daiyam99)

`@zokugun/template` is a fast template engine forked from [doT](https://github.com/olado/doT) with extended and extendable tags.

Getting Started
---------------

With [node](http://nodejs.org) previously installed:

	npm install @zokugun/template

```typescript
import { template } from '@zokugun/template';

var myTemplate = template.compile('It\'s nice to meet you, {{:it}}.');
console.log(myTemplate('miss White'));
```

Differences from doT
--------------------

doT			| zokugun.template					| Description
---			| ----------------					| -----------
`{{ }}`		| <code>{{&#124; &#124;}}</code>	| for evaluation
`{{= }}`	| `{{: }}`							| for interpolation
`{{! }}`	| `{{! }}`							| for interpolation with encoding
`{{# }}`	| use `{{: }}`						| for using partials
`{{## #}}`	| `{{#name(args)}} {{#}}`			| for defining partials
`{{? }}`	| `{{? }}`							| for conditionals
`{{~ }}`	| `{{~ }}`							| for array iteration
			| `{{. }}`							| for object iteration
			| `{{% }}`							| for while iteration
			| `{{^ }}`							| for do/while iteration
			| `{{[ ]}}`							| for range iteration
			| `{{/ }}` `{{\ }}`					| for block
			| <code>{{` }}</code>				| for escaping template
			| `{{-- --}}`						| for comments

`{{ }}` has been changed to `{{| |}}` to be able to do:
```
{{|
	function hello(name) {
		if(arguments.length) {
			return 'hello ' + name;
		}
		else {
			return 'hello world!';
		}
	};
|}}
{{:hello('foo')}}
```

API
---

```typescript
import { template, Template } from '@zokugun/template';
```

The variable *template* is the default template compiler. It contains the tags describe below.

### constructor(tags, options)

Create your own template compiler with the constructor of the `Template` class.

The arguments *tags* and *options* can be optionals.
By default, *options* will be:
```
{
	varnames: 'it',
	strip: true,      // remove spaces in javascript code. Be careful of missing ;
	append: true      // use string concatenation or addition assignment operator
}
```

Example:
```typescript
const custom = new Template({
	interpolate: {
		regex: /\$\{([\s\S]+?)\}/g,
		replace: function(m, code) {
			return this.cse.start + 'it.' + code + this.cse.end;
		}
	}
});

const hello = custom.compile('Hello ${firstname}');
console.log(hello({
	firstname: 'John',
	lastname: 'Doe'
}));
```

### template.addTag(name, regex, replace)

The method *addTag* allow you to add new tag so he can extends the compiler.
The tags are executed in alphabetic order. So its name will determine when the tag will be executed.

The function *replace* will be called as in str.replace(*regex*, *replace*) excepted that the variable *this* will an object like:

```
{
	cse: {
		start: string,         // start of the code
		end: string,           // end of the code
		startencode: string,   // start of the code that will be HTML escaped
		endencode: string      // end of the code that will be HTML escaped
	},
	unescape: function(str),   // unescape the code to pass from the template to the function's code
	sid: integer               // the sid for the variables' names
}
```

### template.clearTags()

The method *clearTags* removes all the tags defined in the compiler.

### template.compile(template, options)

The function *compile* returns a function based of the string *template*.
The argument *options* will overwrite the default options of the compiler.

The first line of the *template* can also contains options for the compiler. It must start with '{{}} ' and the options separated with spaces.

```
{{}} strip:true
hello {{:it.firstname}}
```

```
{{}} strip:false varnames:firstname,lastname
hello {{:firstname}}
```

### template.removeTag(name)

The method *clearTags* removes the tag named *name*.

### template.run(template, variables, options)

The method *run* will firstly compiles the *template* with the *options* and the *variables*' names. Then it will execute the resulting function with the *variables*.

```
console.log(template.run('It\'s nice to meet you, {{:name}}.', {
	name: 'miss White'
}));
```

This is the least efficient to use a template. Because the template will be compiled every time.

Tags
----

### Interpolation

<table>
	<thead>
		<tr>
			<th>Template</th>
			<th>Data</th>
			<th>Result</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td>
				<pre>
```
<div>It\'s nice to meet you, {{:it.name}}!</div>
<div>Today, you have {{:it.age}}.</div>
```
				</pre>
			</td>
			<td>
				<pre>
<code class="lang-javascript">{
	name: 'Jake',
	age: 32
}</code>
				</pre>
			</td>
			<td>
				<pre>
```
<div>It\'s nice to meet you, Jake!</div>
<div>Today, you have 31.</div>
```
				</pre>
			</td>
		</tr>
	</tbody>
</table>

### Interpolation with encoding

<table>
	<thead>
		<tr>
			<th>Template</th>
			<th>Data</th>
			<th>Result</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td>
				<pre>
```
<a href="{{!it.url}}">{{:it.title}}</a>
```
				</pre>
			</td>
			<td>
				<pre>
<code class="lang-javascript">{
	title: 'github',
	url: 'https://github.com'
}</code>
				</pre>
			</td>
			<td>
				<pre>
```
<a href="https://github.com">github</a>
```
				</pre>
			</td>
		</tr>
	</tbody>
</table>

### Evaluation

<table>
	<thead>
		<tr>
			<th>Template</th>
			<th>Data</th>
			<th>Result</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td>
				<pre>
```
{{|
	function hello(name) {
		if(arguments.length) {
			return 'hello ' + name;
		}
		else {
			return 'hello world!';
		}
	};
|}}
<div>{{:hello(it.name)}}</div>
```
				</pre>
			</td>
			<td>
				<pre>
<code class="lang-javascript">{
	name: 'Jake'
}</code>
				</pre>
			</td>
			<td>
				<pre>
```
<div>hello Jake</div>
```
				</pre>
			</td>
		</tr>
	</tbody>
</table>

### Conditionals

<table>
	<thead>
		<tr>
			<th>Template</th>
			<th>Data</th>
			<th>Result</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td rowspan="3">
				<pre>
```
{{?it.morning}}
good morning
{{??it.evening}}
good evening
{{??}}
hello
{{?}}
```
				</pre>
			</td>
			<td>
				<pre>
```
{
	morning: true
}
```
				</pre>
			</td>
			<td>
				<pre>
```
good morning
```
				</pre>
			</td>
		</tr>
		<tr>
			<td>
				<pre>
```
{
	evening: true
}
```
				</pre>
			</td>
			<td>
				<pre>
```
good evening
```
				</pre>
			</td>
		</tr>
		<tr>
			<td>
				<pre>
```
{
}
```
				</pre>
			</td>
			<td>
				<pre>
```
hello
```
				</pre>
			</td>
		</tr>
	</tbody>
</table>

### Array Iteration

<table>
	<thead>
		<tr>
			<th>Template</th>
			<th>Data</th>
			<th>Result</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td>
				<pre>
```
{{~it :value}}
<div>{{:value}}</div>
{{~}}
```
				</pre>
			</td>
			<td rowspan="4">
				<pre>
```
['banana','apple','orange']
```
				</pre>
			</td>
			<td>
				<pre>
```
<div>banana</div>
<div>apple</div>
<div>orange</div>
```
				</pre>
			</td>
		</tr>
		<tr>
			<td>
				<pre>
```
{{~it :value:index}}
<div class="fruit{{:index%2}}">{{:value}}</div>
{{~}}
```
				</pre>
			</td>
			<td>
				<pre>
```
<div class="fruit0">banana</div>
<div class="fruit1">apple</div>
<div class="fruit0">orange</div>
```
				</pre>
			</td>
		</tr>
		<tr>
			<td>
				<pre>
```
{{~~it :value}}
<div>{{:value}}</div>
{{~}}
```
				</pre>
			</td>
			<td>
				<pre>
```
<div>orange</div>
<div>apple</div>
<div>banana</div>
```
				</pre>
			</td>
		</tr>
		<tr>
			<td>
				<pre>
```
{{~~it :value:index}}
<div class="fruit{{:index%2}}">{{:value}}</div>
{{~}}
```
				</pre>
			</td>
			<td>
				<pre>
```
<div class="fruit0">orange</div>
<div class="fruit1">apple</div>
<div class="fruit0">banana</div>
```
				</pre>
			</td>
		</tr>
	</tbody>
</table>

### Object Iteration

<table>
	<thead>
		<tr>
			<th>Template</th>
			<th>Data</th>
			<th>Result</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td>
				<pre>
```
{{.it :value}}
<div>{{:value}}</div>
{{.}}
```
				</pre>
			</td>
			<td rowspan="2">
				<pre>
```
{
	firstname: 'John',
	lastname: 'Doe',
	age: 25
}
```
				</pre>
			</td>
			<td>
				<pre>
```
<div>John</div>
<div>Doe</div>
<div>25</div>
```
				</pre>
			</td>
		</tr>
		<tr>
			<td>
				<pre>
```
{{.it :value:key}}
<div class="{{:key}}">{{:value}}</div>
{{.}}
```
				</pre>
			</td>
			<td>
				<pre>
```
<div class="firstname">John</div>
<div class="lastname">Doe</div>
<div class="age">25</div>
```
				</pre>
			</td>
		</tr>
	</tbody>
</table>

### While Iteration

<table>
	<thead>
		<tr>
			<th>Template</th>
			<th>Data</th>
			<th>Result</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td>
				<pre>
```
{{%4 :i}}
{{:i}}
{{%i-1}}
```
				</pre>
			</td>
			<td>
				<pre>
```
{
}
```
				</pre>
			</td>
			<td>
				<pre>
```
4
3
2
1
```
				</pre>
			</td>
		</tr>
	</tbody>
</table>

### Do/While Iteration

<table>
	<thead>
		<tr>
			<th>Template</th>
			<th>Data</th>
			<th>Result</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td>
				<pre>
```
{{^4 :i}}
{{:i}}
{{^i-1}}
```
				</pre>
			</td>
			<td>
				<pre>
```
{
}
```
				</pre>
			</td>
			<td>
				<pre>
```
4
3
2
1
```
				</pre>
			</td>
		</tr>
	</tbody>
</table>

### Range Iteration

<table>
	<thead>
		<tr>
			<th>Template</th>
			<th>Data</th>
			<th>Result</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td>
				<pre>
```
{{[i 0..3]}}
{{:i}}
{{[]}}
```
				</pre>
			</td>
			<td rowspan="3">
				<pre>
```
{
}
```
				</pre>
			</td>
			<td>
				<pre>
```
0
1
2
3
```
				</pre>
			</td>
		</tr>
		<tr>
			<td>
				<pre>
```
{{[i 3..0]}}
{{:i}}
{{[]}}
```
				</pre>
			</td>
			<td>
				<pre>
```
3
2
1
0
```
				</pre>
			</td>
		</tr>
		<tr>
			<td>
				<pre>
```
{{[i 0..3 :2]}}
{{:i}}
{{[]}}
```
				</pre>
			</td>
			<td>
				<pre>
```
0
1
```
				</pre>
			</td>
		</tr>
		<tr>
			<td>
				<pre>
```
{{[i 0..it]}}
{{:i}}
{{[]}}
```
				</pre>
			</td>
			<td>
				<pre>
```
3
```
				</pre>
			</td>
			<td>
				<pre>
```
0
1
2
3
```
				</pre>
			</td>
		</tr>
	</tbody>
</table>

### Partials

<table>
	<thead>
		<tr>
			<th>Template</th>
			<th>Data</th>
			<th>Result</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td>
				<pre>
<code>
{{#hello}}
	Hello world!
{{#}}

{{:hello()}}
</code>
				</pre>
			</td>
			<td>
				<pre>
<code>
{
}
</code>
				</pre>
			</td>
			<td>
				<pre>
<code>
Hello world!
</code>
				</pre>
			</td>
		</tr>
		<tr>
			<td>
				<pre>
<code>
{{#hello(name)}}
	Hello {{:name}}!
{{#}}

{{:hello(it.name)}}
</code>
				</pre>
			</td>
			<td>
				<pre>
<code>
{
	name: 'Jake'
}
</code>
				</pre>
			</td>
			<td>
				<pre>
<code>
Hello Jake!
</code>
				</pre>
			</td>
		</tr>
	</tbody>
</table>

### Blocks

<table>
	<thead>
		<tr>
			<th>Template</th>
			<th>Data</th>
			<th>Result</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td>
				<pre>
```
{{|var i = 0;|}}
{{/do}}
{{:i}}
{{\while(++i <= 3)}}
```
				</pre>
			</td>
			<td>
				<pre>
```
{
}
```
				</pre>
			</td>
			<td>
				<pre>
```
0
1
2
3
```
				</pre>
			</td>
		</tr>
		<tr>
			<td>
				<pre>
```
{{/for(var i = 0; i <= 3; i++)}}
{{:i}}
{{\}}
```
				</pre>
			</td>
			<td>
				<pre>
```
{
}
```
				</pre>
			</td>
			<td>
				<pre>
```
0
1
2
3
```
				</pre>
			</td>
		</tr>
	</tbody>
</table>

### Comments

<table>
	<thead>
		<tr>
			<th>Template</th>
			<th>Data</th>
			<th>Result</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td>
				<pre>
```
Hello {{--{{:it.name}}--}}
```
				</pre>
			</td>
			<td>
				<pre>
```
{
	name: 'Jake'
}
```
				</pre>
			</td>
			<td>
				<pre>
```
Hello
```
				</pre>
			</td>
		</tr>
	</tbody>
</table>

### Escape

<table>
	<thead>
		<tr>
			<th>Template</th>
			<th>Data</th>
			<th>Result</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td>
				<pre>
```
Hello {{`it.name}}
```
				</pre>
			</td>
			<td>
				<pre>
```
{
	name: 'Jake'
}
```
				</pre>
			</td>
			<td>
				<pre>
```
Hello {{it.name}}
```
				</pre>
			</td>
		</tr>
	</tbody>
</table>

Forked from
-----------

* [Laura Doktorova's doT](https://github.com/olado/doT)
* [Mario Gutierrez's doT](https://github.com/mgutz/doT)

Donations
---------

Support this project by becoming a financial contributor.

<table>
    <tr>
        <td><img src="https://raw.githubusercontent.com/daiyam/assets/master/icons/256/funding_kofi.png" alt="Ko-fi" width="80px" height="80px"></td>
        <td><a href="https://ko-fi.com/daiyam" target="_blank">ko-fi.com/daiyam</a></td>
    </tr>
    <tr>
        <td><img src="https://raw.githubusercontent.com/daiyam/assets/master/icons/256/funding_liberapay.png" alt="Liberapay" width="80px" height="80px"></td>
        <td><a href="https://liberapay.com/daiyam/donate" target="_blank">liberapay.com/daiyam/donate</a></td>
    </tr>
    <tr>
        <td><img src="https://raw.githubusercontent.com/daiyam/assets/master/icons/256/funding_paypal.png" alt="PayPal" width="80px" height="80px"></td>
        <td><a href="https://paypal.me/daiyam99" target="_blank">paypal.me/daiyam99</a></td>
    </tr>
</table>

License
-------

Copyright &copy; 2013-present Baptiste Augrain

Licensed under the [MIT license](https://opensource.org/licenses/MIT).

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