You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

210 lines
9.0 KiB

4 years ago
  1. # Consolidate.js
  2. Template engine consolidation library.
  3. ## Installation
  4. $ npm install consolidate
  5. ## Supported template engines
  6. - [atpl](https://github.com/soywiz/atpl.js)
  7. - [bracket](https://github.com/danlevan/bracket-template)
  8. - [doT.js](https://github.com/olado/doT) [(website)](http://olado.github.io/doT/)
  9. - [dust (unmaintained)](https://github.com/akdubya/dustjs) [(website)](http://akdubya.github.com/dustjs/)
  10. - [dustjs-linkedin (maintained fork of dust)](https://github.com/linkedin/dustjs) [(website)](http://linkedin.github.io/dustjs/)
  11. - [eco](https://github.com/sstephenson/eco)
  12. - [ect](https://github.com/baryshev/ect) [(website)](http://ectjs.com/)
  13. - [ejs](https://github.com/mde/ejs) [(website)](http://ejs.co/)
  14. - [haml](https://github.com/visionmedia/haml.js)
  15. - [haml-coffee](https://github.com/9elements/haml-coffee)
  16. - [hamlet](https://github.com/gregwebs/hamlet.js)
  17. - [handlebars](https://github.com/wycats/handlebars.js/) [(website)](http://handlebarsjs.com/)
  18. - [hogan](https://github.com/twitter/hogan.js) [(website)](http://twitter.github.com/hogan.js/)
  19. - [htmling](https://github.com/codemix/htmling)
  20. - [jade](https://github.com/visionmedia/jade) [(website)](http://jade-lang.com/)
  21. - [jazz](https://github.com/shinetech/jazz)
  22. - [jqtpl](https://github.com/kof/jqtpl)
  23. - [JUST](https://github.com/baryshev/just)
  24. - [liquor](https://github.com/chjj/liquor)
  25. - [lodash](https://github.com/bestiejs/lodash) [(website)](http://lodash.com/)
  26. - [marko](https://github.com/marko-js/marko) [(website)](http://markojs.com)
  27. - [mote](https://github.com/satchmorun/mote) [(website)](http://satchmorun.github.io/mote/)
  28. - [mustache](https://github.com/janl/mustache.js)
  29. - [nunjucks](https://github.com/mozilla/nunjucks) [(website)](https://mozilla.github.io/nunjucks)
  30. - [plates](https://github.com/flatiron/plates)
  31. - [pug (formerly jade)](https://github.com/pugjs/pug) [(website)](http://jade-lang.com/)
  32. - [QEJS](https://github.com/jepso/QEJS)
  33. - [ractive](https://github.com/Rich-Harris/Ractive)
  34. - [react](https://github.com/facebook/react)
  35. - [slm](https://github.com/slm-lang/slm)
  36. - [swig (unmaintained)](https://github.com/paularmstrong/swig)
  37. - [swig (maintained fork)](https://github.com/node-swig/swig-templates)
  38. - [teacup](https://github.com/goodeggs/teacup)
  39. - [templayed](http://archan937.github.com/templayed.js/)
  40. - [twig](https://github.com/justjohn/twig.js)
  41. - [liquid](https://github.com/leizongmin/tinyliquid) [(website)](http://liquidmarkup.org/)
  42. - [toffee](https://github.com/malgorithms/toffee)
  43. - [underscore](https://github.com/documentcloud/underscore) [(website)](http://underscorejs.org/#template)
  44. - [vash](https://github.com/kirbysayshi/vash)
  45. - [walrus](https://github.com/jeremyruppel/walrus) [(website)](http://documentup.com/jeremyruppel/walrus/)
  46. - [whiskers](https://github.com/gsf/whiskers.js)
  47. __NOTE__: you must still install the engines you wish to use, add them to your package.json dependencies.
  48. ## API
  49. All templates supported by this library may be rendered using the signature `(path[, locals], callback)` as shown below, which happens to be the signature that Express 3.x supports so any of these engines may be used within Express.
  50. __NOTE__: All this example code uses cons.swig for the swig template engine. Replace swig with whatever templating you are using. For example, use cons.hogan for hogan.js, cons.jade for jade, etc. `console.log(cons)` for the full list of identifiers.
  51. ```js
  52. var cons = require('consolidate');
  53. cons.swig('views/page.html', { user: 'tobi' }, function(err, html){
  54. if (err) throw err;
  55. console.log(html);
  56. });
  57. ```
  58. Or without options / local variables:
  59. ```js
  60. var cons = require('consolidate');
  61. cons.swig('views/page.html', function(err, html){
  62. if (err) throw err;
  63. console.log(html);
  64. });
  65. ```
  66. To dynamically pass the engine, simply use the subscript operator and a variable:
  67. ```js
  68. var cons = require('consolidate')
  69. , name = 'swig';
  70. cons[name]('views/page.html', { user: 'tobi' }, function(err, html){
  71. if (err) throw err;
  72. console.log(html);
  73. });
  74. ```
  75. ### Promises
  76. Additionally, all templates optionally return a promise if no callback function is provided. The promise represents the eventual result of the template function which will either resolve to a string, compiled from the template, or be rejected. Promises expose a `then` method which registers callbacks to receive the promise’s eventual value and a `catch` method which the reason why the promise could not be fulfilled. Promises allow more synchronous-like code structure and solve issues like race conditions.
  77. ```js
  78. var cons = require('consolidate');
  79. cons.swig('views/page.html', { user: 'tobi' })
  80. .then(function (html) {
  81. console.log(html);
  82. })
  83. .catch(function (err) {
  84. throw err;
  85. });
  86. ```
  87. ## Caching
  88. To enable caching simply pass `{ cache: true }`. Engines _may_ use this option to cache things reading the file contents, compiled `Function`s etc. Engines which do _not_ support this may simply ignore it. All engines that consolidate.js implements I/O for will cache the file contents, ideal for production environments.
  89. When using consolidate directly: `cons.swig('views/page.html', { user: 'tobi', cache:true }, callback);`
  90. Using Express 3 or higher: `app.locals.cache = true` or set NODE_ENV to 'production' and Express will do this for you.
  91. ## Express 3.x example
  92. ```js
  93. var express = require('express')
  94. , cons = require('consolidate')
  95. , app = express();
  96. // assign the swig engine to .html files
  97. app.engine('html', cons.swig);
  98. // set .html as the default extension
  99. app.set('view engine', 'html');
  100. app.set('views', __dirname + '/views');
  101. var users = [];
  102. users.push({ name: 'tobi' });
  103. users.push({ name: 'loki' });
  104. users.push({ name: 'jane' });
  105. app.get('/', function(req, res){
  106. res.render('index', {
  107. title: 'Consolidate.js'
  108. });
  109. });
  110. app.get('/users', function(req, res){
  111. res.render('users', {
  112. title: 'Users',
  113. users: users
  114. });
  115. });
  116. app.listen(3000);
  117. console.log('Express server listening on port 3000');
  118. ```
  119. ## Template Engine Instances
  120. Template engines are exposed via the `cons.requires` object, but they are not instantiated until you've called the `cons[engine].render()` method. You can instantiate them manually beforehand if you want to add filters, globals, mixins, or other engine features.
  121. ```js
  122. var cons = require('consolidate'),
  123. nunjucks = require('nunjucks');
  124. // add nunjucks to requires so filters can be
  125. // added and the same instance will be used inside the render method
  126. cons.requires.nunjucks = nunjucks.configure();
  127. cons.requires.nunjucks.addFilter('foo', function () {
  128. return 'bar';
  129. });
  130. ```
  131. ## Notes
  132. * If you're using Nunjucks, please take a look at the `exports.nunjucks.render` function in `lib.consolidate.js`. You can pass your own engine/environment via `options.nunjucksEnv`, or if you want to support Express you can pass `options.settings.views`, or if you have another use case, pass `options.nunjucks` (see the code for more insight).
  133. * You can pass **partials** with `options.partials`
  134. * For using **template inheritance** with nunjucks, you can pass a loader
  135. with `options.loader`.
  136. * To use **filters** with tinyliquid, use `options.filters` and specify an array of properties, each of which is a named filter function. A filter function takes a string as a parameter and returns a modified version of it.
  137. * To use **custom tags** with tinyliquid, use `options.customTags` to specify an array of tag functions that follow the tinyliquid [custom tag](https://github.com/leizongmin/tinyliquid/wiki/Custom-Tag) definition.
  138. * The default directory used with the **include** tag with tinyliquid is the current working directory. To override this, use `options.includeDir`.
  139. * `React` To render content into a html base template (eg. `index.html` of your React app), pass the path of the template with `options.base`.
  140. ## Running tests
  141. Install dev deps:
  142. $ npm install -d
  143. Run the tests:
  144. $ make test
  145. ## License
  146. (The MIT License)
  147. Copyright (c) 2011-2016 TJ Holowaychuk <tj@vision-media.ca>
  148. Permission is hereby granted, free of charge, to any person obtaining
  149. a copy of this software and associated documentation files (the
  150. 'Software'), to deal in the Software without restriction, including
  151. without limitation the rights to use, copy, modify, merge, publish,
  152. distribute, sublicense, and/or sell copies of the Software, and to
  153. permit persons to whom the Software is furnished to do so, subject to
  154. the following conditions:
  155. The above copyright notice and this permission notice shall be
  156. included in all copies or substantial portions of the Software.
  157. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  158. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  159. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  160. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  161. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  162. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  163. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.