我有一个Handlebars.js模板,如下所示,我需要将option值设置为从nodejs呈现的数据中动态选择的值
<select class="form-control" name="c_level" value="{{course.c_level}}" required>
{{#levelc course.c_level}}
{{#each level}}
<option value="{{this.level}}" name = "c_level">{{this.level}}</option>
{{/each}}
{{/levelc}}
</select>
以上代码的输出为
<select class="form-control" name="c_level" value="C Level" required="">
<option value="C Level" selected="">C Level</option>
<option value="Safety" name="c_level">Safety</option>
<option value="D Level" name="c_level">D Level</option>
<option value="C Level" name="c_level">C Level</option>
<option value="B Level" name="c_level">B Level</option>
<option value="A Level" name="c_level">A Level</option>
<option value="Customer Courses" name="c_level">Customer Courses</option>
<option value="Special Courses" name="c_level">Special Courses</option>
</select>
我的助手js文件是
Handlebars.registerHelper('levelc', function( value, options ){
var $el = $('<select />').html( options.fn(this) );
$el.find('[value="' + value + '"]').attr({'selected':'selected'});
return $el.html();
});
我的声明了助手的服务器js文件是
const exphbs = require('express-handlebars');
app.engine('hbs', exphbs({ extname: 'hbs', helpers: require("./helpers/handlebars").helpers, defaultLayout: 'mainLayout', layoutsDir: __dirname + '/views/layouts/' }));
该html部分的预期输出是应将选择属性“ C Level”的值设置为该选项上的选定值
<select class="form-control" name="c_level" value="C Level" required="">
<option value="Safety" name="c_level">Safety</option>
<option value="D Level" name="c_level">D Level</option>
<option value="C Level" name="c_level" selected>C Level</option>
<option value="B Level" name="c_level">B Level</option>
<option value="A Level" name="c_level">A Level</option>
<option value="Customer Courses" name="c_level">Customer Courses</option>
<option value="Special Courses" name="c_level">Special Courses</option>
</select>
我收到一条错误消息,我无法在两天内弄清楚
(function (exports, require, module, __filename, __dirname) {
Handlebars.registerHelper('levelc', function( value, options ){
^
ReferenceError: Handlebars is not defined