Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js PugJs的模板布局_Node.js_Pug - Fatal编程技术网

Node.js PugJs的模板布局

Node.js PugJs的模板布局,node.js,pug,Node.js,Pug,我将NodeJs与handlebar一起使用,并考虑切换到PugJs,因为有些功能是本机的-使用handlebar需要一些助手功能/部分 在车把中,我必须定义布局并在模板中传递。在PugJs中,我创建了两条示例路线 第一个路由文件: const express = require('express'); const router = express.Router(); router.get('/', (req, res) => { res.render('index', {

我将NodeJs与handlebar一起使用,并考虑切换到PugJs,因为有些功能是本机的-使用handlebar需要一些助手功能/部分

在车把中,我必须定义布局并在模板中传递。在PugJs中,我创建了两条示例路线

第一个路由文件:

const express = require('express');
const router = express.Router();

router.get('/', (req, res) => {
    res.render('index', {
        title: 'Home',
        template: 'main'
    });
});

module.exports = router;
const express = require('express');
const router = express.Router();

router.get('/', (req, res) => {
    res.render('index', {
        title: 'Page2',
        template: 'pageTwo'
    });
});

module.exports = router;
第二个路由文件:

const express = require('express');
const router = express.Router();

router.get('/', (req, res) => {
    res.render('index', {
        title: 'Home',
        template: 'main'
    });
});

module.exports = router;
const express = require('express');
const router = express.Router();

router.get('/', (req, res) => {
    res.render('index', {
        title: 'Page2',
        template: 'pageTwo'
    });
});

module.exports = router;
正如您所看到的,我总是必须呈现我的索引文件,并将所需的pug文件作为模板变量传入

index.pug

doctype html
html
  include ./header.pug
  body
    include ./templates/#{template}.pug
head
  title #{title} template!
script(src='./client/main.js')
h1 main content here
p pageTwo
头.帕格

doctype html
html
  include ./header.pug
  body
    include ./templates/#{template}.pug
head
  title #{title} template!
script(src='./client/main.js')
h1 main content here
p pageTwo
main.pug

doctype html
html
  include ./header.pug
  body
    include ./templates/#{template}.pug
head
  title #{title} template!
script(src='./client/main.js')
h1 main content here
p pageTwo
pageTwo.pug

doctype html
html
  include ./header.pug
  body
    include ./templates/#{template}.pug
head
  title #{title} template!
script(src='./client/main.js')
h1 main content here
p pageTwo
在渲染pug文件时,我遇到了此错误

eNote:没有这样的文件或目录,请打开 “…\views\templates\#{template}.pug”


如何用正确的模板变量替换
#{template}

动态模板选择不是pug的功能,我相信它与pug如何将所有内容预编译成JavaScript函数有关,该函数在运行时保留在node.js的内存中。这样做的好处是超快速的页面呈现,所以我们很乐意继续使用帕格,并解决这个问题

说到这里,你可以通过以下方式完成你想做的事情:


您还应该研究如何简化将标头添加到模板中的过程,以及如何在模板之间恢复代码。您可能会发现,通过重新设计,这些功能可以为您的需求提供更好的解决方案。

动态模板选择不是pug的功能,我相信它与pug如何将所有内容预编译成JavaScript函数有关,该函数在运行时保留在node.js的内存中。这样做的好处是超快速的页面呈现,所以我们很乐意继续使用帕格,并解决这个问题

说到这里,你可以通过以下方式完成你想做的事情:


您还应该研究如何简化将标头添加到模板中的过程,以及如何在模板之间恢复代码。您可能会发现,通过重新设计,这些功能可以为您的需求提供更好的解决方案。

Pug不支持动态
include
语句,不幸的是。嗯,似乎没有机会使用它……Pug不支持动态
include
语句,不幸的是,嗯,看来没有机会使用它了。。。