Coder Social home page Coder Social logo

express-ejs-layouts's Introduction

express-ejs-layouts

Layout support for ejs in express

npm version build status

Installation

$ npm install express-ejs-layouts

Example

Check the example folder.

  1. git clone https://github.com/soarez/express-ejs-layouts.git
  2. cd express-ejs-layouts
  3. npm install
  4. node example
  5. Open http://localhost:3000/

Usage

var express = require('express');
var expressLayouts = require('express-ejs-layouts');

var app = express();

app.set('view engine', 'ejs');

app.use(expressLayouts);

app.get('/', function(req, res) {
  var locals = {
    title: 'Page Title',
    description: 'Page Description',
    header: 'Page Header'
  };
  res.render('the-view', locals);
});

app.listen(3000);

contentFor

A view

tyler
<%- contentFor('foo') %>
club
<%- contentFor('bar') %>
fight

With a layout

<%-bar%> <%-foo%>
<%-body%>

Renders

fight club
tyler

As another example, consider this view:

foo
<%- contentFor('pageSectionA') %>
bar
<%- contentFor('pageSectionB') %>
baz

Using it with this layout:

<div class="header"><%- pageSectionA %></div>
<div class="body"><%- body %></div>
<div class="footer"><%-defineContent('pageSectionB')%></div>

Will render:

<div class="header">bar</div>
<div class="body">foo</div>
<div class="footer">baz</div>

Notice that the difference between using <%- pageSectionA %> and <%-defineContent('pageSectionA')%> is that the former will generate an error if the view doesn't define content for this section.

This can be used not only to fill large layout parts, but also for simple stuff, like setting page title:

With this layout:

<html>
  <head>
    <title><%- title %></title>
  </head>
  <body>
    <%- body %>
  </body>
</html>

You can set page title by:

foo
<%- contentFor('title') %>
Foo Page

Or if you would rather set title at the beginning of the view:

<%- contentFor('title') %>Foo Page<%- contentFor('body') %>
foo

Script blocks extraction

If you like to place all the script blocks at the end, you can do it like this:

app.set("layout extractScripts", true)

A view

something<script>somejs<script>something

With a layout

<body>
  <%- body %>
  <%- script %>
</body>

Renders

<body>
  somethingsomething
  <script>somejs<script>
</body>

Enabling individually:

req.render('view', { extractScripts: true })

When the "layout extractScripts" option is activated, scripts defined in views will be extracted (won't be a part of body) and will be available for use in the layout through the variable scripts.

Another example:

This view:

<script src="/b.js" />
<div>foo</div>
<script src="/a.js" />
<div>bar</div>
<script src="/c.js" />

Used with this layout:

<div class="main">
<%- body %>
</div>
<!-- place the scripts at the end of the html page -->
<%- script %>

Will render:

<div class="main">
<div>foo</div>
<div>bar</div>
</div>
<!-- place the scripts at the end of the html page -->
<script src="/b.js" />
<script src="/a.js" />
<script src="/c.js" />

Style blocks extraction

Works exactly like script blocks extraction except:

  • Supported tags are <link rel="stylesheet" …> and <style …>
  • The option is named extractStyles
  • The template variable in layout is style

Meta blocks extraction

Works exactly like script blocks extraction except:

  • Supported tags are <meta …> and <meta …/>
  • The option is named extractMetas
  • The template variable in layout is meta

Set custom default layout

By default 'layout.ejs' is used. If you want to specify your custom layout (e.g. 'layouts/layout.ejs'), just set layout property in express app settings.

app.set('layout', 'layouts/layout');

Set custom layout for single render

Just pass layout as render locals object.

app.get('/', function(req, res) {
  res.render('the-view', { layout: 'specific-layout' });
});

Set no layout for single render

Just pass layout: false as render locals object.

app.get('/', function(req, res) {
  res.render('the-view', { layout: false });
);

Optional sections

In a layout, you can have optional sections using defineContent: Unspecified section content defaults to ''.

1
<%-defineContent('a')%>
2
<%-defineContent('b')%>
3

with a view:

<%- contentFor('a') %>
1.5

will render:

1
1.5
2
3

Running tests

Clone the repo and run:

$ npm test

License

MIT

express-ejs-layouts's People

Contributors

abhishekpaul1 avatar aditya-rb avatar builtinnya avatar carlosrafaelgn avatar chrisumbel avatar danielmsd1 avatar dependabot[bot] avatar goliatone avatar michaelgrigoryan25 avatar naholyr avatar ramin0 avatar soarez avatar whut avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

express-ejs-layouts's Issues

contentFor is not defined in a partials

As the title says contentFor does not work in partials. Works everywhere else but get contentFor is not defined error message when trying to render a page with a partial containing it.

Why a contentFor doesn't have a closeTag?

I am trying to understand why a contentFor doesn't have a closeTag? This is so wierd.. I need to open another contentFor to close the past content.

Is that a way to start a contentFor and close after a few lines?

different layout

Hi. Im using express-ejs-layout for my project. my project has routing. I want use different layout for different res queries. for example if query is: www.xxx.com/a, use LayoutA.ejs, if query is: www.xxx.com/b, use LayoutB.ejs. My index.js part code is:

app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, '/app_server/views'));
app.use(bodyParser.urlencoded({ extended: false }));
// app.use(function(req, res, next){
//   res.locals.currentUser = req.k_adi;
//   next();
// });
app.use(bodyParser.json());
// app.use(function (req, res, next) {
  // res.status(404).send("Sorry can't find that!");
  // next();
