【问题标题】:rendering returned html code to be shown as a view渲染返回的 html 代码以显示为视图
【发布时间】:2019-10-30 14:01:48
【问题描述】:

所以我将当前日期保存在 cookie 中,并尝试使用模板引擎在 html 网页中显示此 cookie 的值。问题是当我发送包含日期的新模板时,我得到的是 HTML 文本而不是视图。

router.get('/', (req, res) => {
    let template = fs.readFileSync(indexFile, 'utf8');
    var dataToShow = {"cookie_data": 'Letzte Besuch: ' + now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds()};
    template = mustache.render(template, dataToShow);
    res.send(template);
})

在我的 HTML 页面中,我有以下内容:

<p>{{cookie_data}}</p>

我希望有一个视图而不是 html 代码

【问题讨论】:

    标签: node.js express mustache template-engine


    【解决方案1】:

    请尝试使用

    <p>{{{cookie_data}}}</p>
    

    请参考此文档:http://handlebarsjs.com/#html-escaping

    请使用

    res.render('page', 'params')
    

    【讨论】:

    • 他为什么要那样做?那有什么作用?它如何回答这个问题? stackoverflow.com/help/how-to-answer
    • 实际上它已经工作了,但我的问题是res.send(template) 返回一个 html 代码,而不是有一个视图,我最终将 html 代码作为纯文本
    • res.send() 的预期行为。它发送任何数据,然后渲染它。我认为您正在寻找类似 res.render('/route-to-the-page', anyData)
    猜你喜欢
    • 1970-01-01
    • 2013-03-04
    • 2016-12-16
    • 2015-03-15
    • 2018-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多