在server.js
我有這樣的事情:從快車車把內使用helper返回HTML格式的代碼
var express = require('express');
var path = require('path');
var exphbs = require('express-handlebars');
var app = new express();
var handlebars = require('./helpers.js')(exphbs);
app.set('views', path.join(__dirname, 'views'));
app.engine('.hbs', handlebars.engine);
app.set('view engine', '.hbs');
app.set ('port', (process.env.PORT || 5000));
app.get('/', function(req,res){
res.render('home', {
title: 'Express App with Handlebars templates',
content: 'This is some content',
persons: [
{
name: 'branchito',
instrument: 'guitar',
},
{
name: 'joe',
instrument: 'flute',
},
]
});
});
app.listen(app.get('port'), function(){
console.log('Server started on port ' + app.get('port'));
})
然後在我helpers.js
文件:
function hbsHelpers(hbs) {
return hbs.create({
helpers: { // This was missing
inc: function(value, options) {
console.log('reading it');
return parseInt(value) + 1;
},
strong: function(text) {
return '<strong>' + text + '</strong>';
}
// More helpers...
}
});
}
module.exports = hbsHelpers;
但隨後所產生的輸出strong
幫手將其作爲<strong>...</strong>
呈現。我將如何讓該字符串未轉義,我知道有Handlebars.SafeString()
,但不知道如何使用它從這個快速句柄實例中。