// })
// const ctrl = new ctrll();
app.use(ejsLayout);
// public klasörü kamuya açılıyor
app.use('/public', express.static(path.join(__dirname, 'public')));
require('./app_server/routes/routeManager')(app); 

extractScripts and extractStyles don't work as expected

I cloned the repo, did npm install at the root and then launched the server as node example/index.js. When I inspect the source of the rendered page in the browser, both style and scripts are still at their original position in the view instead of being respectively at the top and bottom.

Is this expected?

Passing Variables to layout

Hi,

I'm looking for a way to pass variables to layout, i thought it would work with a regular render i.e.

app.js
res.render('view', { name: value });
layout.ejs
<%= name %>

But it says it's undefined

Technically it passed through layout but calls a view as the body so does that mean that the variables only pass to the actual view or does layout work in a different way?

Why it should be 404 to 500

http://localhost:3000/stylesheets/style2.css 500

var express = require('express');
var path = require('path');
// var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var config = require('./config');
var configRoutes = require('./config.routes');
var expressLayouts = require('express-ejs-layouts');

var app = express();


// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.set("config",config)
// uncomment after placing your favicon in /public
// app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));

app.use(expressLayouts);

app.use(logger(config.debug?'dev':'combined'));


app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: false
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

configRoutes(app); //配置路由

// catch 404 and forward to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

```code 

res.locals not rendering in the layout

I'm using the this module with Express 4.

I'm not sure how to label this, but the issue I have is this. I have a routes file, a layout and a view. I'm trying to pass through a page title and a page description through to the layout using the code below.

Route

router.get('/', function(req, res, next) {

  res.locals.meta = {
    title: 'Page Title',
    description: 'Page Description'
  };

  res.render('pages/index', {
    header: 'Page Header'
  });
});

Layout

<!DOCTYPE html>
<html dir="ltr" lang="en" class="no-js">
<head>
  <title><%= meta.title %></title>
  <meta name="description" content="<%= meta.description %>">
</head>
<body>
  <%- body %>
</body>
</html>

View

<h1 class="page-title"><%= header %></h1>

The header is rendered in the view, but it's missing the page title and page description. If I add other items to the res.locals.meta object and reference them in the view they're rendered, but nothing from the layout is rendered.

What am I doing wrong?

All help is greatly appreciated.

Support for recursive layouts

Say you have a base layout you want to be applied to all pages, but you also have a sub-layout that contains a little extra bloat (such as some extra css files) which is also shared between a few more pages.

Is there any way for this to be achieved?

<!-- base-layout.ejs -->
<html>
<body>
  <nav>Some nav</nav>
  <%- body %>
</body>
</html>
<!-- sub-layout.ejs -->
<div>SOME PAGE BANNER</div> 
<%- body %>
<!-- page.ejs -->
<h1>Page Title</h1>

Expected output

<html>
<body>
  <nav>Some nav</nav>
  <div>SOME PAGE BANNER</div> 
  <h1>Page Title</h1>
</body>
</html>

Including partial ejs files

Hi,

It's ok to add some parts of the code by using defineContent/contentFor and it works successfully.

I also need to include some external ejs files for header, footer in order to keep their codes in separate files. How can I handle it?

no default layout

I had to add app.set('layout', 'layout'); in app.js to make it work.

Missing information on the documentation

I had to check the source code to find out that if you pass false to the layout parameter, you can render the template without using the layout. I would suggest to put it on the README.md as well.

Usage Example:

// Not Found (404)
app.use(function(req, res) {
  res.status(404).render('error-404', { layout: false }); // Don't use any layouts for 404 error pages
});

Wrong ReferenceError in layout

It seems like node prints out the wrong variable name when something is not defined. I had <%= title %> and <%= uid %> and with not passing uid, it gave me a title is not defined error. It also referenced the line title is on. The title ref was first in the file.

content is not defined

when i try to pass locals to both layout and template i get a strange error

layout_content is not defined at eval (eval at compile (C:\Users\Farmy\Documents\github\laboratory_website\bakcend\express\node_modules\ejs-layout\lib\ejs.js:577:12), <anonymous>:10:8) at returnedFn (C:\Users\Farmy\Documents\github\laboratory_website\bakcend\express\node_modules\ejs-layout\lib\ejs.js:606:17) at Object.exports.renderFile (C:\Users\Farmy\Documents\github\laboratory_website\bakcend\express\node_modules\ejs-layout\lib\ejs.js:417:31) at C:\Users\Farmy\Documents\github\laboratory_website\bakcend\express\node_modules\ejs-layout\lib\ejs.js:1103:28 at Object.exports.renderFile (C:\Users\Farmy\Documents\github\laboratory_website\bakcend\express\node_modules\ejs-layout\lib\ejs.js:423:10) at View.exports.main [as engine] (C:\Users\Farmy\Documents\github\laboratory_website\bakcend\express\node_modules\ejs-layout\lib\ejs.js:1100:11)

my code:

router:js:
res.render('index', { currentRoute: 'test', layout_content: req.ejs.layout_content, page_content: custom_content });

layout.ejs:
<title><% layout_content.title %></title> <%- defineContent('body') %>

main.ejs:
<p><% layout_content.text%></p>

Flash messages not showing up

Hi, as a rails developer, I totally appreciate this library. I did notice something weird though - when I configured my application to use a layout, my flash messages stopped working. I'm using express-messages.

My layout app/views/layouts/application.ejs:

<!DOCTYPE html>
<html>
  <% include ../_head %>
  <body>
    <% include ../_header %>

    <%- body %>

    <% include ../_footer %>
  </body>
</html>

Header partial app/views/_header.ejs:

<header>
  <!-- FLASH MESSAGES -->
  <%- messages('_bootstrap_flash_messages') %>

  <!-- SITE TITLE AND NAVIGATION -->
  <h1><a href="/"><%= title %></a></h1>
  <a type="button" class="btn btn-primary pull-right" href="/robots/new">
    <span class="glyphicon glyphicon-plus"></span> new
  </a>
</header>

<h2><%= page_title %></h2>

Bootstrap flash messages partial app/views/_bootstrap_flash_messages.ejs:

<% if(messages){ %>
  <% Object.keys(messages).forEach(function (type) { %>
    <% messages[type].forEach(function (message) { %>
      <div class="alert alert-<%= type %> alert-dismissible" role="alert">
        <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <%= message %>
      </div>
    <% }) %>
  <% }) %>
<% } %>

<div>
  TEST TEST TEST
</div>

Because I'm seeing the content of the header partial but not the text TEST TEST TEST, I think the header partial's <%- messages('_bootstrap_flash_messages') %> function isn't getting invoked but I'm not sure why. Any guidance you can provide is appreciated.

EDIT: even though an attempt to use a custom flash message template, <%- messages('_bootstrap_flash_messages') %>, does not work... an attempt to use the default flash message template,<%- messages() %>, does.

Mix values from diffrent sessions ???

Hi
I try express ejs layouts with passport auth.
when try to show pages that must show for logged in users,
the header section of page shows another user profile ,
but the body of page shows correct value,
how it can happen

Changelog?

Can you set up a changelog file for API changes, bug fixes, and new features?

How i can use this?

Hello. Can you give me more details about contentFor and, Script blocks extraction and <%- script %>
What is <%- script %> ?

Express EJS Layout intercept error

Hello,

I have a strange issue, it seems that each time there is an error in my app (wherever it is), the plugin intercepts the error and print out something really not helping.

It is exactly as this stack overflow question: http://stackoverflow.com/questions/25433238/express-layouts-error-variable-not-defined

Comment say: express-ejs-layouts drives me crazy with this. If anything goes wrong in the controller or view, it reports that the first variable referenced in the view or layout is undefined. Basically I can never trust the error messages I get when using express-ejs-layouts.

And this is exactly the issue I have. It is really hard to debug the app because of that...

Any idea?

Thanks :)

Add <noscript> support for 'extractStyles'

When using

app.set("layout extractStyles", true)

it would be very usefull if styles inside of <noscript> blocks would preserve this fact, otherwise using Fallback styles like this:

<noscript>
    <link rel="stylesheet" href="/assets/css/noscript.css" type="text/css" />
</noscript>

becomes impossible (without using hacks like removing the style with JS)

- Stevetec

Readme typo

<body>
<%- body %>
<%- scripts %>
</body>

scripts should be script

I might need to pass a `options` parameter

My current template directory structure

.
├── bind
└──── login.ejs
└──── home.ejs
└── template
    ├── error.ejs
    └── layer
        ├── header.ejs
        └── layout.ejs

app.ts

import expressLayouts from 'express-ejs-layouts'
App.set('views', path.join(__dirname, 'views'))
App.set('view engine', 'ejs')
App.set('layout extractScripts', true)
App.set('layout extractStyles', true)
App.use(expressLayouts)

According to the example given in your template, I can only create a layout.ejs in the views directory, and then all the .ejs templates in the same directory inherit this directory?

image

Can I set layout.ejs to my current template/layer/layout.ejs (in the views directory)?
Of course, the template file in my bind directory can inherit the pattern of layout.ejs

Can you answer it, is it wrong with me? Thank you.

Does not extract inline script blocks

If I have an ejs file with the following in it

<script src="i-need-a-file.js"></script>
<script>
    // I'm a script block
</script>
<script type="application/javascript" src="i-need-another-file.js"></script>

Then the first and last script elements are extracted, but the middle one with inline script remains in place so that now the script blocks are out of sequence when ejs-layout adds the extracts script blocks to the end of page.

The layout is

<html>
    <body>
        <!-- Other stuff here -->
        <script src="/jquery/jquery-2.1.4.js"></script>
        <script src="/bootstrap4/js/bootstrap.js"></script>

        <%- script %>
   </body>
</html>

Chinese characters in layout.ejs become messy when rendered

Here is a snippet of code in layout.ejs
...

<li class="active"><a href="/">首页</a></li>
<li><a href="/login">登入</a></li>
<li><a href="/reg">注册</a></li>

...

But those Chinese characters become this after rendered (open in browser):
...

<li class="active"><a href="/">��ҳ</a></li>
<li><a href="/login">����</a></li>
<li><a href="/reg">ע��</a></li>

...

Meanwhile, Chinese characters in other(not layout files) ejs files don't have this problem.

What's the problem? thx in advance.

[question]: using ejs for the layout in an Angular app

I have an Angular project. I am trying to use ejs just for the layout of the Angular project. This is because I want to serve dynamic scripts and link tags from express server to the section.

That is all I need it for and after that I am good with the HTML and Angular views.

Your project doesn't by chance help me here does it? It looks like this is still an all ejs repo.

I am struggling to mix ejs with Angular! Thanks

Pass variables to layout

Hi, I read code example. And I found the way to pass variables to layout:

res.locals = {
    title: 'Example',
    message: 'This is a message'
  };

It seem lost old res.locals variables. But I can resolve it by:

let locals = res.locals;
res.locals = {
    ...locals
    title: 'Example',
    message: 'This is a message'
  };

But It looks pretty complicated. So, can pass variables to layout by res.render('views', {variables})?;

extractStyles not working when script tag is in partial

When including a partial in a view and the partial has script tags inside it, they don't get extracted to the end of the page. Is this a bug or am I supposed to do it another way? I've enabled the layout extractStyles option in app.set

I have an issue with the examples.

I have a masterpage.html, like so:

<html>
<head>
...
</head>
<body>
...
<!-- I want the content inserted here -->
...
</body>
</html>

and a content.html like:

`<div>
 Bla bla bla
</div>`

So the client requests content.html and should get the master page with the content inserted.
How do I do this? The examples don't really help me. Thanks.

Cryptic error message

Setting up a simple example project:

//...

app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');
app.set('layout', __dirname + '/views/layouts/default.ejs');
app.set('layout extractScript', true);
app.set('layout extractStyles', true);

//...

When rendering a simple page, I was getting 'script' is not defined (Referring to <%- script %> in the layout file)

I found that the issue was that I set 'extractScript', and not 'extractScripts', but the error message is very cryptic for newcomers. Shouldn't it be something like:

"you need to set 'layout extractScripts' to true before using <%- script %>"

Not critical by any means, but it took me a while to find the typo. It would be nice to get that half an hour of my life back.

Empty view file.

Hello i try to start work with ur extension.
I create layout and use them in created express app.
There is my layout.ejs
<%- include('partials/header') -%>
<%- include('partials/footer') -%>

This is my route file
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express',layout: 'layout' });
});

module.exports = router;

I try to write in index.ejs something and don`t see anything, just what i have in header and footer. (ejs files) can u help me and tell what i am doing wrong?
Thank.

Support express-mailer which use app.render

Hey,

I'm using express-mailer to send emails via node worker. The emails are rendered by app.render (instead of res.render) and therefore not processing the layout middleware.

Is there any possible way to make the layouts work with app.render?

Thanks,
Gil.

Variables aren't available in the layout

For example, I'm trying to use <title><%= title %></title> in the layout but I get a reference error: title is undefined

Even though I'm passing in title to the render function and <%= title %> is accessible from the view itself

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.