【问题标题】:using data from script file in handlebars template在车把模板中使用脚本文件中的数据
【发布时间】:2019-10-31 10:47:39
【问题描述】:

我正在开发 node.js/express 应用程序,我正在使用把手作为模板引擎。我知道如何使用 res.render('results',{data}) 将数据发送到 hbs 模板,但在这种情况下这是不可能的。我试图在车把模板页面上显示在 script.js 中生成的一些数据,但我做不到。

我已尝试包含

<script id="selected-analysis" type="text/x-handlebars-template">
 {{#if visibility}}
  {{city}}
 {{/if}}
</script>

<div class="content-placeholder"></div>

results.hbs 页面和 script.js 页面上的代码

let theTemplateScript = document.getElementById("selected-analysis").innerHTML;
let theTemplate = Handlebars.compile(theTemplateScript);
let display = {
  "visibility":true,
  "city":'beo'
}

let theCompiledHtml = theTemplate(display)
document.querySelector('.content-placeholder').innerHTML += theCompiledHtml

在 main.hbs 我有

<script src="/dist/handlebars-v4.4.3.js"charset="utf-8"></script>

在我的 app.js 文件中,我设置了车把中间件

app.engine('.hbs', exphbs({
 defaultLayout:'main',
 helpers:helpers,
 extname:'.hbs'
}))
app.set('view engine', 'hbs')

谁能告诉我我做错了什么。

谢谢

【问题讨论】:

    标签: handlebars.js


    【解决方案1】:

    请尝试在 app.js 中使用 app.set('view engine', '.hbs');

    请修改app.js如下

    var express = require('express');
    var exphbs  = require('express-handlebars');
    
    var app = express();
    
    app.engine('.hbs', exphbs({
    defaultLayout:'main',
     helpers:helpers,
     extname:'.hbs'}));
    app.set('view engine', '.hbs');
    

    【讨论】:

    • 感谢您的评论,但这实际上与我提出的问题无关
    猜你喜欢
    • 2014-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-07
    相关资源
    最近更新 更多