ajax结合前端模板引擎ejs.js渲染页面(自己理解,不建议参考)

《一、利用前端前端引擎和ajax来渲染前端页面》

1、就是创建一个装渲染内容的盒子
comment是接口传进来的参数,就是传进来的数据,名字就叫“comment”因此:“comment.名”字就是填入的数据名字
 例如:<script  type="text/template"id="comments">
                    <% comments.forEach(function(comment){%>
                        <div class="text_user_text">
                            <div class="text-img z">
                                <img src="imgs/user.png">
                            </div>
                            <div class="user_comment">
                                <div class="spans">
                                <span class="user_name">
                                        <strong><%=comment.user%></strong>
                                    </span>
                                    <span><%=comment.postdate%></span>
                                    <i class="del y" data-id="<%=comment.id%>">x</i>
                                </div>
                                <div class="span_comment">
                                    <p><%=comment.detail%></p>
                                </div>
                            </div>
                        </div>
                    <% }) %>
            </script>
2、连接接口获取数据
就是用AJAX来写js函数获取数据库中的数据
;(function(){
        第一步就是用var先找到要渲染的盒子
        var count = document.getElementById('count')
       这里的count是放内容的大盒子 ,就是没有放入ejs前端渲染引擎的大盒子里
       第二步就是用ajax中的get函数和post函数来连接数据库,查找数据库中的内容
      例子:_.getJSON(‘php接口’,[callback函数]function(data){
第三步就是找到ejs渲染的盒子,就是script的id值<script  type="text/template"id="comments">
         var template = document.querySelector('#comments').innerHTML
    第四步就是判断是否请求成功
   if (data.code === 200){
      //comments是要渲染的盒子,就是内容的盒子
//第六步就是用ejs.render(方内容的盒子,{js盒子id名comments:data.list接口文件的名字})就是把ajax请求过来的数据写到要添加内容的盒子里
      var html = ejs.render(template, {comments: data.list})
    }else{
      var html = ejs.render(template, {comments: []})
    }
    list.innerHTML = html//把list请求过来的数据填到对应的盒子里
} )

///这样就完成了内容的渲染,就是利用ajax把数据库中的内容渲染到页面中,就是循环生成多个标签,就是数据库中有多少条就渲染多少条对应的html标签内容。

《二、利用后端php来渲染前端页面》

1、先是把html文件改成后缀php类型的文件,
2、在其上面写php接口
例如:<?php
                $id = $_GET['id'] ? $_GET['id'] : 1;
                $mysqli = new Mysqli('localhost', 'root', 'root', 'db_yuedu');
                $sql = "SELECT * FROM t_articles WHERE id = {$id}";

                $result = $mysqli->query($sql);

                $row = Mysqli_fetch_assoc($result)

            ?>
这里返回的就是数据$row数据
 3、然后就是在要渲染的模板中填入对应的数据
 例如:<div><?php  echo($row['play_time这是返回来的数据库名字'])   ?></div>

//这样就是用php后端来渲染html前端页面,好像不能循环渲染

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
ejs.render()是一个ejs模板引擎渲染方法,它可以将一个ejs模板文件和数据对象作为参数,返回一个渲染后的HTML字符串。 在代码中使用ejs.render()方法,需要先安装ejs模块,并在代码中引入该模块。然后,需要定义一个ejs模板文件,其中可以使用ejs模板语法来动态地插入数据。 例如,我们可以定义一个名为"template.ejs"的模板文件,其中包含以下内容: ```html <html> <head> <title><%= title %></title> </head> <body> <h1>Welcome <%= name %></h1> </body> </html> ``` 在这个模板文件中,"<%= %>"是ejs模板语法中用来插入数据的标记,其中"title"和"name"是我们需要动态渲染的数据属性。 接下来,在代码中我们需要定义一个数据对象,包含需要渲染的数据属性,例如: ```javascript const data = { title: "My Website", name: "John" }; ``` 最后,我们使用ejs.render()方法将模板文件和数据对象作为参数传入,然后将返回的HTML字符串输出到页面中,例如: ```javascript const ejs = require("ejs"); const fs = require("fs"); const template = fs.readFileSync("template.ejs", "utf8"); const data = { title: "My Website", name: "John" }; const html = ejs.render(template, data); console.log(html); ``` 在这个例子中,我们首先使用fs模块读取模板文件,然后使用ejs.render()方法将模板文件和数据对象渲染为HTML字符串。最后,我们将渲染后的HTML字符串输出到控制台中。 当然,在实际使用中,我们通常会将渲染后的HTML字符串输出到客户端浏览器中,例如: ```javascript res.writeHead(200, {"Content-Type": "text/html"}); res.write(ejs.render(template, data)); res.end(); ``` 这样就可以动态地渲染ejs模板文件中的数据了。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值