Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bryanjhv/express-ejs-extend
Layouts support for EJS templates in Express 3+.
https://github.com/bryanjhv/express-ejs-extend
ejs express-middleware nodejs
Last synced: about 1 month ago
JSON representation
Layouts support for EJS templates in Express 3+.
- Host: GitHub
- URL: https://github.com/bryanjhv/express-ejs-extend
- Owner: bryanjhv
- License: mit
- Created: 2016-11-18T21:13:31.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-06-04T06:38:18.000Z (over 6 years ago)
- Last Synced: 2024-10-29T01:13:59.082Z (about 2 months ago)
- Topics: ejs, express-middleware, nodejs
- Language: JavaScript
- Size: 3.91 KB
- Stars: 9
- Watchers: 3
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Express EJS Extend [![Donate][paybtn]][paylnk]
Layouts support for [EJS][ejs] templates in [Express 3+][express].
## Usage
First, add it as engine in your app:
```js
// server.jsvar express = require('express'),
path = require('path');var app = express();
app.engine('ejs', require('express-ejs-extend')); // add this line
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));app.get('/', function (req, res) {
res.render('index', {title: 'world!'});
});app.listen(3000);
```Finally, in your views, call the `extend` function:
```ejs
<%# views/index.ejs %><% extend('layout') %>
Hello <%= title %>
``````ejs
<%# views/layout.ejs %><%- content %>
```
That's all, your `GET /` will render:
```html
Hello world!
```
### Notes:
* In your view (the one that calls `extend`), you can surround the `extend` with
any combination of (`<%` or `<%-`) and (`%>` or `-%>`).
* In your layout, it's **recommended** to use exactly `<%- content %>` so the
engine doesn't escape your HTML tags. Also you can end it with `-%>` instead
to avoid any trailing `\n`.
* The signature of `extend` is: `extend(layout[, data])`, so you can also pass
an object with data and that will be passed to the layout.## License
This project is released under the [MIT license](LICENSE.txt).
[ejs]: http://ejs.co
[express]: http://expressjs.com[paybtn]: https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif
[paylnk]: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=DVB7SSMVSHGTN