npm.io
1.0.1 • Published 10 years ago

pjs-template

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

PJS Template - An asynchronous templating engine

npm version Dependencies Build Status Code Coverage

An async rendering template engine used by Pajamas. PJS syntax is based on EJS and can handle asynchronous templates easily.

Installation

npm install pjs-template

Usage

var pjs = require('pjs-template');

pjs.renderFile(path, data, options, function (err, html) { /* ... */ });
// or
pjs.render(str, data, options, function (err, html) { /* ... */ });
// or
var template = pjs.compile(str, options);
template(data, function (err, html) { /* ... */ });

With Express.js:

app.engine('pjs', require('pjs-template').__express);
app.set('view engine', 'pjs');
// You can use 'view options' to set the pjs options
app.set('view options', {
  cache: true,
  delimiter: '

Example

Template hello.pjs:

<%
var foo = 'bar';
setTimeout(function () {
  foo = 'PJS';
  done(); // tell PJS it's an async block
}, 100);
%>
Hello <%= foo %>!

Render the file:

var pjs = require('pjs-template');

pjs.renderFile('./hello.pjs', { foo: "bar" }, function (err, html) {
  console.log(html);
  // Display: Hello PJS!
});

The done() method tell PJS that it's an async block and to wait until done() is called.

If your block is not asynchronous, you don't need to use it:

<% var foo = 'bar'; %>
Hello <%= foo %>!

Will display Hello bar!

Options

  • cache (boolean) - Compiled functions are cached, requires filename option when used with the render method
  • filename - Used by cache to key caches, and for includes
  • watchFiles (boolean) - Require cache: true, watch for changes on the cached files to clear their cache automatically
  • debug - Output generated function body
  • compileDebug - When false no debug instrumentation is compiled
  • delimiter - Character to use with angle brackets for open/close
  • escapeFunction - Custom function for escaping HTML

Tags

  • <% 'Scriptlet' tag, for control-flow, no output
  • <%= Outputs the value into the template (HTML escaped)
  • <%- Outputs the unescaped value into the template
  • <%# Comment tag, no execution, no output
  • <%% Outputs a literal '<%'
  • %> Plain ending tag
  • -%> Trim-mode ('newline slurp') tag, trims following newline

Includes

Includes are relatives to the template with the include call.

<% include ./hello.pjs %>

Customer Delimiters

Custom delimiters can be applied on a per-template basis, or globally:

var pjs = require('pjs-template'),
    users = ['geddy', 'neil', 'alex'];

// Just one template
pjs.render('<?= users.join(" | "); ?>', { users: users }, { delimiter: '?' }, function (err, html) {
  // html = 'geddy | neil | alex'
});

// Or globally
pjs.delimiter = '

Methods

  • pjs.renderFile(path [, data] [, opts], callback)
  • pjs.render(str [, data] [, opts], callback)
  • pjs.compile(str [, opts])
  • pjs.clearCache()
  • pjs.escape(html)
});

Example

Template __INLINE_CODE_1__:

__CODE_BLOCK_2__

Render the file:

__CODE_BLOCK_3__

The __INLINE_CODE_2__ method tell PJS that it's an async block and to wait until done() is called.

If your block is not asynchronous, you don't need to use it:

__CODE_BLOCK_4__

Will display __INLINE_CODE_3__

Options

  • __INLINE_CODE_4__ (boolean) - Compiled functions are cached, requires __INLINE_CODE_5__ option when used with the __INLINE_CODE_6__ method
  • __INLINE_CODE_7__ - Used by cache to key caches, and for includes
  • __INLINE_CODE_8__ (boolean) - Require __INLINE_CODE_9__, watch for changes on the cached files to clear their cache automatically
  • __INLINE_CODE_10__ - Output generated function body
  • __INLINE_CODE_11__ - When false no debug instrumentation is compiled
  • __INLINE_CODE_12__ - Character to use with angle brackets for open/close
  • __INLINE_CODE_13__ - Custom function for escaping HTML

Tags

  • __INLINE_CODE_14__ 'Scriptlet' tag, for control-flow, no output
  • __INLINE_CODE_15__ Outputs the value into the template (HTML escaped)
  • __INLINE_CODE_16__ Outputs the unescaped value into the template
  • __INLINE_CODE_17__ Comment tag, no execution, no output
  • __INLINE_CODE_18__ Outputs a literal '<%'
  • __INLINE_CODE_19__ Plain ending tag
  • __INLINE_CODE_20__ Trim-mode ('newline slurp') tag, trims following newline

Includes

Includes are relatives to the template with the __INLINE_CODE_21__ call.

__CODE_BLOCK_5__

Customer Delimiters

Custom delimiters can be applied on a per-template basis, or globally:

__CODE_BLOCK_6__

Methods

  • pjs.renderFile(path [, data] [, opts], callback)
  • pjs.render(str [, data] [, opts], callback)
  • pjs.compile(str [, opts])
  • pjs.clearCache()
  • pjs.escape(html)
; pjs.render('<$= users.join(" | "); __CODE_BLOCK_6__gt;', { users: users }, function (err, html) { // html = 'geddy | neil | alex' });

Methods

  • pjs.renderFile(path [, data] [, opts], callback)
  • pjs.render(str [, data] [, opts], callback)
  • pjs.compile(str [, opts])
  • pjs.clearCache()
  • pjs.escape(html)
});

Example

Template __INLINE_CODE_1__:

__CODE_BLOCK_2__

Render the file:

__CODE_BLOCK_3__

The __INLINE_CODE_2__ method tell PJS that it's an async block and to wait until done() is called.

If your block is not asynchronous, you don't need to use it:

__CODE_BLOCK_4__

Will display __INLINE_CODE_3__

Options

  • __INLINE_CODE_4__ (boolean) - Compiled functions are cached, requires __INLINE_CODE_5__ option when used with the __INLINE_CODE_6__ method
  • __INLINE_CODE_7__ - Used by cache to key caches, and for includes
  • __INLINE_CODE_8__ (boolean) - Require __INLINE_CODE_9__, watch for changes on the cached files to clear their cache automatically
  • __INLINE_CODE_10__ - Output generated function body
  • __INLINE_CODE_11__ - When false no debug instrumentation is compiled
  • __INLINE_CODE_12__ - Character to use with angle brackets for open/close
  • __INLINE_CODE_13__ - Custom function for escaping HTML

Tags

  • __INLINE_CODE_14__ 'Scriptlet' tag, for control-flow, no output
  • __INLINE_CODE_15__ Outputs the value into the template (HTML escaped)
  • __INLINE_CODE_16__ Outputs the unescaped value into the template
  • __INLINE_CODE_17__ Comment tag, no execution, no output
  • __INLINE_CODE_18__ Outputs a literal '<%'
  • __INLINE_CODE_19__ Plain ending tag
  • __INLINE_CODE_20__ Trim-mode ('newline slurp') tag, trims following newline

Includes

Includes are relatives to the template with the __INLINE_CODE_21__ call.

__CODE_BLOCK_5__

Customer Delimiters

Custom delimiters can be applied on a per-template basis, or globally:

__CODE_BLOCK_6__

Methods

  • pjs.renderFile(path [, data] [, opts], callback)
  • pjs.render(str [, data] [, opts], callback)
  • pjs.compile(str [, opts])
  • pjs.clearCache()
  • pjs.escape(html)

Keywords