【问题标题】:Include pug mixin from other view folder包括来自其他视图文件夹的 pug mixin
【发布时间】:2019-02-01 13:59:31
【问题描述】:

我在我的 express 应用程序中设置了多个视图路径:

express.set('views',['/path1', '/path2', '/path3']);

当我渲染视图时,我想在path1 中包含pug 文件形式path2

# /path2/index.pug

include path1/mixin.pug

我找不到这个问题的解决方案。

【问题讨论】:

标签: node.js express include pug mixins


【解决方案1】:

我没有使用 express.set 视图。

只需在您的 pug 文件中执行此操作

包括 ../path1/mixin.pug (或者没有 .pug 也可以)例如包含 ../path1/mixin

编辑:

对于那些投反对票的人,乔恩雪诺你什么都不知道。

我的设置:
文件:根项目文件夹中的 index.js [注意没有 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}`))

文件夹:查看(根文件夹内)

文件夹:part(在视图文件夹内,例如 root/view/part)

文件:mixin.pug(在视图文件夹内,例如 root/view/part/mixin.pug

mixin pet(name)
  li.pet= name

文件夹:模板(在视图文件夹内,例如根/视图/模板)

文件:main.pug(在 root/view/template/main.pug 中)

doctype html
html
    head
    body
        h1 Hello template/main.pug
        block content

文件夹:home(在根文件夹内,例如 root/home)

文件:route.js(在主文件夹内,例如 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)

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

绝对证明它可以在没有 express.set 视图和 include ../path1/mixin.pug 的情况下工作

它是如何工作的:

  • 根 index.js 文件路由到文件夹 home 到文件 route.js,拉取文件 index.pug。
  • index.pug 拉取模板文件 main.pug。然后拉出 mixin。

“你什么都不知道乔恩·斯诺”

【讨论】:

    猜你喜欢
    • 2017-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多