Engine agnostic helpers for express view rendering.
Engine agnostic view rendering helpers for express.
npm install express-compose
There are two ways of using it. The simplest way is to call the methods
decorate(). They have almost the same signature than the express
render() method: they're fed with a route result as an extra parameter in first position.
The second way is more elegant and preserves the express
render() signature. It happens in two times: first the result is embedded in an object using the
wrap() method. Then the object
decorate() methods can be used to achieve the same results.
// madatory step: require the modulevar compose = require'express-compose';// simple waycomposerepeatres template options callback;// elegant waycomposewrapresrepeattemplate options callback;
The interest of the elegant way is that you can provide some options to the wrapper itself. Then the usage is:
composewrapres wrapOptionsrepeattemplate viewOptions callback;
It's similar to rendering, the difference being that instead of providing a option hash, you provide an array of hashes. The view will be rendered for each hash and the results will be concatenated.
// ...// setup of the app, some paths and rendering engine// ...var users = name: 'tobi' name: 'paul' name: 'aure' ;appusecomposewrapresrepeat'views/user' users;;
It's similar to rendering, the difference being that instead of providing a single template, you provide an array of them. The first template will be rendered, passed along with original options to the second one, which in turn will be ... until all templates are rendered.
// ...// setup of the app, some paths and rendering engine// ...var user = name: 'tobi' ;appusecomposewrapresdecorate'views/user' 'views/box' user;;// The `box` template gets the following object:name: 'tobi' content: resrender'view/user' user
Propagation of the original options to outer templates is very handy: It makes dead easy to set a title in a layout using the user properties, for example.
And if you don't like the default key name, you can change it when wrapping the result:
// ...// setup of the app, some paths and rendering engine// ...var user = name: 'tobi' ;appusecomposewrapres compose: 'rendered' decorate'views/user' 'views/box' user;;// The `box` template now gets the following object, with the customized key:name: 'tobi' rendered: resrender'view/user' user
# Something likesudo apt-get install jscoverage# orbrew install jscoverage
Install dev deps:
npm install -d
Run the tests:
(The MIT License)
Copyright (c) 2012 Aurélien Scoubeau
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.