我想将我的note.js(包含一个函数)包含到ejs中。
“ ejs文件将在服务器端呈现,而js文件将在客户端呈现。您可以将数据从ejs传递到js,但不能从js传递回ejs。”
我找到了一些在ejs中定义javasript函数的解决方案,但没有任何效果。现在,我尝试了Abrar Hossain EJS include functions defined in a separate ejs file的解决方案,但出现了错误:
助手没有定义
(因为)我不确定要将哪些内容添加到helpers.js中,也许调用是错误的?
我得到了控制台日志“是”,所以路径是正确的。
index.js:
// include helpers.js for note.js
const ejs_helpers = require('./public/js/helpers.js');
.get('db', .... {
....
....
}
res.render('db', results, { helpers:ejs_helpers });
.ejs:
<%- helpers.note(note.js); %>
helpers.js:
/* ejs_helpers.js - to use external scripts */
console.log("yeah!");
exports = module.exports = {};
exports.iif = function(cond,value){
if(cond) return value;
return '';
}