Skip to content

Provides current request URL as property to templates (views).

License

Notifications You must be signed in to change notification settings

zahard/template-url-middleware

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

template-url-middleware

Middleware for Express framework that provides current request URL as property to render engine. So it can be accessed in any template without passing it in each response.render() call

Install

npm i template-url-middleware --save

Example

Lets assume we have app.js file with application and a view directory with file home.ejs (or you can use any other template engine)

// app.js 
const express = require('express');
const urlMiddleware = require('template-url-middleware');

const app = express();
app.set('view engine', 'ejs');
app.set('views', 'views');

// Prodive current path to all templates globally
// Provided string will be used as name of template property (default is 'path')
app.use(urlMiddleware('path'));
app.use('/home', (res, res) => {
  res.render('home', {
    myProperty: 'Hello'
  });
});
app.listen();

After you will be able to refference this path property in templates

<!-- Fragment of template views/home.ejs -->
<ol>
  <li><%= path %></li> <!-- will resolve to  '/home' -->
  <li><%= myProperty %></li>
<ol>

About

Provides current request URL as property to templates (views).

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published