Giter Club home page Giter Club logo

express-ejs-extend's Introduction

Express EJS Extend     Donate

Layouts support for EJS templates in Express 3+.

Usage

First, add it as engine in your app:

// 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:

<%# views/index.ejs %>

<% extend('layout') %>

<h1>Hello <%= title %></h1>
<%# views/layout.ejs %>

<!DOCTYPE html>
<html>
<body>
<%- content %>
</body>
</html>

That's all, your GET / will render:

<!DOCTYPE html>
<html>
<body>
<h1>Hello world!</h1>
</body>
</html>

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.

express-ejs-extend's People

Contributors

bryanjhv avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.