我试着渲染EJS代码,我从home.js
发送到home.ejs
作为一个字符串,但它不起作用。我现在正在尝试,但似乎没有任何工作。我的代码是:EJS不在客户端渲染ejs代码
home.js
var templates = {};
templates.onScroll = fs.readFileSync('views/partials/onScrollAppendPost.ejs', 'utf-8');
res.render("users/home", {templates: templates});`
home.ejs
var template = new EJS({text: <%- JSON.stringify(templates.onScroll) %>}).update('#mainContainer', {postArr: postArr});
编辑:
什么即时试图做的是我要检测当用户到达页面底部时。使用此代码:
$(window).scroll(function() {
if ($(window).scrollTop() == $(document).height() - $(window).height()) {
//get post data and append post
}});
当用户到达页面的底部时,我想将ejs代码添加到页面。基本上我想在ejs和js之间进行沟通。
说实话,我从来没有见过这样的代码,但也许我只是无知。你是如何学习它的?你能提供一些使用类似代码的教程或例子吗? –
这些模板需要在服务器本身上编译,浏览器无法编译ejs文件AFAIK。你需要使用模板引擎 –
[检查此](https://coligo.io/templating-node-and-express-apps-with-ejs/) –