1.1.2 • Published 6 years ago

subcode-view v1.1.2

Weekly downloads
1
License
MIT
Repository
gitlab
Last release
6 years ago

subcode-view

View system that uses subcode templates with express

Installation

npm install subcode-view

Usage

const view = require('subcode-view');

Compile views

A compiled view function is an express request handler function which takes an express request and a response object as arguments.

await view(options);
  • options <object> | <string> - An object with the following options or the entry option. + entry <string> - The filename of the template to compile. + basedir <string> - An optional directory for resolving relative entry paths. + minify <boolean> - True to minify rendered html before sending to the client. + error(err, req, res) <function> - A function that is called if a render error occurs. By default the view will log the error and respond with a 500 Internal Server Error. + locals(obj) <function> - A function which is called for every request to extend the locals object passed to the template before rendering. + syntax, encoding, extend, cache - The same options passed to the subcode compile function.

Note that the compiled template will always be async!

Template locals

The following are properties of the locals object passed to templates and the locals option:

  • req - The express request object.
  • res - The express response object.
  • cancel((req, res) => { ... }) - A function that throws to cancel the current render process and uses the specified handler to send an alternative response.

Apply default options

Using default options can be helpful when compiling many views with nearly the same options. The using function returns the same api as exposed by the view module directly and is chainable.

const view = require('subcode-view').using(defaults);
  • defaults <object> - An object with view options listed above. Options specified here can still be overwritten.

Default locals

When multiple locals functions are specified, they are merged together so that defaults will be overwritten.

const view = require('subcode-view').using({
	locals(obj) {
		obj.a = 'a';
		obj.b = 'b';
	}
});

await view({
	locals(obj) {
		obj.b = 42;
	}
});

// Locals passed to the template will be:
{
	a: 'a',
	b: 42,
	res, req, cancel
}

Compile and attach views

The attach and attachAll functions compile views and attach them to an express route or application.

await view.attach(route, mount, middleware, options);
// Example:
await view.attach(route, '/', {entry: 'index.html'});

await view.attachAll(route, views)
// Example:
await view.attachAll(route, [
	['/', {entry: 'index.html'}],
	['/foo', {entry: 'bar.html'}]
]);
  • route - An express router or application.
  • mount <string> - The mount path to attch the view after compiling.
  • middleware <array> - An optional array of express middleware.
  • options <object> | <string> - An object with view compile options or the entry option.
  • views <iterable> - An iterable of arrays which contain the mount, middleware and options arguments.
1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago