Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
templates
Advanced tools
System for creating and managing template collections, and rendering templates with any node.js template engine. Can be used as the basis for creating a static site generator or blog framework.
System for creating and managing template collections, and rendering templates with any node.js template engine. Can be used as the basis for creating a static site generator or blog framework.
(TOC generated by verb using markdown-toc)
Install with npm:
$ npm install templates --save
Features
app.create('foo')
Example
This is just a very small glimpse at the templates
API!
var templates = require('templates');
var app = templates();
// create a collection
app.create('pages');
// add views to the collection
app.page('a.html', {content: 'this is <%= foo %>'});
app.page('b.html', {content: 'this is <%= bar %>'});
app.page('c.html', {content: 'this is <%= baz %>'});
app.pages.getView('a.html')
.render({foo: 'home'}, function (err, view) {
//=> 'this is home'
});
var templates = require('templates');
var app = templates();
This section describes API features that are shared by all Templates classes.
Set or get an option value.
Params
key
{String|Object}: Pass a key-value pair or an object to set.val
{any}: Any value when a key-value pair is passed. This can also be options if a glob pattern is passed as the first value.returns
{Object}: Returns the instance for chaining.Example
app.option('a', 'b');
app.option({c: 'd'});
console.log(app.options);
//=> {a: 'b', c: 'd'}
Run a plugin on the given instance. Plugins are invoked immediately upon instantiating in the order in which they were defined.
Example
The simplest plugin looks something like the following:
app.use(function(inst) {
// do something to `inst`
});
Note that inst
is the instance of the class you're instantiating. So if you create an instance of Collection
, inst is the collection instance.
Params
fn
{Function}: Plugin function. If the plugin returns a function it will be passed to the use
method of each item created on the instance.returns
{Object}: Returns the instance for chaining.Usage
collection.use(function(items) {
// `items` is the instance, as is `this`
// optionally return a function to be passed to
// the `.use` method of each item created on the
// instance
return function(item) {
// do stuff to each `item`
};
});
API for the main Templates
class.
This function is the main export of the templates module. Initialize an instance of templates
to create your application.
Params
options
{Object}Example
var templates = require('templates');
var app = templates();
Create a new list. See the list docs for more information about lists.
Params
opts
{Object}: List optionsreturns
{Object}: Returns the list
instance for chaining.Example
var list = app.list();
list.addItem('abc', {content: '...'});
// or, create list from a collection
app.create('pages');
var list = app.list(app.pages);
Create a new collection. Collections are decorated with special methods for getting and setting items from the collection. Note that, unlike the create method, collections created with .collection()
are not cached.
See the collection docs for more information about collections.
Params
opts
{Object}: Collection optionsreturns
{Object}: Returns the collection
instance for chaining.Create a new view collection to be stored on the app.views
object. See
the create docs for more details.
Params
name
{String}: The name of the collection to create. Plural or singular form may be used, as the inflections are automatically resolved when the collection is created.opts
{Object}: Collection optionsreturns
{Object}: Returns the collection
instance for chaining.Expose static setup
method for providing access to an instance before any other code is run.
Params
app
{Object}: Application instancename
{String}: Optionally pass the constructor name to use.returns
{undefined}Example
function App(options) {
Templates.call(this, options);
Templates.setup(this);
}
Templates.extend(App);
Register a view engine callback fn
as ext
.
Params
exts
{String|Array}: String or array of file extensions.fn
{Function|Object}: or settings
settings
{Object}: Optionally pass engine options as the last argument.Example
app.engine('hbs', require('engine-handlebars'));
// using consolidate.js
var engine = require('consolidate');
app.engine('jade', engine.jade);
app.engine('swig', engine.swig);
// get a registered engine
var swig = app.engine('swig');
Get registered engine ext
.
Params
ext
{String}: The engine to get.Example
app.engine('hbs', require('engine-handlebars'));
var engine = app.getEngine('hbs');
Register a template helper.
Params
name
{String}: Helper namefn
{Function}: Helper function.Example
app.helper('upper', function(str) {
return str.toUpperCase();
});
Register multiple template helpers.
Params
helpers
{Object|Array}: Object, array of objects, or glob patterns.Example
app.helpers({
foo: function() {},
bar: function() {},
baz: function() {}
});
Get a previously registered helper.
Params
name
{String}: Helper namereturns
{Function}: Returns the registered helper function.Example
var fn = app.getHelper('foo');
Get a previously registered async helper.
Params
name
{String}: Helper namereturns
{Function}: Returns the registered helper function.Example
var fn = app.getAsyncHelper('foo');
Return true if sync helper name
is registered.
Params
name
{String}: sync helper namereturns
{Boolean}: Returns true if the sync helper is registeredExample
if (app.hasHelper('foo')) {
// do stuff
}
Return true if async helper name
is registered.
Params
name
{String}: Async helper namereturns
{Boolean}: Returns true if the async helper is registeredExample
if (app.hasAsyncHelper('foo')) {
// do stuff
}
Register an async helper.
Params
name
{String}: Helper name.fn
{Function}: Helper functionExample
app.asyncHelper('upper', function(str, next) {
next(null, str.toUpperCase());
});
Register multiple async template helpers.
Params
helpers
{Object|Array}: Object, array of objects, or glob patterns.Example
app.asyncHelpers({
foo: function() {},
bar: function() {},
baz: function() {}
});
Register a namespaced helper group.
Params
helpers
{Object|Array}: Object, array of objects, or glob patterns.Example
// markdown-utils
app.helperGroup('mdu', {
foo: function() {},
bar: function() {},
});
// Usage:
// <%= mdu.foo() %>
// <%= mdu.bar() %>
API for the View
class.
Create an instance of View
. Optionally pass a default object to use.
Params
view
{Object}Example
var view = new View({
path: 'foo.html',
content: '...'
});
Creates a context object from front-matter data, view.locals
and the given locals
object.
Params
locals
{Object}: Optionally pass locals to the engine.returns
{Object}: Returns the context object.Example
var ctx = page.context({foo: 'bar'});
Synchronously compile a view.
Params
locals
{Object}: Optionally pass locals to the engine.returns
{Object} View
: instance, for chaining.Example
var view = page.compile();
view.fn({title: 'A'});
view.fn({title: 'B'});
view.fn({title: 'C'});
Asynchronously render a view.
Params
locals
{Object}: Optionally pass locals to the engine.returns
{Object} View
: instance, for chaining.Example
view.render({title: 'Home'}, function(err, res) {
//=> view object with rendered `content`
});
Return true if the view is the given view type
. Since types are assigned by collections, views that are "collection-less" will not have a type, and thus will always return false
(as expected).
Params
type
{String}: (renderable
, partial
, layout
)Example
view.isType('partial');
Set, get and load data to be passed to templates as context at render-time.
Params
key
{String|Object}: Pass a key-value pair or an object to set.val
{any}: Any value when a key-value pair is passed. This can also be options if a glob pattern is passed as the first value.returns
{Object}: Returns an instance of Templates
for chaining.Example
app.data('a', 'b');
app.data({c: 'd'});
console.log(app.cache.data);
//=> {a: 'b', c: 'd'}
Build the context for the given view
and locals
.
Params
view
{Object}: The view being renderedlocals
{Object}returns
{Object}: The object to be passed to engines/views as context.Update context in a helper so that this.helper.options
is
the options for that specific helper.
Params
context
{Object}key
{String}Merge "partials" view types. This is necessary for template engines have no support for partials or only support one type of partials.
Params
options
{Object}: Optionally pass an array of viewTypes
to include on options.viewTypes
returns
{Object}: Merged partialsMerge "partials" view types. This is necessary for template engines have no support for partials or only support one type of partials.
Params
options
{Object}: Optionally pass an array of viewTypes
to include on options.viewTypes
callback
{Function}: Function that exposes err
and partials
parametersAPI for the Item
class.
Create an instance of Item
. Optionally pass a default object to use.
Params
item
{Object}Example
var item = new Item({
path: 'foo.html',
content: '...'
});
Re-decorate Item methods after calling vinyl's .clone()
method.
Params
options
{Object}returns
{Object} item
: Cloned instanceExample
item.clone({deep: true}); // false by default
Set, get and load data to be passed to templates as context at render-time.
Params
key
{String|Object}: Pass a key-value pair or an object to set.val
{any}: Any value when a key-value pair is passed. This can also be options if a glob pattern is passed as the first value.returns
{Object}: Returns an instance of Templates
for chaining.Example
app.data('a', 'b');
app.data({c: 'd'});
console.log(app.cache.data);
//=> {a: 'b', c: 'd'}
Build the context for the given view
and locals
.
Params
view
{Object}: The view being renderedlocals
{Object}returns
{Object}: The object to be passed to engines/views as context.Update context in a helper so that this.helper.options
is
the options for that specific helper.
Params
context
{Object}key
{String}Merge "partials" view types. This is necessary for template engines have no support for partials or only support one type of partials.
Params
options
{Object}: Optionally pass an array of viewTypes
to include on options.viewTypes
returns
{Object}: Merged partialsMerge "partials" view types. This is necessary for template engines have no support for partials or only support one type of partials.
Params
options
{Object}: Optionally pass an array of viewTypes
to include on options.viewTypes
callback
{Function}: Function that exposes err
and partials
parametersAPI for the Views
class.
Create an instance of Views
with the given options
.
Params
options
{Object}Example
var collection = new Views();
collection.addView('foo', {content: 'bar'});
Set a view on the collection. This is identical to addView except setView
does not emit an event for each view.
Params
key
{String|Object}: View key or objectvalue
{Object}: If key is a string, value is the view object.returns
{Object}: returns the view
instance.Example
collection.setView('foo', {content: 'bar'});
Similar to setView, adds a view to the collection but also fires an event and iterates over the loading queue
for loading views from the addView
event listener. If the given view is not already an instance of View
, it will be converted to one before being added to the views
object.
Params
key
{String}value
{Object}returns
{Object}: Returns the instance of the created View
to allow chaining view methods.Example
var views = new Views(...);
views.addView('a.html', {path: 'a.html', contents: '...'});
Delete a view from collection views
.
Params
key
{String}returns
{Object}: Returns the instance for chainingExample
views.deleteView('foo.html');
Load multiple views onto the collection.
Params
views
{Object|Array}returns
{Object}: returns the collection
objectExample
collection.addViews({
'a.html': {content: '...'},
'b.html': {content: '...'},
'c.html': {content: '...'}
});
Load an array of views onto the collection.
Params
list
{Array}returns
{Object}: returns the views
instanceExample
collection.addList([
{path: 'a.html', content: '...'},
{path: 'b.html', content: '...'},
{path: 'c.html', content: '...'}
]);
Group all collection views
by the given property, properties or compare functions. See group-array for the full range of available features and options.
returns
{Object}: Returns an object of grouped views.Example
var collection = new Collection();
collection.addViews(...);
var groups = collection.groupBy('data.date', 'data.slug');
Get view name
from collection.views
.
Params
key
{String}: Key of the view to get.fn
{Function}: Optionally pass a function to modify the key.returns
{Object}Example
collection.getView('a.html');
Load a view from the file system.
Params
view
{Object}returns
{Object}Example
collection.loadView(view);
Return true if the collection belongs to the given view type
.
Params
type
{String}: (renderable
, partial
, layout
)Example
collection.isType('partial');
Alias for viewType
Set, get and load data to be passed to templates as context at render-time.
Params
key
{String|Object}: Pass a key-value pair or an object to set.val
{any}: Any value when a key-value pair is passed. This can also be options if a glob pattern is passed as the first value.returns
{Object}: Returns an instance of Templates
for chaining.Example
app.data('a', 'b');
app.data({c: 'd'});
console.log(app.cache.data);
//=> {a: 'b', c: 'd'}
Build the context for the given view
and locals
.
Params
view
{Object}: The view being renderedlocals
{Object}returns
{Object}: The object to be passed to engines/views as context.Update context in a helper so that this.helper.options
is
the options for that specific helper.
Params
context
{Object}key
{String}Merge "partials" view types. This is necessary for template engines have no support for partials or only support one type of partials.
Params
options
{Object}: Optionally pass an array of viewTypes
to include on options.viewTypes
returns
{Object}: Merged partialsMerge "partials" view types. This is necessary for template engines have no support for partials or only support one type of partials.
Params
options
{Object}: Optionally pass an array of viewTypes
to include on options.viewTypes
callback
{Function}: Function that exposes err
and partials
parametersFind a view by name
, optionally passing a collection
to limit the search. If no collection is passed all renderable
collections will be searched.
Params
name
{String}: The name/key of the view to findcolleciton
{String}: Optionally pass a collection name (e.g. pages)returns
{Object|undefined}: Returns the view if found, or undefined
if not.Example
var page = app.find('my-page.hbs');
// optionally pass a collection name as the second argument
var page = app.find('my-page.hbs', 'pages');
Get view key
from the specified collection
.
Params
collection
{String}: Collection name, e.g. pages
key
{String}: Template namefn
{Function}: Optionally pass a renameKey
functionreturns
{Object}Example
var view = app.getView('pages', 'a/b/c.hbs');
// optionally pass a `renameKey` function to modify the lookup
var view = app.getView('pages', 'a/b/c.hbs', function(fp) {
return path.basename(fp);
});
Get all views from a collection
using the collection's singular or plural name.
Params
name
{String}: The collection name, e.g. pages
or page
returns
{Object}Example
var pages = app.getViews('pages');
//=> { pages: {'home.hbs': { ... }}
var posts = app.getViews('posts');
//=> { posts: {'2015-10-10.md': { ... }}
API for the Collections
class.
Create an instance of Collection
with the given options
.
Params
options
{Object}Example
var collection = new Collection();
collection.addItem('foo', {content: 'bar'});
Set an item on the collection. This is identical to addItem except setItem
does not emit an event for each item and does not iterate over the item queue
.
Params
key
{String|Object}: Item key or objectvalue
{Object}: If key is a string, value is the item object.returns
{Object}: returns the item
instance.Example
collection.setItem('foo', {content: 'bar'});
Similar to setItem
, adds an item to the collection but also fires an event and iterates over the item queue
to load items from the addItem
event listener. An item may be an instance of Item
, if not, the item is converted to an instance of Item
.
Params
key
{String}value
{Object}Example
var list = new List(...);
list.addItem('a.html', {path: 'a.html', contents: '...'});
Delete an item from collection items
.
Params
key
{String}returns
{Object}: Returns the instance for chainingExample
items.deleteItem('abc');
Load multiple items onto the collection.
Params
items
{Object|Array}returns
{Object}: returns the instance for chainingExample
collection.addItems({
'a.html': {content: '...'},
'b.html': {content: '...'},
'c.html': {content: '...'}
});
Load an array of items onto the collection.
Params
items
{Array}: or an instance of List
fn
{Function}: Optional sync callback function that is called on each item.returns
{Object}: returns the Collection instance for chainingExample
collection.addList([
{path: 'a.html', content: '...'},
{path: 'b.html', content: '...'},
{path: 'c.html', content: '...'}
]);
Get an item from the collection.
Params
key
{String}: Key of the item to get.returns
{Object}Example
collection.getItem('a.html');
Set, get and load data to be passed to templates as context at render-time.
Params
key
{String|Object}: Pass a key-value pair or an object to set.val
{any}: Any value when a key-value pair is passed. This can also be options if a glob pattern is passed as the first value.returns
{Object}: Returns an instance of Templates
for chaining.Example
app.data('a', 'b');
app.data({c: 'd'});
console.log(app.cache.data);
//=> {a: 'b', c: 'd'}
Build the context for the given view
and locals
.
Params
view
{Object}: The view being renderedlocals
{Object}returns
{Object}: The object to be passed to engines/views as context.Update context in a helper so that this.helper.options
is
the options for that specific helper.
Params
context
{Object}key
{String}Merge "partials" view types. This is necessary for template engines have no support for partials or only support one type of partials.
Params
options
{Object}: Optionally pass an array of viewTypes
to include on options.viewTypes
returns
{Object}: Merged partialsMerge "partials" view types. This is necessary for template engines have no support for partials or only support one type of partials.
Params
options
{Object}: Optionally pass an array of viewTypes
to include on options.viewTypes
callback
{Function}: Function that exposes err
and partials
parametersAPI for the List
class.
Create an instance of List
with the given options
. Lists differ from collections in that items are stored as an array, allowing items to be paginated, sorted, and grouped.
Params
options
{Object}Example
var list = new List();
list.addItem('foo', {content: 'bar'});
Set an item on the collection. This is identical to addItem except setItem
does not emit an event for each item and does not iterate over the item queue
.
Params
key
{String|Object}: Item key or objectvalue
{Object}: If key is a string, value is the item object.returns
{Object}: returns the item
instance.Example
collection.setItem('foo', {content: 'bar'});
Similar to setItem, adds an item to the list but also fires an event and iterates over the item queue
to load items from the addItem
event listener. If the given item is not already an instance of Item
, it will be converted to one before being added to the items
object.
Params
key
{String}value
{Object}returns
{Object}: Returns the instance of the created Item
to allow chaining item methods.Example
var items = new Items(...);
items.addItem('a.html', {path: 'a.html', contents: '...'});
Load multiple items onto the collection.
Params
items
{Object|Array}returns
{Object}: returns the instance for chainingExample
collection.addItems({
'a.html': {content: '...'},
'b.html': {content: '...'},
'c.html': {content: '...'}
});
Load an array of items or the items from another instance of List
.
Params
items
{Array}: or an instance of List
fn
{Function}: Optional sync callback function that is called on each item.returns
{Object}: returns the List instance for chainingExample
var foo = new List(...);
var bar = new List(...);
bar.addList(foo);
Return true if the list has the given item (name).
Params
key
{String}returns
{Object}Example
list.addItem('foo.html', {content: '...'});
list.hasItem('foo.html');
//=> true
Get a the index of a specific item from the list by key
.
Params
key
{String}returns
{Object}Example
list.getIndex('foo.html');
//=> 1
Get a specific item from the list by key
.
Params
key
{String}: The item name/key.returns
{Object}Example
list.getItem('foo.html');
//=> '<Item <foo.html>>'
Proxy for getItem
Params
key
{String}: Pass the key of the item
to get.returns
{Object}Example
list.getItem('foo.html');
//=> '<Item "foo.html" <buffer e2 e2 e2>>'
Remove an item from the list.
Params
key
{Object|String}: Pass an item
instance (object) or item.key
(string).Example
list.deleteItem('a.html');
Decorate each item on the list with additional methods and properties. This provides a way of easily overriding defaults.
Params
item
{Object}returns
{Object}: Instance of item for chainingGroup all list items
using the given property, properties or compare functions. See group-array for the full range of available features and options.
returns
{Object}: Returns the grouped items.Example
var list = new List();
list.addItems(...);
var groups = list.groupBy('data.date', 'data.slug');
Sort all list items
using the given property, properties or compare functions. See array-sort for the full range of available features and options.
returns
{Object}: Returns a new List
instance with sorted items.Example
var list = new List();
list.addItems(...);
var result = list.sortBy('data.date');
//=> new sorted list
Paginate all items
in the list with the given options, See paginationator for the full range of available features and options.
returns
{Object}: Returns the paginated items.Example
var list = new List(items);
var pages = list.paginate({limit: 5});
Set, get and load data to be passed to templates as context at render-time.
Params
key
{String|Object}: Pass a key-value pair or an object to set.val
{any}: Any value when a key-value pair is passed. This can also be options if a glob pattern is passed as the first value.returns
{Object}: Returns an instance of Templates
for chaining.Example
app.data('a', 'b');
app.data({c: 'd'});
console.log(app.cache.data);
//=> {a: 'b', c: 'd'}
Build the context for the given view
and locals
.
Params
view
{Object}: The view being renderedlocals
{Object}returns
{Object}: The object to be passed to engines/views as context.Update context in a helper so that this.helper.options
is
the options for that specific helper.
Params
context
{Object}key
{String}Merge "partials" view types. This is necessary for template engines have no support for partials or only support one type of partials.
Params
options
{Object}: Optionally pass an array of viewTypes
to include on options.viewTypes
returns
{Object}: Merged partialsMerge "partials" view types. This is necessary for template engines have no support for partials or only support one type of partials.
Params
options
{Object}: Optionally pass an array of viewTypes
to include on options.viewTypes
callback
{Function}: Function that exposes err
and partials
parametersAPI for the Group
class.
Create an instance of Group
with the given options
.
Params
options
{Object}Example
var group = new Group({
'foo': { items: [1,2,3] }
});
Find a view by name
, optionally passing a collection
to limit the search. If no collection is passed all renderable
collections will be searched.
Params
name
{String}: The name/key of the view to findcolleciton
{String}: Optionally pass a collection name (e.g. pages)returns
{Object|undefined}: Returns the view if found, or undefined
if not.Example
var page = app.find('my-page.hbs');
// optionally pass a collection name as the second argument
var page = app.find('my-page.hbs', 'pages');
Get view key
from the specified collection
.
Params
collection
{String}: Collection name, e.g. pages
key
{String}: Template namefn
{Function}: Optionally pass a renameKey
functionreturns
{Object}Example
var view = app.getView('pages', 'a/b/c.hbs');
// optionally pass a `renameKey` function to modify the lookup
var view = app.getView('pages', 'a/b/c.hbs', function(fp) {
return path.basename(fp);
});
Get all views from a collection
using the collection's singular or plural name.
Params
name
{String}: The collection name, e.g. pages
or page
returns
{Object}Example
var pages = app.getViews('pages');
//=> { pages: {'home.hbs': { ... }}
var posts = app.getViews('posts');
//=> { posts: {'2015-10-10.md': { ... }}
Compile content
with the given locals
.
Params
view
{Object|String}: View object.locals
{Object}isAsync
{Boolean}: Load async helpersreturns
{Object}: View object with compiled view.fn
property.Example
var indexPage = app.page('some-index-page.hbs');
var view = app.compile(indexPage);
// view.fn => [function]
// you can call the compiled function more than once
// to render the view with different data
view.fn({title: 'Foo'});
view.fn({title: 'Bar'});
view.fn({title: 'Baz'});
Asynchronously compile content
with the given locals
and callback. (fwiw, this method name uses the unconventional "*Async" nomenclature to allow us to preserve the synchronous behavior of the view.compile
method as well as the name).
Params
view
{Object|String}: View object.locals
{Object}isAsync
{Boolean}: Pass true to load helpers as async (mostly used internally)callback
{Function}: function that exposes err
and the view
object with compiled view.fn
propertyExample
var indexPage = app.page('some-index-page.hbs');
app.compileAsync(indexPage, function(err, view) {
// view.fn => compiled function
});
Render a view with the given locals
and callback
.
Params
view
{Object|String}: Instance of View
locals
{Object}: Locals to pass to template engine.callback
{Function}Example
var blogPost = app.post.getView('2015-09-01-foo-bar');
app.render(blogPost, {title: 'Foo'}, function(err, view) {
// `view` is an object with a rendered `content` property
});
Set, get and load data to be passed to templates as context at render-time.
Params
key
{String|Object}: Pass a key-value pair or an object to set.val
{any}: Any value when a key-value pair is passed. This can also be options if a glob pattern is passed as the first value.returns
{Object}: Returns an instance of Templates
for chaining.Example
app.data('a', 'b');
app.data({c: 'd'});
console.log(app.cache.data);
//=> {a: 'b', c: 'd'}
Build the context for the given view
and locals
.
Params
view
{Object}: The view being renderedlocals
{Object}returns
{Object}: The object to be passed to engines/views as context.Update context in a helper so that this.helper.options
is
the options for that specific helper.
Params
context
{Object}key
{String}Merge "partials" view types. This is necessary for template engines have no support for partials or only support one type of partials.
Params
options
{Object}: Optionally pass an array of viewTypes
to include on options.viewTypes
returns
{Object}: Merged partialsMerge "partials" view types. This is necessary for template engines have no support for partials or only support one type of partials.
Params
options
{Object}: Optionally pass an array of viewTypes
to include on options.viewTypes
callback
{Function}: Function that exposes err
and partials
parametersHandle a middleware method
for view
.
Params
method
{String}: Name of the router method to handle. See router methodsview
{Object}: View objectcallback
{Function}: Callback functionreturns
{Object}Example
app.handle('customMethod', view, callback);
Deprecated, use .handleOnce
Create a new Route for the given path. Each route contains a separate middleware stack.
See the [route API documentation][route-api] for details on adding handlers and middleware to routes.
Params
path
{String}returns
{Object} Route
: for chainingExample
app.create('posts');
app.route(/blog/)
.all(function(view, next) {
// do something with view
next();
});
app.post('whatever', {path: 'blog/foo.bar', content: 'bar baz'});
Special route method that works just like the router.METHOD()
methods, except that it matches all verbs.
Params
path
{String}callback
{Function}returns
{Object} this
: for chainingExample
app.all(/\.hbs$/, function(view, next) {
// do stuff to view
next();
});
Add callback triggers to route parameters, where name
is the name of the parameter and fn
is the callback function.
Params
name
{String}fn
{Function}returns
{Object}: Returns the instance of Templates
for chaining.Example
app.param('title', function(view, next, title) {
//=> title === 'foo.js'
next();
});
app.onLoad('/blog/:title', function(view, next) {
//=> view.path === '/blog/foo.js'
next();
});
Static method that returns true if the given value is a templates
instance (App
).
Params
val
{Object}: The value to test.returns
{Boolean}Example
var templates = require('templates');
var app = templates();
templates.isApp(templates);
//=> false
templates.isApp(app);
//=> true
Static method that returns true if the given value is a templates Collection
instance.
Params
val
{Object}: The value to test.returns
{Boolean}Example
var templates = require('templates');
var app = templates();
app.create('pages');
templates.isCollection(app.pages);
//=> true
Static method that returns true if the given value is a templates Views
instance.
Params
val
{Object}: The value to test.returns
{Boolean}Example
var templates = require('templates');
var app = templates();
app.create('pages');
templates.isViews(app.pages);
//=> true
Static method that returns true if the given value is a templates List
instance.
Params
val
{Object}: The value to test.returns
{Boolean}Example
var templates = require('templates');
var List = templates.List;
var app = templates();
var list = new List();
templates.isList(list);
//=> true
Static method that returns true if the given value is a templates Group
instance.
Params
val
{Object}: The value to test.returns
{Boolean}Example
var templates = require('templates');
var Group = templates.Group;
var app = templates();
var group = new Group();
templates.isGroup(group);
//=> true
Static method that returns true if the given value is a templates View
instance.
Params
val
{Object}: The value to test.returns
{Boolean}Example
var templates = require('templates');
var app = templates();
templates.isView('foo');
//=> false
var view = app.view('foo', {content: '...'});
templates.isView(view);
//=> true
Static method that returns true if the given value is a templates Item
instance.
Params
val
{Object}: The value to test.returns
{Boolean}Example
var templates = require('templates');
var app = templates();
templates.isItem('foo');
//=> false
var view = app.view('foo', {content: '...'});
templates.isItem(view);
//=> true
Static method that returns true if the given value is a vinyl File
instance.
Params
val
{Object}: The value to test.returns
{Boolean}Example
var File = require('vinyl');
var templates = require('templates');
var app = templates();
var view = app.view('foo', {content: '...'});
templates.isVinyl(view);
//=> true
var file = new File({path: 'foo', contents: new Buffer('...')});
templates.isVinyl(file);
//=> true
Context
Helpers
.ctx()
method on helper context, to simplify merging context in non-built-in helpersEngines
debug
methods and related codepartial
, renderable
and layout
).removeItem
method that was deprecated in v0.10.7 from List
.handleView
is deprecated in favor of .handleOnce
and will be removed in a future version. Start using .handleOnce
now.Templates.setup()
method for initializing any setup code that should have access to the instance before any other use code is run.app.option.set
, app.option.get
and app.option.merge
Although 99% of users won't be effected by the changes in this release, there were some potentially breaking changes.
render
and compile
methods were streamlined, making it clear that .mergePartials
should not have been renamed to mergePartialsSync
. So that change was reverted.this.helper
object to the context in helpers, which has the helper name and options that were set specifically for that helperthis.view
object to the context in helpers, which is the current view being rendered. This was (and still is) always expose on this.context.view
, but it makes sense to add this to the root of the context as a convenience. We will deprecate this.context.view
in a future version..get
, .set
and .merge
methods on this.options
, this.context
and the this
object in helpers..compileSync
, we felt that it made more sense to add .compileAsync
, since .compile
is a public method and most users will expect it to be sync, and .compile
methods with most engines are typically sync. In other words, .compileAsync
probably won't be seen by most users, but we wanted to explain the decision to go against node.js naming conventions.engine
can now be defined on app
or a collection using using app.option('engine')
, views.option('engine')
layout
can now defined using app.option('layout')
, views.option('layout')
. No changes have been made to view.layout
, it should work as before. Resolves issue/#818view
helper has been refactored completely. The helper is now async and renders the view before returning its content.isApp
, isViews
, isCollection
, isList
, isView
, isGroup
, and isItem
static methods. All return true when the given value is an instance of the respective class.deleteItem
method to List and Collection, and deleteView
method to Views._.proto
property which is only exposed for unit tests was renamed to _.plugin
.isRegistered
feature.getView
, now that the method has been refactored to be faster.getView
method no longer automatically reads views from the file system. This was undocumented before and, but it's a breaking change nonetheless. The removed functionality can easily be done in a plugin.renderable
view is rendered (e.g. page)app.context
method. It now only accepts two arguments, view
and locals
, since ctx
(the parameter that was removed) was technically being merged in twice.isType
method on view
. Shouldn't be any breaking changes..error
method to .formatError
mergeContext
optionview
and item
as the second argumentisType
method for checking the viewType
on a collectiondefault
layout was automatically applied to partials, causing an infinite loop in rare cases.You might also be interested in these projects:
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Generate readme and API documentation with verb:
$ npm install verb && npm run docs
Or, if verb is installed globally:
$ verb
Install dev dependencies:
$ npm install -d && npm test
Jon Schlinkert
Copyright © 2016, Jon Schlinkert. Released under the MIT license.
This file was generated by verb, v0.9.0, on May 27, 2016.
FAQs
System for creating and managing template collections, and rendering templates with any node.js template engine. Can be used as the basis for creating a static site generator or blog framework.
The npm package templates receives a total of 30,727 weekly downloads. As such, templates popularity was classified as popular.
We found that templates demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.