npm.io
1.0.1 • Published 13 years ago

ert

Licence
MIT
Version
1.0.1
Deps
1
Vulns
0
Weekly
0
Stars
8

ert

Express routing templates provides a simple micro-templating syntax for building strings from a template and a request object.

Build Status Dependency Status

Installation

$ npm install ert

Usage

var ert = require('ert');
var express = require('express');
var app = express();

app.get('/:package', function (req, res) {
  res.redirect(ert(req, '/package/:package'));
});

app.listen(3000);

Syntax

Code Blocks

e.g.

ert(req, '/foo/bar/[req.parms.x || "default-x"]')

Code in squre brackets ([ and ]) is evalutated as a JavaScript expression. It has req available as an object. It also supports shortcuts, so you could write the above as:

ert(req, '/foo/bar/[:x || "default-x"]')
Shortucts

There are characters that are shortucts, the defaults of these are:

exports.shortcutMap = {
  ':': 'req.params',
  '

These can be used directly like:

ert(req, '/foo/bar/:x')

which is equivallent to:

ert(req, '/foo/bar/[req.params && req.params["x"]]')

API

ert(req, src) or ert.translate(req, src)

Transforms the string with req and returns the resulting string. It caches the compiled function so should be pretty good for performance.

ert.compile(src)

Compiles src and returns a JavaScript expression as a string. This can be used to generate client side code with something like:

function client(name) {
  return 'function name(req){return ' + ert.compile(src) + '}';
}
ert.compileFn(src)

Compiles src and returns a function which expects a single argument, request.

License

MIT

: 'req.query', '@': 'req.body' };

These can be used directly like:

__CODE_BLOCK_5__

which is equivallent to:

__CODE_BLOCK_6__

API

ert(req, src) or ert.translate(req, src)

Transforms the string with __INLINE_CODE_4__ and returns the resulting string. It caches the compiled function so should be pretty good for performance.

ert.compile(src)

Compiles __INLINE_CODE_5__ and returns a JavaScript expression as a string. This can be used to generate client side code with something like:

__CODE_BLOCK_7__
ert.compileFn(src)

Compiles __INLINE_CODE_6__ and returns a function which expects a single argument, __INLINE_CODE_7__.

License

MIT

Keywords