节点表示如何将车把 html 页面呈现到文件
node express how to render handlebars html page to file
我想通过 wkhtmltopdf 将一些 html 页面转换为 pdf。但是,我要转换为 pdf 的 html 页面是使用 handlebars 动态生成的。
所以我认为一种解决方案可能是通过把手生成 html 页面,但生成一个文件(html 文件)。然后,使用 hkhtmltopdf 将该文件转换为 pdf,然后允许用户以某种方式下载 pdf。
所以,我的问题是:如何将(把手)动态生成的 html 页面呈现到文件中?
谢谢再见...
创建文件的简单示例。
var Handlebars = require('handlebars');
var source = "<p>Hello, my name is {{name}}. I am from {{hometown}}. I have " +
"{{kids.length}} kids:</p>" +
"<ul>{{#kids}}<li>{{name}} is {{age}}</li>{{/kids}}</ul>";
var template = Handlebars.compile(source);
var data = { "name": "Alan", "hometown": "Somewhere, TX",
"kids": [{"name": "Jimmy", "age": "12"}, {"name": "Sally", "age": "4"}]};
var result = template(data);
var fs = require('fs');
fs.writeFile("test.html", result, function(err) {
if(err) {
return console.log(err);
}
});
上面来自 Alex 的代码完美无缺。然而,我的困惑是:我使用的是 'express-handlebars' 而不是 'handlebars'。现在,我能理解的是 Express-Handlebars 是我正在使用的 Express 应用程序的 Handlebars 实现。我只是没有找到在 Express-Handlebars 中使用 'compile()' 方法的方法,所以我最终安装了 Handlebars(独立)并用它来编译我的 (html) 模板并将结果保存到磁盘,正如 Alex 上面解释的那样。
总结:
1) 我知道 Express-Handlebars 是 Express 应用程序的 Handlebars。
2)我不知道如何使用 express-handlebars 的 "compile()" 方法,所以我最终安装了 Handlebars(来自 npm)并在服务器上使用它来生成我的 html 文件(来自模板) 并将其保存到磁盘。
3) 当然,我到处都安装并使用了 Express-Handlebars 来在我的 Express 应用程序中提供我的页面;刚刚安装了 Handlebars 以使用 "compile()" 方法生成我的 html(在服务器中)并将结果保存到磁盘。
希望这是可以理解的。再次感谢,再见...
使用 express-handlebars
,您应该使用高级模式并像 this example 中那样创建它的实例。
正确的方法是创建一个视图文件(就像你可能已经有的问题一样)并使用 express handlebars 实例来呈现它:
// init code
var exphbs = require('express-handlebars');
var hbs = exphbs.create({
defaultLayout: 'your-layout-name',
helpers: require("path-to-your-helpers-if-any"),
});
app.engine('.file-extention-you-use', hbs.engine);
app.set('view engine', '.file-extention-you-use');
// ...then, in the router
hbs.render('full-path-to-view',conext, options).then(function(hbsTemplate){
// hbsTemplate contains the rendered html, do something with it...
});
HTH
我想通过 wkhtmltopdf 将一些 html 页面转换为 pdf。但是,我要转换为 pdf 的 html 页面是使用 handlebars 动态生成的。
所以我认为一种解决方案可能是通过把手生成 html 页面,但生成一个文件(html 文件)。然后,使用 hkhtmltopdf 将该文件转换为 pdf,然后允许用户以某种方式下载 pdf。
所以,我的问题是:如何将(把手)动态生成的 html 页面呈现到文件中?
谢谢再见...
创建文件的简单示例。
var Handlebars = require('handlebars');
var source = "<p>Hello, my name is {{name}}. I am from {{hometown}}. I have " +
"{{kids.length}} kids:</p>" +
"<ul>{{#kids}}<li>{{name}} is {{age}}</li>{{/kids}}</ul>";
var template = Handlebars.compile(source);
var data = { "name": "Alan", "hometown": "Somewhere, TX",
"kids": [{"name": "Jimmy", "age": "12"}, {"name": "Sally", "age": "4"}]};
var result = template(data);
var fs = require('fs');
fs.writeFile("test.html", result, function(err) {
if(err) {
return console.log(err);
}
});
上面来自 Alex 的代码完美无缺。然而,我的困惑是:我使用的是 'express-handlebars' 而不是 'handlebars'。现在,我能理解的是 Express-Handlebars 是我正在使用的 Express 应用程序的 Handlebars 实现。我只是没有找到在 Express-Handlebars 中使用 'compile()' 方法的方法,所以我最终安装了 Handlebars(独立)并用它来编译我的 (html) 模板并将结果保存到磁盘,正如 Alex 上面解释的那样。
总结: 1) 我知道 Express-Handlebars 是 Express 应用程序的 Handlebars。 2)我不知道如何使用 express-handlebars 的 "compile()" 方法,所以我最终安装了 Handlebars(来自 npm)并在服务器上使用它来生成我的 html 文件(来自模板) 并将其保存到磁盘。 3) 当然,我到处都安装并使用了 Express-Handlebars 来在我的 Express 应用程序中提供我的页面;刚刚安装了 Handlebars 以使用 "compile()" 方法生成我的 html(在服务器中)并将结果保存到磁盘。
希望这是可以理解的。再次感谢,再见...
使用 express-handlebars
,您应该使用高级模式并像 this example 中那样创建它的实例。
正确的方法是创建一个视图文件(就像你可能已经有的问题一样)并使用 express handlebars 实例来呈现它:
// init code
var exphbs = require('express-handlebars');
var hbs = exphbs.create({
defaultLayout: 'your-layout-name',
helpers: require("path-to-your-helpers-if-any"),
});
app.engine('.file-extention-you-use', hbs.engine);
app.set('view engine', '.file-extention-you-use');
// ...then, in the router
hbs.render('full-path-to-view',conext, options).then(function(hbsTemplate){
// hbsTemplate contains the rendered html, do something with it...
});
HTH