简体   繁体   English

EJS不在客户端渲染ejs代码

[英]EJS not rendering ejs code on client side

I tried rendering EJS code that I sent from home.js to home.ejs as a string but it didn't work. 我试图渲染,我发自EJS代码home.jshome.ejs作为一个字符串,但没有奏效。 I'm trying this now but nothing seems to work. 我现在正在尝试此操作,但似乎没有任何效果。 My code is: 我的代码是:

home.js home.js

var templates = {};
templates.onScroll = fs.readFileSync('views/partials/onScrollAppendPost.ejs', 'utf-8');
res.render("users/home", {templates: templates});`

home.ejs home.ejs

 var template = new EJS({text: <%- JSON.stringify(templates.onScroll) %>}).update('#mainContainer', {postArr: postArr});

Edit: 编辑:

What im trying to do is I want to detect when a user gets to the bottom of the page. 我想做的是我想检测用户何时到达页面底部。 With this code: 使用此代码:

$(window).scroll(function () {
        if ($(window).scrollTop() == $(document).height() - $(window).height()) {
//get post data and append post
}});

When the user gets to the bottom of the page i want to append ejs code to the page. 当用户到达页面底部时,我想将ejs代码附加到页面上。 Basically i want to communicate between ejs and js. 基本上我想在ejs和js之间进行通信。

Based on the example at http://ejs.co , you first need to include a browser build of ejs in your page: 根据http://ejs.co上的示例,您首先需要在页面中包含ejs的浏览器版本:

<script src="ejs.js"></script>

Then you can use EJS client-side like this: 然后,您可以像这样使用EJS客户端:

var html = ejs.render('your template here', {postArr: postArr});

$('#mainContainer').html(html);

Here's I'm assuming postArr is set appropriately, possibly as the result of an AJAX request. 这是假设postArr设置正确,可能是AJAX请求的结果。

As you're using EJS to generate EJS it all gets a lot more difficult to understand but the relevant code in your home.ejs would be something like this: 当您使用EJS生成EJS时,所有这些都变得更加难以理解,但是home.ejs的相关代码将如下所示:

var html = ejs.render(<%- JSON.stringify(templates.onScroll) %>, {postArr: postArr});

$('#mainContainer').html(html);

This assumes that the onScroll template doesn't include any other templates. 假设onScroll模板不include任何其他模板。 You haven't specified whether home.ejs is being used to generate HTML or JavaScript so it's unclear precisely what other escaping considerations might apply but it's possible that won't be a problem. 您尚未指定是使用home.ejs生成HTML还是JavaScript,因此尚不清楚其他适用的转义注意事项可能适用,但可能不会出现问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM