如何防止express-ejs-layouts包装其他页面? [英] How can I prevent express-ejs-layouts for wrapping my other page?

查看:37
本文介绍了如何防止express-ejs-layouts包装其他页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为node.js找到页面布局,例如 laravel php 都有其模板布局,这是完美的.我想在node.js中实现它,最后找到这个 express-ejs-layouts ,但是有一个问题,我在他们的文档中看不到布局会包装我的所有页面,特别是我的登录页面和注册页面,它们具有不同的页眉和页脚.我们如何防止express-ejs-layouts包装我的其他页面?

I'm finding a page layout for node.js like laravel php have their Template for layout and it is perfect. I want to achieve it here in node.js and finally found this express-ejs-layouts but there is a problem in it that I cant see in their documentation the layout will wrap all of my pages specially my signin and signup page which have a different header and footer. How can we prevent express-ejs-layouts from wrapping my other page?

const express = require('express');
const router = express.Router();
const path = require('path');
const multer = require('multer');
const bodyParser = require('body-parser');
const mysql = require('mysql');
const app = express();
const server = require('http').createServer(app);
const expressLayouts = require('express-ejs-layouts');

// Set Database Connection
const connection=mysql.createConnection({
    host:'localhost',
    user:'root',
    password:'',
    database:'project_101'
});

connection.connect(function(error){
    if(!!error) console.log(error);
    else console.log('Database Connected!');
});

app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.static('assets'));
app.use(bodyParser.json());       
app.use(bodyParser.urlencoded());
app.use(bodyParser.urlencoded({extended: true}));
app.use(expressLayouts);

app.get('/',(req, res) => {
    let sql = "SELECT * FROM uploads";
    let query = connection.query(sql, (err, rows) => {
        if(err) throw err;
        res.render('index');
    });
});

app.get('/signup', (req, res) => {
    res.render('signup');
});

app.get('/signin', (req, res) => {
    res.render('signin');
});

app.get('/unknown-user', (req, res) => {
    res.render('unknown-user');
});

app.get('/profile', (req, res) => {
    res.render('profile');
});

const port = process.env.PORT || 3000;

// Server Listening
server.listen(port, function () {
    console.log('Server successfully running at: -',port);
});

推荐答案

最终得到了问题的解决方法

Finally got the solution for the problem express-ejs-layouts

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

app.use(expressLayouts);

您只需要声明页面为布局并将其设置为false.

You just need to declare your page as a layout and set it to false.

app.set("layout signin", false);

并呈现页面和布局.

and render the page together with the layout.

app.get('/signin', (req, res) => {
    res.render('signin', { layout: 'signin' });
});

ez fix⚡️

这篇关于如何防止express-ejs-layouts包装其他页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