UNPKG

56.3 kBMarkdownView Raw
1# templates [![NPM version](https://img.shields.io/npm/v/templates.svg?style=flat)](https://www.npmjs.com/package/templates) [![NPM downloads](https://img.shields.io/npm/dm/templates.svg?style=flat)](https://npmjs.org/package/templates) [![Build Status](https://img.shields.io/travis/jonschlinkert/templates.svg?style=flat)](https://travis-ci.org/jonschlinkert/templates)
2
3System 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.
4
5## TOC
6
7- [Install](#install)
8- [Usage](#usage)
9- [API](#api)
10 * [Common](#common)
11 + [.option](#option)
12 + [.use](#use)
13 * [App](#app)
14 * [Engines](#engines)
15 * [Helpers](#helpers)
16 * [Built-in helpers](#built-in-helpers)
17 * [View](#view)
18 + [View Data](#view-data)
19 * [Item](#item)
20 + [Item Data](#item-data)
21 * [Views](#views)
22 + [Views Data](#views-data)
23 + [Lookup methods](#lookup-methods)
24 * [Collections](#collections)
25 * [List](#list)
26 * [Group](#group)
27 * [Lookups](#lookups)
28 * [Rendering](#rendering)
29 * [Context](#context)
30 * [Routes and middleware](#routes-and-middleware)
31 * [is](#is)
32- [History](#history)
33- [Related projects](#related-projects)
34- [Contributing](#contributing)
35- [Building docs](#building-docs)
36- [Running tests](#running-tests)
37- [Author](#author)
38- [License](#license)
39
40_(TOC generated by [verb](https://github.com/verbose/verb) using [markdown-toc](https://github.com/jonschlinkert/markdown-toc))_
41
42## Install
43
44Install with [npm](https://www.npmjs.com/):
45
46```sh
47$ npm install --save templates
48```
49
50**Features**
51
52* create custom [view collections](#collections)
53* register any [template engine](#engine) for rendering views
54* register [helpers](#helper)
55* partials
56* layouts
57* plugins
58* middleware
59
60**Example**
61
62This is just a very small glimpse at the `templates` API!
63
64```js
65var templates = require('templates');
66var app = templates();
67
68// create a collection
69app.create('pages');
70
71// add views to the collection
72app.page('a.html', {content: 'this is <%= foo %>'});
73app.page('b.html', {content: 'this is <%= bar %>'});
74app.page('c.html', {content: 'this is <%= baz %>'});
75
76app.pages.getView('a.html')
77 .render({foo: 'home'}, function (err, view) {
78 //=> 'this is home'
79 });
80```
81
82## Usage
83
84```js
85var templates = require('templates');
86var app = templates();
87```
88
89## API
90
91### Common
92
93This section describes API features that are shared by all Templates classes.
94
95#### .option
96
97Set or get an option value.
98
99**Params**
100
101* `key` **{String|Object}**: Pass a key-value pair or an object to set.
102* `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.
103* `returns` **{Object}**: Returns the instance for chaining.
104
105**Example**
106
107```js
108app.option('a', 'b');
109app.option({c: 'd'});
110console.log(app.options);
111//=> {a: 'b', c: 'd'}
112```
113
114#### .use
115
116Run a plugin on the given instance. Plugins are invoked immediately upon instantiating in the order in which they were defined.
117
118**Example**
119
120The simplest plugin looks something like the following:
121
122```js
123app.use(function(inst) {
124 // do something to `inst`
125});
126```
127
128Note that `inst` is the instance of the class you're instantiating. So if you create an instance of `Collection`, inst is the collection instance.
129
130**Params**
131
132* `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.
133* `returns` **{Object}**: Returns the instance for chaining.
134
135**Usage**
136
137```js
138collection.use(function(items) {
139 // `items` is the instance, as is `this`
140
141 // optionally return a function to be passed to
142 // the `.use` method of each item created on the
143 // instance
144 return function(item) {
145 // do stuff to each `item`
146 };
147});
148```
149
150### App
151
152The `Templates` class is the main export of the `templates` library. All of the other classes are exposed as static properties on `Templates`:
153
154* [Item](#Item): Collection item, powered by [vinyl-item][].
155* [View](#View): Collection item, powered by [vinyl-view][].
156* [List](#List)
157* [Views](#Views):
158* [Collection](#Collection): Base collections class. Use this if you need to customize the render cycle, middleware stages, and so on.
159* [Group](#Group)
160
161### [Templates](index.js#L36)
162
163This function is the main export of the templates module. Initialize an instance of `templates` to create your application.
164
165**Params**
166
167* `options` **{Object}**
168
169**Example**
170
171```js
172var templates = require('templates');
173var app = templates();
174```
175
176### [.list](index.js#L154)
177
178Create a new list. See the [list docs](docs/lists.md) for more information about lists.
179
180**Params**
181
182* `opts` **{Object}**: List options
183* `returns` **{Object}**: Returns the `list` instance for chaining.
184
185**Example**
186
187```js
188var list = app.list();
189list.addItem('abc', {content: '...'});
190
191// or, create list from a collection
192app.create('pages');
193var list = app.list(app.pages);
194```
195
196### [.collection](index.js#L193)
197
198Create a new collection. Collections are decorated with special methods for getting and setting items from the collection. Note that, unlike the [create](#create) method, collections created with `.collection()` are not cached.
199
200See the [collection docs](docs/collections.md) for more
201information about collections.
202
203**Params**
204
205* `opts` **{Object}**: Collection options
206* `returns` **{Object}**: Returns the `collection` instance for chaining.
207
208### [.create](index.js#L245)
209
210Create a new view collection to be stored on the `app.views` object. See
211the [create docs](docs/collections.md#create) for more details.
212
213**Params**
214
215* `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.
216* `opts` **{Object}**: Collection options
217* `returns` **{Object}**: Returns the `collection` instance for chaining.
218
219### [.setup](index.js#L375)
220
221Expose static `setup` method for providing access to an instance before any other code is run.
222
223**Params**
224
225* `app` **{Object}**: Application instance
226* `name` **{String}**: Optionally pass the constructor name to use.
227* `returns` **{undefined}**
228
229**Example**
230
231```js
232function App(options) {
233 Templates.call(this, options);
234 Templates.setup(this);
235}
236Templates.extend(App);
237```
238
239***
240
241### [.engine](node_modules/base-engines/index.js#L45)
242
243Register a view engine callback `fn` as `ext`. Calls `.setEngine` and `.getEngine` internally.
244
245**Params**
246
247* `exts` **{String|Array}**: String or array of file extensions.
248* `fn` **{Function|Object}**: or `settings`
249* `settings` **{Object}**: Optionally pass engine options as the last argument.
250
251**Example**
252
253```js
254app.engine('hbs', require('engine-handlebars'));
255
256// using consolidate.js
257var engine = require('consolidate');
258app.engine('jade', engine.jade);
259app.engine('swig', engine.swig);
260
261// get a registered engine
262var swig = app.engine('swig');
263```
264
265### [.setEngine](node_modules/base-engines/index.js#L74)
266
267Register engine `ext` with the given render `fn` and/or `settings`.
268
269**Params**
270
271* `ext` **{String}**: The engine to set.
272
273**Example**
274
275```js
276app.setEngine('hbs', require('engine-handlebars'), {
277 delims: ['<%', '%>']
278});
279```
280
281### [.getEngine](node_modules/base-engines/index.js#L97)
282
283Get registered engine `ext`.
284
285**Params**
286
287* `ext` **{String}**: The engine to get.
288
289**Example**
290
291```js
292app.engine('hbs', require('engine-handlebars'));
293var engine = app.getEngine('hbs');
294```
295
296***
297
298### [.helper](node_modules/base-helpers/index.js#L46)
299
300Register a template helper.
301
302**Params**
303
304* `name` **{String}**: Helper name
305* `fn` **{Function}**: Helper function.
306
307**Example**
308
309```js
310app.helper('upper', function(str) {
311 return str.toUpperCase();
312});
313```
314
315### [.helpers](node_modules/base-helpers/index.js#L67)
316
317Register multiple template helpers.
318
319**Params**
320
321* `helpers` **{Object|Array}**: Object, array of objects, or glob patterns.
322
323**Example**
324
325```js
326app.helpers({
327 foo: function() {},
328 bar: function() {},
329 baz: function() {}
330});
331```
332
333### [.asyncHelper](node_modules/base-helpers/index.js#L86)
334
335Register an async helper.
336
337**Params**
338
339* `name` **{String}**: Helper name.
340* `fn` **{Function}**: Helper function
341
342**Example**
343
344```js
345app.asyncHelper('upper', function(str, next) {
346 next(null, str.toUpperCase());
347});
348```
349
350### [.asyncHelpers](node_modules/base-helpers/index.js#L107)
351
352Register multiple async template helpers.
353
354**Params**
355
356* `helpers` **{Object|Array}**: Object, array of objects, or glob patterns.
357
358**Example**
359
360```js
361app.asyncHelpers({
362 foo: function() {},
363 bar: function() {},
364 baz: function() {}
365});
366```
367
368### [.getHelper](node_modules/base-helpers/index.js#L124)
369
370Get a previously registered helper.
371
372**Params**
373
374* `name` **{String}**: Helper name
375* `returns` **{Function}**: Returns the registered helper function.
376
377**Example**
378
379```js
380var fn = app.getHelper('foo');
381```
382
383### [.getAsyncHelper](node_modules/base-helpers/index.js#L141)
384
385Get a previously registered async helper.
386
387**Params**
388
389* `name` **{String}**: Helper name
390* `returns` **{Function}**: Returns the registered helper function.
391
392**Example**
393
394```js
395var fn = app.getAsyncHelper('foo');
396```
397
398### [.hasHelper](node_modules/base-helpers/index.js#L160)
399
400Return true if sync helper `name` is registered.
401
402**Params**
403
404* `name` **{String}**: sync helper name
405* `returns` **{Boolean}**: Returns true if the sync helper is registered
406
407**Example**
408
409```js
410if (app.hasHelper('foo')) {
411 // do stuff
412}
413```
414
415### [.hasAsyncHelper](node_modules/base-helpers/index.js#L178)
416
417Return true if async helper `name` is registered.
418
419**Params**
420
421* `name` **{String}**: Async helper name
422* `returns` **{Boolean}**: Returns true if the async helper is registered
423
424**Example**
425
426```js
427if (app.hasAsyncHelper('foo')) {
428 // do stuff
429}
430```
431
432### [.helperGroup](node_modules/base-helpers/index.js#L201)
433
434Register a namespaced helper group.
435
436**Params**
437
438* `helpers` **{Object|Array}**: Object, array of objects, or glob patterns.
439
440**Example**
441
442```js
443// markdown-utils
444app.helperGroup('mdu', {
445 foo: function() {},
446 bar: function() {},
447});
448
449// Usage:
450// <%= mdu.foo() %>
451// <%= mdu.bar() %>
452```
453
454### Built-in helpers
455
456***
457
458### View
459
460API for the `View` class.
461
462### [View](node_modules/vinyl-view/index.js#L26)
463
464Create an instance of `View`. Optionally pass a default object to use.
465
466**Params**
467
468* `view` **{Object}**
469
470**Example**
471
472```js
473var view = new View({
474 path: 'foo.html',
475 contents: new Buffer('...')
476});
477```
478
479### [.compile](node_modules/vinyl-view/index.js#L57)
480
481Synchronously compile a view.
482
483**Params**
484
485* `locals` **{Object}**: Optionally pass locals to the engine.
486* `returns` **{Object}** `View`: instance, for chaining.
487
488**Example**
489
490```js
491var view = page.compile();
492view.fn({title: 'A'});
493view.fn({title: 'B'});
494view.fn({title: 'C'});
495```
496
497### [.renderSync](node_modules/vinyl-view/index.js#L75)
498
499Synchronously render templates in `view.content`.
500
501**Params**
502
503* `locals` **{Object}**: Optionally pass locals to the engine.
504* `returns` **{Object}** `View`: instance, for chaining.
505
506**Example**
507
508```js
509var view = new View({content: 'This is <%= title %>'});
510view.renderSync({title: 'Home'});
511console.log(view.content);
512```
513
514### [.render](node_modules/vinyl-view/index.js#L101)
515
516Asynchronously render templates in `view.content`.
517
518**Params**
519
520* `locals` **{Object}**: Context to use for rendering templates.
521
522**Example**
523
524```js
525view.render({title: 'Home'}, function(err, res) {
526 //=> view object with rendered `content`
527});
528```
529
530### [.context](node_modules/vinyl-view/index.js#L132)
531
532Create a context object from `locals` and the `view.data` and `view.locals` objects. The `view.data` property is typically created from front-matter, and `view.locals` is used when a `new View()` is created.
533
534This method be overridden either by defining a custom `view.options.context` function
535to customize context for a view instance, or static [View.context](#view-context) function to customize
536context for all view instances.
537
538**Params**
539
540* `locals` **{Object}**: Optionally pass a locals object to merge onto the context.
541* `returns` **{Object}**: Returns the context object.
542
543**Example**
544
545```js
546var page = new View({path: 'a/b/c.txt', locals: {a: 'b', c: 'd'}});
547var ctx = page.context({a: 'z'});
548console.log(ctx);
549//=> {a: 'z', c: 'd'}
550```
551
552### [.isType](node_modules/vinyl-view/index.js#L148)
553
554Returns true if the view is the given `viewType`. Returns `false` if no type is assigned. When used with vinyl-collections, types are assigned by their respective collections.
555
556**Params**
557
558* `type` **{String}**: (`renderable`, `partial`, `layout`)
559
560**Example**
561
562```js
563var view = new View({path: 'a/b/c.txt', viewType: 'partial'})
564view.isType('partial');
565```
566
567### [.View.context](node_modules/vinyl-view/index.js#L248)
568
569Define a custom static `View.context` function to override default `.context` behavior. See the [context](#context) docs for more info.
570
571**Params**
572
573* `locals` **{Object}**
574* `returns` **{Object}**
575
576**Example**
577
578```js
579// custom context function
580View.context = function(locals) {
581 // `this` is the view being rendered
582 return locals;
583};
584```
585
586### [.data](lib/plugins/context.js#L42)
587
588Set, get and load data to be passed to templates as context at render-time.
589
590**Params**
591
592* `key` **{String|Object}**: Pass a key-value pair or an object to set.
593* `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.
594* `returns` **{Object}**: Returns an instance of `Templates` for chaining.
595
596**Example**
597
598```js
599app.data('a', 'b');
600app.data({c: 'd'});
601console.log(app.cache.data);
602//=> {a: 'b', c: 'd'}
603```
604
605### [.context](lib/plugins/context.js#L62)
606
607Build the context for the given `view` and `locals`.
608
609**Params**
610
611* `view` **{Object}**: The view being rendered
612* `locals` **{Object}**
613* `returns` **{Object}**: The object to be passed to engines/views as context.
614
615### [setHelperOptions](lib/plugins/context.js#L119)
616
617Update context in a helper so that `this.helper.options` is
618the options for that specific helper.
619
620**Params**
621
622* `context` **{Object}**
623* `key` **{String}**
624
625### [.mergePartials](lib/plugins/context.js#L309)
626
627Merge "partials" view types. This is necessary for template
628engines have no support for partials or only support one
629type of partials.
630
631**Params**
632
633* `options` **{Object}**: Optionally pass an array of `viewTypes` to include on `options.viewTypes`
634* `returns` **{Object}**: Merged partials
635
636### [.mergePartialsAsync](lib/plugins/context.js#L350)
637
638Merge "partials" view types. This is necessary for template engines
639have no support for partials or only support one type of partials.
640
641**Params**
642
643* `options` **{Object}**: Optionally pass an array of `viewTypes` to include on `options.viewTypes`
644* `callback` **{Function}**: Function that exposes `err` and `partials` parameters
645
646***
647
648### Item
649
650API for the `Item` class.
651
652### [Item](node_modules/vinyl-item/index.js#L28)
653
654Create an instance of `Item`. Optionally pass a default object to use. See [vinyl][] docs for API details and additional documentation.
655
656**Params**
657
658* `item` **{Object}**
659
660**Example**
661
662```js
663var item = new Item({
664 path: 'foo.html',
665 contents: new Buffer('...')
666});
667```
668
669### [.content](node_modules/vinyl-item/index.js#L184)
670
671Normalize the `content` and `contents` properties on `item`. This is done to ensure compatibility with the vinyl convention of using `contents` as a Buffer, as well as the assemble convention of using `content` as a string. We will eventually deprecate the `content` property.
672
673**Example**
674
675```js
676var item = new Item({path: 'foo/bar.hbs', contents: new Buffer('foo')});
677console.log(item.content);
678//=> 'foo'
679```
680
681### [.engine](node_modules/vinyl-item/index.js#L206)
682
683Getter/setter to resolve the name of the `engine` to use for rendering.
684
685**Example**
686
687```js
688var item = new Item({path: 'foo/bar.hbs'});
689console.log(item.engine);
690//=> '.hbs'
691```
692
693### [.data](lib/plugins/context.js#L42)
694
695Set, get and load data to be passed to templates as context at render-time.
696
697**Params**
698
699* `key` **{String|Object}**: Pass a key-value pair or an object to set.
700* `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.
701* `returns` **{Object}**: Returns an instance of `Templates` for chaining.
702
703**Example**
704
705```js
706app.data('a', 'b');
707app.data({c: 'd'});
708console.log(app.cache.data);
709//=> {a: 'b', c: 'd'}
710```
711
712### [.context](lib/plugins/context.js#L62)
713
714Build the context for the given `view` and `locals`.
715
716**Params**
717
718* `view` **{Object}**: The view being rendered
719* `locals` **{Object}**
720* `returns` **{Object}**: The object to be passed to engines/views as context.
721
722### [setHelperOptions](lib/plugins/context.js#L119)
723
724Update context in a helper so that `this.helper.options` is
725the options for that specific helper.
726
727**Params**
728
729* `context` **{Object}**
730* `key` **{String}**
731
732### [.mergePartials](lib/plugins/context.js#L309)
733
734Merge "partials" view types. This is necessary for template
735engines have no support for partials or only support one
736type of partials.
737
738**Params**
739
740* `options` **{Object}**: Optionally pass an array of `viewTypes` to include on `options.viewTypes`
741* `returns` **{Object}**: Merged partials
742
743### [.mergePartialsAsync](lib/plugins/context.js#L350)
744
745Merge "partials" view types. This is necessary for template engines
746have no support for partials or only support one type of partials.
747
748**Params**
749
750* `options` **{Object}**: Optionally pass an array of `viewTypes` to include on `options.viewTypes`
751* `callback` **{Function}**: Function that exposes `err` and `partials` parameters
752
753***
754
755### Views
756
757API for the `Views` class.
758
759### [Views](lib/views.js#L28)
760
761Create an instance of `Views` with the given `options`.
762
763**Params**
764
765* `options` **{Object}**
766
767**Example**
768
769```js
770var collection = new Views();
771collection.addView('foo', {content: 'bar'});
772```
773
774### [.addView](lib/views.js#L126)
775
776Add a view to `collection.views`. This is identical to [addView](#addView) except `setView` returns the collection instance, and `addView` returns the item instance.
777
778**Params**
779
780* `key` **{String|Object}**: View key or object
781* `value` **{Object}**: If key is a string, value is the view object.
782* `returns` **{Object}**: returns the `view` instance.
783
784**Example**
785
786```js
787collection.setView('foo', {content: 'bar'});
788```
789
790### [.setView](lib/views.js#L168)
791
792Set a view on the collection. This is identical to [addView](#addView) except `setView` does not emit an event for each view.
793
794**Params**
795
796* `key` **{String|Object}**: View key or object
797* `value` **{Object}**: If key is a string, value is the view object.
798* `returns` **{Object}**: returns the `view` instance.
799
800**Example**
801
802```js
803collection.setView('foo', {content: 'bar'});
804```
805
806### [.getView](lib/views.js#L185)
807
808Get view `name` from `collection.views`.
809
810**Params**
811
812* `key` **{String}**: Key of the view to get.
813* `fn` **{Function}**: Optionally pass a function to modify the key.
814* `returns` **{Object}**
815
816**Example**
817
818```js
819collection.getView('a.html');
820```
821
822### [.deleteView](lib/views.js#L220)
823
824Delete a view from collection `views`.
825
826**Params**
827
828* `key` **{String}**
829* `returns` **{Object}**: Returns the instance for chaining
830
831**Example**
832
833```js
834views.deleteView('foo.html');
835```
836
837### [.addViews](lib/views.js#L244)
838
839Load multiple views onto the collection.
840
841**Params**
842
843* `views` **{Object|Array}**
844* `returns` **{Object}**: returns the `collection` object
845
846**Example**
847
848```js
849collection.addViews({
850 'a.html': {content: '...'},
851 'b.html': {content: '...'},
852 'c.html': {content: '...'}
853});
854```
855
856### [.addList](lib/views.js#L276)
857
858Load an array of views onto the collection.
859
860**Params**
861
862* `list` **{Array}**
863* `returns` **{Object}**: returns the `views` instance
864
865**Example**
866
867```js
868collection.addList([
869 {path: 'a.html', content: '...'},
870 {path: 'b.html', content: '...'},
871 {path: 'c.html', content: '...'}
872]);
873```
874
875### [.groupBy](lib/views.js#L313)
876
877Group all collection `views` by the given property, properties or compare functions. See [group-array][] for the full range of available features and options.
878
879* `returns` **{Object}**: Returns an object of grouped views.
880
881**Example**
882
883```js
884var collection = new Collection();
885collection.addViews(...);
886var groups = collection.groupBy('data.date', 'data.slug');
887```
888
889### [.isType](lib/views.js#L343)
890
891Return true if the collection belongs to the given view `type`.
892
893**Params**
894
895* `type` **{String}**: (`renderable`, `partial`, `layout`)
896
897**Example**
898
899```js
900collection.isType('partial');
901```
902
903### [.viewTypes](lib/views.js#L390)
904
905Alias for `viewType`
906
907### [.data](lib/plugins/context.js#L42)
908
909Set, get and load data to be passed to templates as context at render-time.
910
911**Params**
912
913* `key` **{String|Object}**: Pass a key-value pair or an object to set.
914* `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.
915* `returns` **{Object}**: Returns an instance of `Templates` for chaining.
916
917**Example**
918
919```js
920app.data('a', 'b');
921app.data({c: 'd'});
922console.log(app.cache.data);
923//=> {a: 'b', c: 'd'}
924```
925
926### [.context](lib/plugins/context.js#L62)
927
928Build the context for the given `view` and `locals`.
929
930**Params**
931
932* `view` **{Object}**: The view being rendered
933* `locals` **{Object}**
934* `returns` **{Object}**: The object to be passed to engines/views as context.
935
936### [setHelperOptions](lib/plugins/context.js#L119)
937
938Update context in a helper so that `this.helper.options` is
939the options for that specific helper.
940
941**Params**
942
943* `context` **{Object}**
944* `key` **{String}**
945
946### [.mergePartials](lib/plugins/context.js#L309)
947
948Merge "partials" view types. This is necessary for template
949engines have no support for partials or only support one
950type of partials.
951
952**Params**
953
954* `options` **{Object}**: Optionally pass an array of `viewTypes` to include on `options.viewTypes`
955* `returns` **{Object}**: Merged partials
956
957### [.mergePartialsAsync](lib/plugins/context.js#L350)
958
959Merge "partials" view types. This is necessary for template engines
960have no support for partials or only support one type of partials.
961
962**Params**
963
964* `options` **{Object}**: Optionally pass an array of `viewTypes` to include on `options.viewTypes`
965* `callback` **{Function}**: Function that exposes `err` and `partials` parameters
966
967***
968
969### [.find](lib/plugins/lookup.js#L25)
970
971Find a view by `name`, optionally passing a `collection` to limit the search. If no collection is passed all `renderable` collections will be searched.
972
973**Params**
974
975* `name` **{String}**: The name/key of the view to find
976* `colleciton` **{String}**: Optionally pass a collection name (e.g. pages)
977* `returns` **{Object|undefined}**: Returns the view if found, or `undefined` if not.
978
979**Example**
980
981```js
982var page = app.find('my-page.hbs');
983
984// optionally pass a collection name as the second argument
985var page = app.find('my-page.hbs', 'pages');
986```
987
988### [.getView](lib/plugins/lookup.js#L64)
989
990Get view `key` from the specified `collection`.
991
992**Params**
993
994* `collection` **{String}**: Collection name, e.g. `pages`
995* `key` **{String}**: Template name
996* `fn` **{Function}**: Optionally pass a `renameKey` function
997* `returns` **{Object}**
998
999**Example**
1000
1001```js
1002var view = app.getView('pages', 'a/b/c.hbs');
1003
1004// optionally pass a `renameKey` function to modify the lookup
1005var view = app.getView('pages', 'a/b/c.hbs', function(fp) {
1006 return path.basename(fp);
1007});
1008```
1009
1010### [.getViews](lib/plugins/lookup.js#L103)
1011
1012Get all views from a `collection` using the collection's singular or plural name.
1013
1014**Params**
1015
1016* `name` **{String}**: The collection name, e.g. `pages` or `page`
1017* `returns` **{Object}**
1018
1019**Example**
1020
1021```js
1022var pages = app.getViews('pages');
1023//=> { pages: {'home.hbs': { ... }}
1024
1025var posts = app.getViews('posts');
1026//=> { posts: {'2015-10-10.md': { ... }}
1027```
1028
1029***
1030
1031### Collections
1032
1033API for the `Collections` class.
1034
1035### [Collection](lib/collection.js#L25)
1036
1037Create an instance of `Collection` with the given `options`.
1038
1039**Params**
1040
1041* `options` **{Object}**
1042
1043**Example**
1044
1045```js
1046var collection = new Collection();
1047collection.addItem('foo', {content: 'bar'});
1048```
1049
1050### [.addItem](lib/collection.js#L93)
1051
1052Add an item to the collection.
1053
1054**Params**
1055
1056* `key` **{String|Object}**: Item name or object
1057* `val` **{Object}**: Item object, when `key` is a string.
1058* `returns` **{Object}**: returns the `item` instance.
1059
1060**Events**
1061
1062* `emits`: `item` With the created `item` and `collection` instance as arguments.
1063
1064**Example**
1065
1066```js
1067collection.addItem('foo', {content: 'bar'});
1068```
1069
1070### [.setItem](lib/collection.js#L118)
1071
1072Identical to `.addItem`, except the collection instance is returned instead of the item, to allow chaining.
1073
1074**Params**
1075
1076* `key` **{String|Object}**: Item name or object
1077* `val` **{Object}**: Item object, when `key` is a string.
1078* `returns` **{Object}**: returns the `collection` instance.
1079
1080**Events**
1081
1082* `emits`: `item` With the created `item` and `collection` instance as arguments.
1083
1084**Example**
1085
1086```js
1087collection.setItem('foo', {content: 'bar'});
1088```
1089
1090### [.getItem](lib/collection.js#L134)
1091
1092Get an item from `collection.items`.
1093
1094**Params**
1095
1096* `key` **{String}**: Key of the item to get.
1097* `returns` **{Object}**
1098
1099**Example**
1100
1101```js
1102collection.getItem('a.html');
1103```
1104
1105### [.deleteItem](lib/collection.js#L149)
1106
1107Remove an item from `collection.items`.
1108
1109**Params**
1110
1111* `key` **{String}**
1112* `returns` **{Object}**: Returns the instance for chaining
1113
1114**Example**
1115
1116```js
1117items.deleteItem('abc');
1118```
1119
1120### [.addItems](lib/collection.js#L172)
1121
1122Load multiple items onto the collection.
1123
1124**Params**
1125
1126* `items` **{Object|Array}**
1127* `returns` **{Object}**: returns the instance for chaining
1128
1129**Example**
1130
1131```js
1132collection.addItems({
1133 'a.html': {content: '...'},
1134 'b.html': {content: '...'},
1135 'c.html': {content: '...'}
1136});
1137```
1138
1139### [.addList](lib/collection.js#L196)
1140
1141Load an array of items onto the collection.
1142
1143**Params**
1144
1145* `items` **{Array}**: or an instance of `List`
1146* `fn` **{Function}**: Optional sync callback function that is called on each item.
1147* `returns` **{Object}**: returns the Collection instance for chaining
1148
1149**Example**
1150
1151```js
1152collection.addList([
1153 {path: 'a.html', content: '...'},
1154 {path: 'b.html', content: '...'},
1155 {path: 'c.html', content: '...'}
1156]);
1157```
1158
1159### [.data](lib/plugins/context.js#L42)
1160
1161Set, get and load data to be passed to templates as context at render-time.
1162
1163**Params**
1164
1165* `key` **{String|Object}**: Pass a key-value pair or an object to set.
1166* `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.
1167* `returns` **{Object}**: Returns an instance of `Templates` for chaining.
1168
1169**Example**
1170
1171```js
1172app.data('a', 'b');
1173app.data({c: 'd'});
1174console.log(app.cache.data);
1175//=> {a: 'b', c: 'd'}
1176```
1177
1178### [.context](lib/plugins/context.js#L62)
1179
1180Build the context for the given `view` and `locals`.
1181
1182**Params**
1183
1184* `view` **{Object}**: The view being rendered
1185* `locals` **{Object}**
1186* `returns` **{Object}**: The object to be passed to engines/views as context.
1187
1188### [setHelperOptions](lib/plugins/context.js#L119)
1189
1190Update context in a helper so that `this.helper.options` is
1191the options for that specific helper.
1192
1193**Params**
1194
1195* `context` **{Object}**
1196* `key` **{String}**
1197
1198### [.mergePartials](lib/plugins/context.js#L309)
1199
1200Merge "partials" view types. This is necessary for template
1201engines have no support for partials or only support one
1202type of partials.
1203
1204**Params**
1205
1206* `options` **{Object}**: Optionally pass an array of `viewTypes` to include on `options.viewTypes`
1207* `returns` **{Object}**: Merged partials
1208
1209### [.mergePartialsAsync](lib/plugins/context.js#L350)
1210
1211Merge "partials" view types. This is necessary for template engines
1212have no support for partials or only support one type of partials.
1213
1214**Params**
1215
1216* `options` **{Object}**: Optionally pass an array of `viewTypes` to include on `options.viewTypes`
1217* `callback` **{Function}**: Function that exposes `err` and `partials` parameters
1218
1219***
1220
1221### List
1222
1223API for the `List` class.
1224
1225### [List](lib/list.js#L29)
1226
1227Create 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.
1228
1229**Params**
1230
1231* `options` **{Object}**
1232
1233**Example**
1234
1235```js
1236var list = new List();
1237list.addItem('foo', {content: 'bar'});
1238```
1239
1240### [.addItem](lib/list.js#L108)
1241
1242Add an item to `list.items`. This is identical to [setItem](#setItem) except `addItem` returns the `item`, add `setItem` returns the instance of `List`.
1243
1244**Params**
1245
1246* `key` **{String|Object}**: Item key or object
1247* `value` **{Object}**: If key is a string, value is the item object.
1248* `returns` **{Object}**: returns the `item` instance.
1249
1250**Example**
1251
1252```js
1253collection.addItem('foo', {content: 'bar'});
1254```
1255
1256### [.setItem](lib/list.js#L146)
1257
1258Add an item to `list.items`. This is identical to [addItem](#addItem) except `addItem` returns the `item`, add `setItem` returns the instance of `List`.
1259
1260**Params**
1261
1262* `key` **{String}**
1263* `value` **{Object}**
1264* `returns` **{Object}**: Returns the instance of `List` to support chaining.
1265
1266**Example**
1267
1268```js
1269var items = new Items(...);
1270items.setItem('a.html', {path: 'a.html', contents: '...'});
1271```
1272
1273### [.addItems](lib/list.js#L166)
1274
1275Load multiple items onto the collection.
1276
1277**Params**
1278
1279* `items` **{Object|Array}**
1280* `returns` **{Object}**: returns the instance for chaining
1281
1282**Example**
1283
1284```js
1285collection.addItems({
1286 'a.html': {content: '...'},
1287 'b.html': {content: '...'},
1288 'c.html': {content: '...'}
1289});
1290```
1291
1292### [.addList](lib/list.js#L195)
1293
1294Load an array of items or the items from another instance of `List`.
1295
1296**Params**
1297
1298* `items` **{Array}**: or an instance of `List`
1299* `fn` **{Function}**: Optional sync callback function that is called on each item.
1300* `returns` **{Object}**: returns the List instance for chaining
1301
1302**Example**
1303
1304```js
1305var foo = new List(...);
1306var bar = new List(...);
1307bar.addList(foo);
1308```
1309
1310### [.hasItem](lib/list.js#L232)
1311
1312Return true if the list has the given item (name).
1313
1314**Params**
1315
1316* `key` **{String}**
1317* `returns` **{Object}**
1318
1319**Example**
1320
1321```js
1322list.addItem('foo.html', {content: '...'});
1323list.hasItem('foo.html');
1324//=> true
1325```
1326
1327### [.getIndex](lib/list.js#L248)
1328
1329Get a the index of a specific item from the list by `key`.
1330
1331**Params**
1332
1333* `key` **{String}**
1334* `returns` **{Object}**
1335
1336**Example**
1337
1338```js
1339list.getIndex('foo.html');
1340//=> 1
1341```
1342
1343### [.getItem](lib/list.js#L292)
1344
1345Get a specific item from the list by `key`.
1346
1347**Params**
1348
1349* `key` **{String}**: The item name/key.
1350* `returns` **{Object}**
1351
1352**Example**
1353
1354```js
1355list.getItem('foo.html');
1356//=> '<Item <foo.html>>'
1357```
1358
1359### [.getView](lib/list.js#L311)
1360
1361Proxy for `getItem`
1362
1363**Params**
1364
1365* `key` **{String}**: Pass the key of the `item` to get.
1366* `returns` **{Object}**
1367
1368**Example**
1369
1370```js
1371list.getItem('foo.html');
1372//=> '<Item "foo.html" <buffer e2 e2 e2>>'
1373```
1374
1375### [.deleteItem](lib/list.js#L325)
1376
1377Remove an item from the list.
1378
1379**Params**
1380
1381* `key` **{Object|String}**: Pass an `item` instance (object) or `item.key` (string).
1382
1383**Example**
1384
1385```js
1386list.deleteItem('a.html');
1387```
1388
1389### [.extendItem](lib/list.js#L344)
1390
1391Decorate each item on the list with additional methods
1392and properties. This provides a way of easily overriding
1393defaults.
1394
1395**Params**
1396
1397* `item` **{Object}**
1398* `returns` **{Object}**: Instance of item for chaining
1399
1400### [.groupBy](lib/list.js#L363)
1401
1402Group all list `items` using the given property, properties or compare functions. See [group-array][] for the full range of available features and options.
1403
1404* `returns` **{Object}**: Returns the grouped items.
1405
1406**Example**
1407
1408```js
1409var list = new List();
1410list.addItems(...);
1411var groups = list.groupBy('data.date', 'data.slug');
1412```
1413
1414### [.sortBy](lib/list.js#L389)
1415
1416Sort all list `items` using the given property, properties or compare functions. See [array-sort][] for the full range of available features and options.
1417
1418* `returns` **{Object}**: Returns a new `List` instance with sorted items.
1419
1420**Example**
1421
1422```js
1423var list = new List();
1424list.addItems(...);
1425var result = list.sortBy('data.date');
1426//=> new sorted list
1427```
1428
1429### [.paginate](lib/list.js#L437)
1430
1431Paginate all `items` in the list with the given options, See [paginationator][] for the full range of available features and options.
1432
1433* `returns` **{Object}**: Returns the paginated items.
1434
1435**Example**
1436
1437```js
1438var list = new List(items);
1439var pages = list.paginate({limit: 5});
1440```
1441
1442### [.data](lib/plugins/context.js#L42)
1443
1444Set, get and load data to be passed to templates as context at render-time.
1445
1446**Params**
1447
1448* `key` **{String|Object}**: Pass a key-value pair or an object to set.
1449* `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.
1450* `returns` **{Object}**: Returns an instance of `Templates` for chaining.
1451
1452**Example**
1453
1454```js
1455app.data('a', 'b');
1456app.data({c: 'd'});
1457console.log(app.cache.data);
1458//=> {a: 'b', c: 'd'}
1459```
1460
1461### [.context](lib/plugins/context.js#L62)
1462
1463Build the context for the given `view` and `locals`.
1464
1465**Params**
1466
1467* `view` **{Object}**: The view being rendered
1468* `locals` **{Object}**
1469* `returns` **{Object}**: The object to be passed to engines/views as context.
1470
1471### [setHelperOptions](lib/plugins/context.js#L119)
1472
1473Update context in a helper so that `this.helper.options` is
1474the options for that specific helper.
1475
1476**Params**
1477
1478* `context` **{Object}**
1479* `key` **{String}**
1480
1481### [.mergePartials](lib/plugins/context.js#L309)
1482
1483Merge "partials" view types. This is necessary for template
1484engines have no support for partials or only support one
1485type of partials.
1486
1487**Params**
1488
1489* `options` **{Object}**: Optionally pass an array of `viewTypes` to include on `options.viewTypes`
1490* `returns` **{Object}**: Merged partials
1491
1492### [.mergePartialsAsync](lib/plugins/context.js#L350)
1493
1494Merge "partials" view types. This is necessary for template engines
1495have no support for partials or only support one type of partials.
1496
1497**Params**
1498
1499* `options` **{Object}**: Optionally pass an array of `viewTypes` to include on `options.viewTypes`
1500* `callback` **{Function}**: Function that exposes `err` and `partials` parameters
1501
1502***
1503
1504### Group
1505
1506API for the `Group` class.
1507
1508### [Group](lib/group.js#L25)
1509
1510Create an instance of `Group` with the given `options`.
1511
1512**Params**
1513
1514* `options` **{Object}**
1515
1516**Example**
1517
1518```js
1519var group = new Group({
1520 'foo': { items: [1,2,3] }
1521});
1522```
1523
1524***
1525
1526### [.find](lib/plugins/lookup.js#L25)
1527
1528Find a view by `name`, optionally passing a `collection` to limit the search. If no collection is passed all `renderable` collections will be searched.
1529
1530**Params**
1531
1532* `name` **{String}**: The name/key of the view to find
1533* `colleciton` **{String}**: Optionally pass a collection name (e.g. pages)
1534* `returns` **{Object|undefined}**: Returns the view if found, or `undefined` if not.
1535
1536**Example**
1537
1538```js
1539var page = app.find('my-page.hbs');
1540
1541// optionally pass a collection name as the second argument
1542var page = app.find('my-page.hbs', 'pages');
1543```
1544
1545### [.getView](lib/plugins/lookup.js#L64)
1546
1547Get view `key` from the specified `collection`.
1548
1549**Params**
1550
1551* `collection` **{String}**: Collection name, e.g. `pages`
1552* `key` **{String}**: Template name
1553* `fn` **{Function}**: Optionally pass a `renameKey` function
1554* `returns` **{Object}**
1555
1556**Example**
1557
1558```js
1559var view = app.getView('pages', 'a/b/c.hbs');
1560
1561// optionally pass a `renameKey` function to modify the lookup
1562var view = app.getView('pages', 'a/b/c.hbs', function(fp) {
1563 return path.basename(fp);
1564});
1565```
1566
1567### [.getViews](lib/plugins/lookup.js#L103)
1568
1569Get all views from a `collection` using the collection's singular or plural name.
1570
1571**Params**
1572
1573* `name` **{String}**: The collection name, e.g. `pages` or `page`
1574* `returns` **{Object}**
1575
1576**Example**
1577
1578```js
1579var pages = app.getViews('pages');
1580//=> { pages: {'home.hbs': { ... }}
1581
1582var posts = app.getViews('posts');
1583//=> { posts: {'2015-10-10.md': { ... }}
1584```
1585
1586***
1587
1588### [.compile](lib/plugins/render.js#L98)
1589
1590Compile `content` with the given `locals`.
1591
1592**Params**
1593
1594* `view` **{Object|String}**: View object.
1595* `locals` **{Object}**
1596* `isAsync` **{Boolean}**: Load async helpers
1597* `returns` **{Object}**: View object with compiled `view.fn` property.
1598
1599**Example**
1600
1601```js
1602var indexPage = app.page('some-index-page.hbs');
1603var view = app.compile(indexPage);
1604// view.fn => [function]
1605
1606// you can call the compiled function more than once
1607// to render the view with different data
1608view.fn({title: 'Foo'});
1609view.fn({title: 'Bar'});
1610view.fn({title: 'Baz'});
1611```
1612
1613### [.compileAsync](lib/plugins/render.js#L173)
1614
1615Asynchronously 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)_.
1616
1617**Params**
1618
1619* `view` **{Object|String}**: View object.
1620* `locals` **{Object}**
1621* `isAsync` **{Boolean}**: Pass true to load helpers as async (mostly used internally)
1622* `callback` **{Function}**: function that exposes `err` and the `view` object with compiled `view.fn` property
1623
1624**Example**
1625
1626```js
1627var indexPage = app.page('some-index-page.hbs');
1628app.compileAsync(indexPage, function(err, view) {
1629 // view.fn => compiled function
1630});
1631```
1632
1633### [.render](lib/plugins/render.js#L260)
1634
1635Render a view with the given `locals` and `callback`.
1636
1637**Params**
1638
1639* `view` **{Object|String}**: Instance of `View`
1640* `locals` **{Object}**: Locals to pass to template engine.
1641* `callback` **{Function}**
1642
1643**Example**
1644
1645```js
1646var blogPost = app.post.getView('2015-09-01-foo-bar');
1647app.render(blogPost, {title: 'Foo'}, function(err, view) {
1648 // `view` is an object with a rendered `content` property
1649});
1650```
1651
1652***
1653
1654### [.data](lib/plugins/context.js#L42)
1655
1656Set, get and load data to be passed to templates as context at render-time.
1657
1658**Params**
1659
1660* `key` **{String|Object}**: Pass a key-value pair or an object to set.
1661* `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.
1662* `returns` **{Object}**: Returns an instance of `Templates` for chaining.
1663
1664**Example**
1665
1666```js
1667app.data('a', 'b');
1668app.data({c: 'd'});
1669console.log(app.cache.data);
1670//=> {a: 'b', c: 'd'}
1671```
1672
1673### [.context](lib/plugins/context.js#L62)
1674
1675Build the context for the given `view` and `locals`.
1676
1677**Params**
1678
1679* `view` **{Object}**: The view being rendered
1680* `locals` **{Object}**
1681* `returns` **{Object}**: The object to be passed to engines/views as context.
1682
1683### [setHelperOptions](lib/plugins/context.js#L119)
1684
1685Update context in a helper so that `this.helper.options` is
1686the options for that specific helper.
1687
1688**Params**
1689
1690* `context` **{Object}**
1691* `key` **{String}**
1692
1693### [.mergePartials](lib/plugins/context.js#L309)
1694
1695Merge "partials" view types. This is necessary for template
1696engines have no support for partials or only support one
1697type of partials.
1698
1699**Params**
1700
1701* `options` **{Object}**: Optionally pass an array of `viewTypes` to include on `options.viewTypes`
1702* `returns` **{Object}**: Merged partials
1703
1704### [.mergePartialsAsync](lib/plugins/context.js#L350)
1705
1706Merge "partials" view types. This is necessary for template engines
1707have no support for partials or only support one type of partials.
1708
1709**Params**
1710
1711* `options` **{Object}**: Optionally pass an array of `viewTypes` to include on `options.viewTypes`
1712* `callback` **{Function}**: Function that exposes `err` and `partials` parameters
1713
1714***
1715
1716### Routes and middleware
1717
1718**Example**
1719
1720```js
1721var router = new app.Router();
1722var route = new app.Route();
1723```
1724
1725### [.handle](node_modules/base-routes/index.js#L55)
1726
1727Handle a middleware `method` for `file`.
1728
1729**Params**
1730
1731* `method` **{String}**: Name of the router method to handle. See [router methods](./docs/router.md)
1732* `file` **{Object}**: View object
1733* `callback` **{Function}**: Callback function
1734* `returns` **{undefined}**
1735
1736**Example**
1737
1738```js
1739app.handle('customMethod', file, callback);
1740```
1741
1742### [.handleOnce](node_modules/base-routes/index.js#L109)
1743
1744Run the given middleware handler only if the file has not already been handled by `method`.
1745
1746**Params**
1747
1748* `method` **{Object}**: The name of the handler method to call.
1749* `file` **{Object}**
1750* `returns` **{undefined}**
1751
1752**Example**
1753
1754```js
1755app.handleOnce('onLoad', file, callback);
1756```
1757
1758### [.route](node_modules/base-routes/index.js#L181)
1759
1760Create 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.
1761
1762**Params**
1763
1764* `path` **{String}**
1765* `returns` **{Object}**: Returns the instance for chaining.
1766
1767**Example**
1768
1769```js
1770app.create('posts');
1771app.route(/blog/)
1772 .all(function(file, next) {
1773 // do something with file
1774 next();
1775 });
1776
1777app.post('whatever', {path: 'blog/foo.bar', content: 'bar baz'});
1778```
1779
1780### [.param](node_modules/base-routes/index.js#L208)
1781
1782Add callback triggers to route parameters, where `name` is the name of the parameter and `fn` is the callback function.
1783
1784**Params**
1785
1786* `name` **{String}**
1787* `fn` **{Function}**
1788* `returns` **{Object}**: Returns the instance for chaining.
1789
1790**Example**
1791
1792```js
1793app.param('title', function(view, next, title) {
1794 //=> title === 'foo.js'
1795 next();
1796});
1797
1798app.onLoad('/blog/:title', function(view, next) {
1799 //=> view.path === '/blog/foo.js'
1800 next();
1801});
1802```
1803
1804### [.all](node_modules/base-routes/index.js#L231)
1805
1806Special route method that works just like the `router.METHOD()` methods, except that it matches all verbs.
1807
1808**Params**
1809
1810* `path` **{String}**
1811* `callback` **{Function}**
1812* `returns` **{Object}** `this`: for chaining
1813
1814**Example**
1815
1816```js
1817app.all(/\.hbs$/, function(view, next) {
1818 // do stuff to view
1819 next();
1820});
1821```
1822
1823### [.handler](node_modules/base-routes/index.js#L253)
1824
1825Add a router handler method to the instance. Interchangeable with the [handlers](#handlers) method.
1826
1827**Params**
1828
1829* `method` **{String}**: Name of the handler method to define.
1830* `returns` **{Object}**: Returns the instance for chaining
1831
1832**Example**
1833
1834```js
1835app.handler('onFoo');
1836// or
1837app.handler(['onFoo', 'onBar']);
1838```
1839
1840### [.handlers](node_modules/base-routes/index.js#L272)
1841
1842Add one or more router handler methods to the instance.
1843
1844**Params**
1845
1846* `methods` **{Array|String}**: One or more method names to define.
1847* `returns` **{Object}**: Returns the instance for chaining
1848
1849**Example**
1850
1851```js
1852app.handlers(['onFoo', 'onBar', 'onBaz']);
1853// or
1854app.handlers('onFoo');
1855```
1856
1857***
1858
1859### [.isApp](lib/plugins/is.js#L33)
1860
1861Static method that returns true if the given value is a `templates` instance (`App`).
1862
1863**Params**
1864
1865* `val` **{Object}**: The value to test.
1866* `returns` **{Boolean}**
1867
1868**Example**
1869
1870```js
1871var templates = require('templates');
1872var app = templates();
1873
1874templates.isApp(templates);
1875//=> false
1876
1877templates.isApp(app);
1878//=> true
1879```
1880
1881### [.isCollection](lib/plugins/is.js#L55)
1882
1883Static method that returns true if the given value is a templates `Collection` instance.
1884
1885**Params**
1886
1887* `val` **{Object}**: The value to test.
1888* `returns` **{Boolean}**
1889
1890**Example**
1891
1892```js
1893var templates = require('templates');
1894var app = templates();
1895
1896app.create('pages');
1897templates.isCollection(app.pages);
1898//=> true
1899```
1900
1901### [.isViews](lib/plugins/is.js#L77)
1902
1903Static method that returns true if the given value is a templates `Views` instance.
1904
1905**Params**
1906
1907* `val` **{Object}**: The value to test.
1908* `returns` **{Boolean}**
1909
1910**Example**
1911
1912```js
1913var templates = require('templates');
1914var app = templates();
1915
1916app.create('pages');
1917templates.isViews(app.pages);
1918//=> true
1919```
1920
1921### [.isList](lib/plugins/is.js#L100)
1922
1923Static method that returns true if the given value is a templates `List` instance.
1924
1925**Params**
1926
1927* `val` **{Object}**: The value to test.
1928* `returns` **{Boolean}**
1929
1930**Example**
1931
1932```js
1933var templates = require('templates');
1934var List = templates.List;
1935var app = templates();
1936
1937var list = new List();
1938templates.isList(list);
1939//=> true
1940```
1941
1942### [.isGroup](lib/plugins/is.js#L123)
1943
1944Static method that returns true if the given value is a templates `Group` instance.
1945
1946**Params**
1947
1948* `val` **{Object}**: The value to test.
1949* `returns` **{Boolean}**
1950
1951**Example**
1952
1953```js
1954var templates = require('templates');
1955var Group = templates.Group;
1956var app = templates();
1957
1958var group = new Group();
1959templates.isGroup(group);
1960//=> true
1961```
1962
1963### [.isView](lib/plugins/is.js#L148)
1964
1965Static method that returns true if the given value is a templates `View` instance.
1966
1967**Params**
1968
1969* `val` **{Object}**: The value to test.
1970* `returns` **{Boolean}**
1971
1972**Example**
1973
1974```js
1975var templates = require('templates');
1976var app = templates();
1977
1978templates.isView('foo');
1979//=> false
1980
1981var view = app.view('foo', {content: '...'});
1982templates.isView(view);
1983//=> true
1984```
1985
1986### [.isItem](lib/plugins/is.js#L173)
1987
1988Static method that returns true if the given value is a templates `Item` instance.
1989
1990**Params**
1991
1992* `val` **{Object}**: The value to test.
1993* `returns` **{Boolean}**
1994
1995**Example**
1996
1997```js
1998var templates = require('templates');
1999var app = templates();
2000
2001templates.isItem('foo');
2002//=> false
2003
2004var view = app.view('foo', {content: '...'});
2005templates.isItem(view);
2006//=> true
2007```
2008
2009### [.isVinyl](lib/plugins/is.js#L200)
2010
2011Static method that returns true if the given value is a vinyl `File` instance.
2012
2013**Params**
2014
2015* `val` **{Object}**: The value to test.
2016* `returns` **{Boolean}**
2017
2018**Example**
2019
2020```js
2021var File = require('vinyl');
2022var templates = require('templates');
2023var app = templates();
2024
2025var view = app.view('foo', {content: '...'});
2026templates.isVinyl(view);
2027//=> true
2028
2029var file = new File({path: 'foo', contents: new Buffer('...')});
2030templates.isVinyl(file);
2031//=> true
2032```
2033
2034***
2035
2036## History
2037
2038### v0.24.0
2039
2040* Bumps [base-data][] which removed `renameKey` option used when loading data. Use the `namespace` option instead.
2041
2042### v0.23.0
2043
2044* Bumps [base-engine][] to fix a bug in [engine-cache][].
2045
2046### v0.22.2
2047
2048* fixes `List` bug that was caused collection helpers to explode
2049
2050### v0.22.0
2051
2052There should be no breaking changes in this release. If you experience a regression, please [create an issue](../../issues).
2053
2054* Externalizes a few core plugins to: [base-helpers][], [base-routes][], and [base-engine][]. The goal is to allow you to use only the plugins you need in your builds.
2055* Improvements to lookup functions: `app.getView()` and `app.find()`
2056* Bumps [base][] to take advantages of code optimizations.
2057
2058### v0.21.0
2059
2060**Breaking changes**
2061
2062* The `queue` property has been removed, as well as related code for loading views using events. This behavior can easily be added using plugins or existing emitters.
2063
2064**Non-breaking**
2065
2066* The `View` and `Item` class have been externalized to modules [vinyl-item][] and [vinyl-view][] so they can be used in other libraries.
2067
2068### v0.20.0
2069
2070* **Context**: In general, context should be merged so that the most specific context wins over less specific. This fixes one case where locals was winning over front-matter
2071* **Helpers**: Exposes `.ctx()` method on helper context, to simplify merging context in non-built-in helpers
2072* **Engines**: Fixes bug that was using default engine on options instead of engine that matches view file extension.
2073
2074### v0.19.0
2075
2076* Numerous [dependency updates](https://github.com/jonschlinkert/templates/commit/6f78d88aa1920b84d20177bf35942e596b8e58b5)
2077
2078### v0.18.0
2079
2080* [Fixes inheritance bug](https://github.com/jonschlinkert/templates/commit/66b0d885648600c97b4a158eaebf3e95443ec66e) that only manifests in node v0.4.0
2081* Improved [error handling in routes](https://github.com/jonschlinkert/templates/commit/d7654b74502465587da1e490c09e486fbf43f6db)
2082
2083### v0.17.0
2084
2085* Removed `debug` methods and related code
2086* Improve layout handling with respect to template types (`partial`, `renderable` and `layout`)
2087* Update dependencies
2088
2089### v0.16.0
2090
2091* Improved context handling
2092* Ensure collection middleware is handled [after app middleware](https://github.com/jonschlinkert/templates/commit/f47385f5172a2773c3ab2a969ebfccc533ec5e27)
2093
2094### v0.15.0
2095
2096* removes `.removeItem` method that was deprecated in v0.10.7 from `List`
2097* `.handleView` is deprecated in favor of `.handleOnce` and will be removed in a future version. Start using `.handleOnce` now.
2098* adds a static `Templates.setup()` method for initializing any setup code that should have access to the instance before any other use code is run.
2099* upgrade to [base-data][] v0.4.0, which adds `app.option.set`, `app.option.get` and `app.option.merge`
2100
2101### v0.14.0
2102
2103Although 99% of users won't be effected by the changes in this release, there were some **potentially breaking changes**.
2104
2105* The `render` and `compile` methods were streamlined, making it clear that `.mergePartials` should not have been renamed to `mergePartialsSync`. So that change was reverted.
2106* Helper context: Exposes a `this.helper` object to the context in helpers, which has the helper name and options that were set specifically for that helper
2107* Helper context: Exposes a `this.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.
2108* Helper context: `.get`, `.set` and `.merge` methods on `this.options`, `this.context` and the `this` object in helpers.
2109
2110### v0.13.0
2111
2112* All template handling is async by default. Instead of adding `.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.
2113* Improved layout detection and handling
2114
2115### v0.12.0
2116
2117* Adds helper methods, [.hasAsyncHelper](#hasasynchelper), [.hasHelper](#hashelper), [.getAsyncHelper](#getasynchelper), and [.getHelper](#gethelper)
2118* Ensures that both collection and app routes are handled when both are defined for a view
2119
2120### v0.11.0
2121
2122* Default `engine` can now be defined on `app` or a collection using using `app.option('engine')`, `views.option('engine')`
2123* Default `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/#818](../../issues/818)
2124* Improves logic for finding a layout, this should make layouts easier to define and find going forward.
2125* The built-in `view` helper has been refactored completely. The helper is now async and renders the view before returning its content.
2126* Adds `isApp`, `isViews`, `isCollection`, `isList`, `isView`, `isGroup`, and `isItem` static methods. All return true when the given value is an instance of the respective class.
2127* Adds `deleteItem` method to List and Collection, and `deleteView` method to Views.
2128* Last, the static `_.proto` property which is only exposed for unit tests was renamed to `_.plugin`.
2129
2130### v0.10.7
2131
2132* Force-update [base][] to v0.6.4 to take advantage of `isRegistered` feature.
2133
2134### v0.10.6
2135
2136* Re-introduces fs logic to `getView`, now that the method has been refactored to be faster.
2137
2138### v0.10.0
2139
2140* `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.
2141
2142### v0.9.5
2143
2144* Fixes error messages when no engine is found for a view, and the view does not have a file extension.
2145
2146### v0.9.4
2147
2148* Fixes a lookup bug in render and compile that was returning the first view that matched the given name from _any_ collection. So if a partial and a page shared the same name, if the partial was matched first it was returned. Now the `renderable` view is rendered (e.g. page)
2149
2150### v0.9.0
2151
2152* _breaking change_: changes parameters on `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.
2153
2154### v0.8.0
2155
2156* Exposes `isType` method on `view`. Shouldn't be any breaking changes.
2157
2158### v0.7.0
2159
2160* _breaking change_: renamed `.error` method to `.formatError`
2161* adds `mergeContext` option
2162* collection name is now emitted with `view` and `item` as the second argument
2163* adds `isType` method for checking the `viewType` on a collection
2164* also now emits an event with the collection name when a view is created
2165
2166### v0.5.1
2167
2168* fixes bug where `default` layout was automatically applied to partials, causing an infinite loop in rare cases.
2169
2170## Related projects
2171
2172You might also be interested in these projects:
2173
2174* [assemble](https://www.npmjs.com/package/assemble): Assemble is a powerful, extendable and easy to use static site generator for node.js. Used… [more](https://github.com/assemble/assemble) | [homepage](https://github.com/assemble/assemble "Assemble is a powerful, extendable and easy to use static site generator for node.js. Used by thousands of projects for much more than building websites, Assemble is also used for creating themes, scaffolds, boilerplates, e-books, UI components, API docum")
2175* [en-route](https://www.npmjs.com/package/en-route): Routing for static site generators, build systems and task runners, heavily based on express.js routes… [more](https://github.com/jonschlinkert/en-route) | [homepage](https://github.com/jonschlinkert/en-route "Routing for static site generators, build systems and task runners, heavily based on express.js routes but works with file objects. Used by Assemble, Verb, and Template.")
2176* [engine](https://www.npmjs.com/package/engine): Template engine based on Lo-Dash template, but adds features like the ability to register helpers… [more](https://github.com/jonschlinkert/engine) | [homepage](https://github.com/jonschlinkert/engine "Template engine based on Lo-Dash template, but adds features like the ability to register helpers and more easily set data to be used as context in templates.")
2177* [layouts](https://www.npmjs.com/package/layouts): Wraps templates with layouts. Layouts can use other layouts and be nested to any depth… [more](https://github.com/doowb/layouts) | [homepage](https://github.com/doowb/layouts "Wraps templates with layouts. Layouts can use other layouts and be nested to any depth. This can be used 100% standalone to wrap any kind of file with banners, headers or footer content. Use for markdown, HTML, handlebars views, lo-dash templates, etc. La")
2178* [verb](https://www.npmjs.com/package/verb): Documentation generator for GitHub projects. Verb is extremely powerful, easy to use, and is used… [more](https://github.com/verbose/verb) | [homepage](https://github.com/verbose/verb "Documentation generator for GitHub projects. Verb is extremely powerful, easy to use, and is used on hundreds of projects of all sizes to generate everything from API docs to readmes.")
2179
2180## Contributing
2181
2182This document was generated by [verb-readme-generator][] (a [verb][] generator), please don't edit directly. Any changes to the readme must be made in [.verb.md](.verb.md). See [Building Docs](#building-docs).
2183
2184Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
2185
2186Or visit the [verb-readme-generator][] project to submit bug reports or pull requests for the readme layout template.
2187
2188## Building docs
2189
2190_(This document was generated by [verb-readme-generator][] (a [verb][] generator), please don't edit the readme directly. Any changes to the readme must be made in [.verb.md](.verb.md).)_
2191
2192Generate readme and API documentation with [verb][]:
2193
2194```sh
2195$ npm install -g verb verb-readme-generator && verb
2196```
2197
2198## Running tests
2199
2200Install dev dependencies:
2201
2202```sh
2203$ npm install -d && npm test
2204```
2205
2206## Author
2207
2208**Jon Schlinkert**
2209
2210* [github/jonschlinkert](https://github.com/jonschlinkert)
2211* [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
2212
2213## License
2214
2215Copyright © 2016, [Jon Schlinkert](https://github.com/jonschlinkert).
2216Released under the [MIT license](https://github.com/jonschlinkert/templates/blob/master/LICENSE).
2217
2218***
2219
2220_This file was generated by [verb](https://github.com/verbose/verb), v0.9.0, on July 02, 2016._
\No newline at end of file