Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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+.

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.js

var 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