# vmplate

> Simple templates for node programs

Latest version **1.1.2** (published 2015-10-25) · ISC license · 0 weekly downloads

## Install

```sh
npm install vmplate
pnpm add vmplate
yarn add vmplate
bun add vmplate
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.2 |
| Published | 2015-10-25 |
| First published | 2015-10-17 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Jeremy Ruppel |
| Maintainers | jeremyruppel |
| Keywords | template, mini, micro, vm, cli, file |

## Links

- npm: https://www.npmjs.com/package/vmplate
- Repository: git@github.com:jeremyruppel/vmplate
- Homepage: https://github.com/jeremyruppel/vmplate
- Issues: https://github.com/jeremyruppel/vmplate/issues
- npm.io page: https://npm.io/package/vmplate

## Dependencies (1)

- [debug](https://npm.io/package/debug.md) ^2.2.0

## Alternatives

- [unionfs](https://npm.io/package/unionfs.md) — 2.2M weekly downloads
- [path-starts-with](https://npm.io/package/path-starts-with.md) — 35.9K weekly downloads
- [redzip](https://npm.io/package/redzip.md) — 1.2K weekly downloads
- [vscode-anymatch](https://npm.io/package/vscode-anymatch.md) — 848 weekly downloads
- [@ledgerhq/coin-filecoin](https://npm.io/package/@ledgerhq/coin-filecoin.md) — 793 weekly downloads

## Recent versions

- 1.1.2 (latest) — 2015-10-25
- 1.1.1 — 2015-10-17
- 1.1.0 — 2015-10-17
- 1.0.2 — 2015-10-17
- 1.0.1 — 2015-10-17

## README

# vmplate

Simple templates for node programs.

[![Build Status](https://travis-ci.org/jeremyruppel/vmplate.svg?branch=master)](https://travis-ci.org/jeremyruppel/vmplate)

vmplate templates depend on the [vm][1] module to create a reusable template context. They're great for command-line programs or templating files, but they don't do any escaping and they won't work in the browser, so they're a _bad choice for HTML_.

# Install

```
$ npm install vmplate
```

# Usage

``` js
var vmplate = require('vmplate');

var render = vmplate('hello <%= name %>!')

console.log(render({ name: 'world' })); // hello world!
```

### vmplate(string[, locals[, filename]])

Compiles a new vmplate function for `string`.

If you pass in a `locals` hash, this will be used as the render function's locals hash. Otherwise, it's an empty hash that you can modify after the fact.

``` js
var render = vmplate('hello <%= name %>!', { name: 'world' });

console.log(render()); // hello world!
console.log(render({ name: 'beer' })); // hello beer!

render.locals.name = 'nasty';

console.log(render()); // hello nasty!
```

If you provide a `filename`, it will be used as the filename of the vm script and only really shows up in error messages.

### templating

vmplate uses [ejs][2]-style delimiters, but there are only two supported operations.

`<%= foo %>` (note the *=*) will add the value of `foo` to the buffer. The program will throw if `foo` is not defined.

`<% var foo = "bar"; %>` will evaluate the expression but will _not_ add it to the buffer. This makes any expression that's also valid javascript usable in your template.

For example, here's how to iterate over an array:

``` js
var render = subject('<% for(var i = 0; i < arr.length; i++){ %><%= arr[i] %><% } %>');

console.log(render({ arr: [1, 2, 3] })); // 123
```

The program will throw if there is a syntax error in your javascript.

### vmplate.locals → render.locals → locals

There are four ways to provide locals to your vmplate:

The locals hash on vmplate itself:

``` js
var vmplate = require('vmplate');
var render = vmplate('<%= foo %>');

vmplate.locals.foo = 'yay';

console.log(render()); // yay
```

The locals hash on the vmplate instance:

``` js
var render = vmplate('<%= foo %>');

render.locals.foo = 'yay';

console.log(render()); // yay
```

The locals hash passed to the vmplate factory:

``` js
var render = vmplate('<%= foo %>', { foo: 'yay' });

console.log(render()); // yay
```

Or the locals hash passed to the render function:

``` js
var render = vmplate('<%= foo %>');

console.log(render({ foo: 'yay' })); // yay
```

Render locals inherit from instance locals, which in turn inherit from the vmplate factory locals.

## License

[ISC License](https://github.com/jeremyruppel/vmplate/blob/master/LICENSE)

[1]: https://nodejs.org/api/vm.html
[2]: https://github.com/mde/ejs

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