README.md
1# pug-runtime
2
3The runtime components for the pug templating language
4
5[](https://travis-ci.org/pugjs/pug-runtime)
6[](https://david-dm.org/pugjs/pug?path=packages/pug-runtime)
7[](https://david-dm.org/pugjs/pug?path=packages/pug-runtime&type=dev)
8[](https://www.npmjs.org/package/pug-runtime)
9
10## Installation
11
12 npm install pug-runtime
13
14## Usage
15
16
17You can call runtime methods directly using `runtime.method`. This is particularly useful when compiling to deal with things that are already known at compile time.
18
19```js
20var runtime = require('pug-runtime');
21
22assert(runtime.attr('foo', 'bar', true, true) === ' foo="bar"');
23```
24
25You can also build a string with a given list of functions available as `pug_method` by calling `build(arrayOfMethods)`. This is useful for inlining runtime functions within the compiled templates.
26
27```js
28var build = require('pug-runtime/build');
29var src = build(['attr']);
30
31var attr = Function('', src + ';return pug_attr;')();
32assert(attr('foo', 'bar', true, true) === ' foo="bar"');
33```
34
35When testing code compiled for the browser in Node.js, it is necessary to make the runtime available. To do so, one can use `require('pug-runtime/wrap')`:
36
37```js
38var pug = require('pug');
39var wrap = require('pug-runtime/wrap');
40
41var pugSrc = 'p= content';
42// By default compileClient automatically embeds the needed runtime functions,
43// rendering this module useless.
44var compiledCode = pug.compileClient(pugSrc, {
45 externalRuntime: true
46});
47//=> 'function template (locals) { ... pug.escape() ... }'
48
49var templateFunc = wrap(compiledCode);
50templateFunc({content: 'Hey!'});
51//=> '<p>Hey!</p>'
52
53// Change template function name to 'heyTemplate'
54compiledCode = pug.compileClient(pugSrc, {
55 externalRuntime: true,
56 name: 'heyTemplate'
57});
58//=> 'function heyTemplate (locals) { ... }'
59
60templateFunc = wrap(compiledCode, 'heyTemplate');
61templateFunc({content: 'Hey!'});
62//=> '<p>Hey!</p>'
63```
64
65
66## License
67
68 MIT
69