gpt4 book ai didi

javascript - EJS动态包含

转载 作者:搜寻专家 更新时间:2023-10-31 23:00:09 24 4
gpt4 key购买 nike

我正在尝试使用 EJS 动态包含我的部分内容之一。我像这样呈现一个模板:

路由器

router.get('/', noteController.getNotes, (req, res, next) => {
return res.render(template, {'page':'all'})
})

我正在尝试根据变量“page”在此模板中包含一个部分。

模板.ejs

<% include %><%= page %><% .ejs %>

然而,这是行不通的。我尝试了多种方法,但无济于事。非常感谢任何帮助。

最佳答案

简单地:

<%- include(page) %>

或者如果它在 partials 文件夹中:

<%- include('partials/'+page) %>



附言不要忘记将 View 引擎声明为 ejs:

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

来自 official source :

<%- Outputs the unescaped value into the template

You'll likely want to use the raw output tag (<%-) with your includeto avoid double-escaping the HTML output.

关于javascript - EJS动态包含,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44251142/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com