包括其他视图文件夹中的哈巴狗mixin [英] Include pug mixin from other view folder
问题描述
我在我的express应用程序中设置了多个视图路径:
I have set multiple view paths in my express application:
express.set('views',['/path1', '/path2', '/path3']);
渲染视图时,我想在path1
中包含帕格文件格式path2
.
When I am rendering my view, I want to include the pug file form path2
in path1
.
# /path2/index.pug
include path1/mixin.pug
我找不到解决此问题的方法.
I can not find a solution for this problem.
推荐答案
我没有使用express.set视图.
I didn't use express.set views.
只需在您的哈巴狗文件中执行此操作
Just do this in your pug file
包括../path1/mixin.pug (或者没有.pug也可以工作),例如include ../path1/mixin
include ../path1/mixin.pug (or without .pug can also work) eg include ../path1/mixin
对于那些对此不赞成的人,你不知道乔恩·雪诺.
For those that down voted this, you know nothing jon snow.
我的设置:
文件:根项目文件夹中的index.js [注意,没有app.set('views',path.join(__ dirname,'view'))]
My setup:
File: index.js in root project folder [NOTE there's no app.set('views', path.join(__dirname, 'view'))]
const express = require('express')
const app = express()
const PORT = process.env.PORT || 3000
app.use(express.static('public'))
app.set('view engine', 'pug')
// routing
app.use('/', require('./home/route'))
app.listen(PORT, console.log(`Server started on port ${PORT}`))
文件夹:视图(在根文件夹内)
Folder: view (inside root folder)
文件夹:部分(内部视图文件夹,例如root/view/part)
Folder: part (inside view folder eg root/view/part)
文件:mixin.pug(在内部视图文件夹中,例如root/view/part/mixin.pug
File: mixin.pug (inside view folder eg root/view/part/mixin.pug
mixin pet(name)
li.pet= name
文件夹:模板(内部视图文件夹,例如root/view/template)
Folder: template (inside view folder eg root/view/template)
文件:main.pug(在root/view/template/main.pug中)
File: main.pug (in root/view/template/main.pug)
doctype html
html
head
body
h1 Hello template/main.pug
block content
文件夹:home(在根文件夹内,例如root/home)
Folder: home (inside root folder eg root/home)
文件:route.js(位于主文件夹内,例如root/home/route.js)
File: route.js (inside home folder eg root/home/route.js)
const express = require('express')
const app = module.exports = express()
app.get("/",(req,res)=>{
res.render('../home/index')
})
文件:index.pug(在主文件夹内,例如root/home/index.pug)
File: index.pug (inside home folder eg root/home/index.pug)
extends ../view/template/main
block content
h1 Helllllo from home/index.pug
include ../view/part/mixin
+pet('you')
+pet('know')
+pet('nothing')
+pet('jon')
+pet('snow')
如您所见,这是输出localhost:3000
As you can see, this is the output localhost:3000
绝对可以在没有express.set视图的情况下使用,并且带有include ../path1/mixin.pug
Absolute proof that it works WITHOUT express.set views, AND with include ../path1/mixin.pug
工作原理:
- 将index.js根目录文件路由到文件route.js的主目录,拉出文件index.pug.
- index.pug拉模板文件main.pug.然后拉混音.
你什么都不知道,琼恩·雪诺"
"You know nothing jon snow"
这篇关于包括其他视图文件夹中的哈巴狗mixin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!