ejs模板渲染页面

2023-11-21 14:59
文章标签 模板 页面 渲染 ejs

本文主要是介绍ejs模板渲染页面,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、npm init 

2、安装ejs npm i ejs --save

3、app.js 

 1 const http = require('http');
 2 const fs = require('fs');
 3 const ejs = require('ejs');
 4 const path = require('path');
 5 
 6 // 1. 创建服务器
 7 http.createServer((req, res)=>{
 8     // 1.1 读取文件
 9     readDataJson((jsonData)=>{
10 
11          // 1.2 读取模板信息
12         fs.readFile(path.join(__dirname, 'view/list.ejs'), (err, data)=>{
13             if(err) throw err;
14 
15             // 实例化模板引擎
16             let ejsList = data.toString();
17             let tempList = ejs.render(ejsList, jsonData);  //jsonData 下方函数返回的,将jsonData注入ejs中
19       // 1.3 响应给客户端  
20       res.writeHead(200, {'Content-Type': 'text/html;charset=utf-8'});

21       res.end(tempList); 22 }); 23 });
24     }).listen(3000);
26 let readDataJson = (callback)=>{
27   fs.readFile(path.join(__dirname, 'model/data.json'), (err, data)=>{
28   if(err) throw err;
29   let jsonData = JSON.parse(data);
30   callback && callback(jsonData);
31 });
32 };

//第一个callback判断是否存在 第二个存在就返回

4、model中json文件

{"lists":[{"title": "撩课学院1周年庆倒计时", "count": 675593, "up": 1},{"title": "女演员全美善自杀", "count": 634434, "up": 1},{"title": "哈登骑电动车被抓", "count": 623323, "up": 0},{"title": "吃酸辣粉被罚款", "count": 546767, "up": 0},{"title": "蔚来汽车庄莉离职", "count": 536237, "up": 1},{"title": "父母抓阄陪女儿", "count": 525193, "up": 0},{"title": "宋仲基爸爸短信", "count": 475593, "up": 0},{"title": "宋仲基爸爸短信", "count": 375593, "up": 1},{"title": "今天天气很好", "count": 275593, "up": 1}],"source": "撩课风云榜 - itLike.com"
}

5、view中ejs.js文件

  1 <!doctype html>
  2 <html>
  3 <head>
  4     <meta charset="UTF-8">
  5     <meta name="viewport"
  6           content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  7     <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8     <title>百度风云排行版</title>
  9     <style>
 10         * {
 11             margin: 0;
 12             padding: 0;
 13             list-style: none;
 14         }
 15 
 16         #panel {
 17             width: 500px;
 18             border: 1px solid #c6c8ca;
 19             margin: 100px auto;
 20         }
 21 
 22         #panel_header {
 23             display: flex;
 24             justify-content: space-around;
 25             border-bottom: 1px solid #ccc;
 26             line-height: 44px;
 27             color: #777;
 28         }
 29 
 30         #panel_body > li {
 31             display: flex;
 32             flex-direction: row;
 33             justify-content: space-between;
 34             line-height: 44px;
 35             border-bottom: 1px solid #e8e8e8;
 36         }
 37 
 38         .c-icon {
 39             background: url(https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/static/protocol/https/global/img/icons_5859e57.png) no-repeat 0 0;
 40             display: inline-block;
 41             width: 14px;
 42             height: 14px;
 43             vertical-align: text-bottom;
 44             font-style: normal;
 45             overflow: hidden;
 46         }
 47 
 48         .opr-toplist-st {
 49             margin-bottom: 2px;
 50         }
 51 
 52         .c-icon-up {
 53             background-position: -720px -168px;
 54         }
 55 
 56         .c-icon-down {
 57             background-position: -744px -168px;
 58         }
 59 
 60         .left {
 61             margin-left: 10px;
 62             display: flex;
 63             flex-direction: row;
 64             align-items: center;
 65         }
 66 
 67         .left .no {
 68             display: flex;
 69             justify-content: center;
 70             align-items: center;
 71             width: 18px;
 72             height: 18px;
 73             background-color: red;
 74             color: #fff;
 75             margin: 5px;
 76         }
 77 
 78         .right {
 79             margin-right: 10px;
 80         }
 81 
 82         #panel_footer {
 83             display: flex;
 84             justify-content: flex-end;
 85             margin: 10px;
 86             color: #777;
 87             font-size: 15px;
 88         }
 89     </style>
 90 </head>
 91 <body>
 92 <section id="panel">
 93     <div id="panel_header">
 94         <span>排名</span>
 95         <span>搜索指数</span>
 96     </div>
 97     <ul id="panel_body">
 98         <% for(var i = 0; i < lists.length; i++) { %>
 99             <li>
100                 <div class="left">
101                 <span class="no" style="background: <%= i > 2 ? 'gray' : 'red' %>;">
102                     <%= i + 1 %>
103                 </span>
104                     <span>
105                     <%= lists[i].title %>
106                 </span>
107                 </div>
108                 <div class="right">
109                 <span>
110                     <%= lists[i].count %>
111                 </span>
112                     <% if(lists[i].up === 1){ %>
113                         <i class="opr-toplist-st c-icon c-icon-up"></i>
114                     <% }else { %>
115                         <i class="opr-toplist-st c-icon c-icon-down"></i>
116                     <% } %>
117                 </div>
118             </li>
119         <% } %>
120     </ul>
121     <div id="panel_footer">
122         <span style="margin-right: 5px">来源:</span>
123         <span>
124             <%= source %>
125         </span>
126     </div>
127 </section>
128 </body>
129 </html>
View Code

 

 

转载于:https://www.cnblogs.com/zhangzhengyang/p/11146150.html

这篇关于ejs模板渲染页面的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/403090

相关文章

详解如何在React中执行条件渲染

《详解如何在React中执行条件渲染》在现代Web开发中,React作为一种流行的JavaScript库,为开发者提供了一种高效构建用户界面的方式,条件渲染是React中的一个关键概念,本文将深入探讨... 目录引言什么是条件渲染?基础示例使用逻辑与运算符(&&)使用条件语句列表中的条件渲染总结引言在现代

基于Java实现模板填充Word

《基于Java实现模板填充Word》这篇文章主要为大家详细介绍了如何用Java实现按产品经理提供的Word模板填充数据,并以word或pdf形式导出,有需要的小伙伴可以参考一下... Java实现按模板填充wor编程d本文讲解的需求是:我们需要把数据库中的某些数据按照 产品经理提供的 word模板,把数据

使用JavaScript将PDF页面中的标注扁平化的操作指南

《使用JavaScript将PDF页面中的标注扁平化的操作指南》扁平化(flatten)操作可以将标注作为矢量图形包含在PDF页面的内容中,使其不可编辑,DynamsoftDocumentViewer... 目录使用Dynamsoft Document Viewer打开一个PDF文件并启用标注添加功能扁平化

SpringBoot如何访问jsp页面

《SpringBoot如何访问jsp页面》本文介绍了如何在SpringBoot项目中进行Web开发,包括创建项目、配置文件、添加依赖、控制层修改、测试效果以及在IDEA中进行配置的详细步骤... 目录SpringBoot如何访问JSP页python面简介实现步骤1. 首先创建的项目一定要是web项目2. 在

poj3468(线段树成段更新模板题)

题意:包括两个操作:1、将[a.b]上的数字加上v;2、查询区间[a,b]上的和 下面的介绍是下解题思路: 首先介绍  lazy-tag思想:用一个变量记录每一个线段树节点的变化值,当这部分线段的一致性被破坏我们就将这个变化值传递给子区间,大大增加了线段树的效率。 比如现在需要对[a,b]区间值进行加c操作,那么就从根节点[1,n]开始调用update函数进行操作,如果刚好执行到一个子节点,

C++11第三弹:lambda表达式 | 新的类功能 | 模板的可变参数

🌈个人主页: 南桥几晴秋 🌈C++专栏: 南桥谈C++ 🌈C语言专栏: C语言学习系列 🌈Linux学习专栏: 南桥谈Linux 🌈数据结构学习专栏: 数据结构杂谈 🌈数据库学习专栏: 南桥谈MySQL 🌈Qt学习专栏: 南桥谈Qt 🌈菜鸡代码练习: 练习随想记录 🌈git学习: 南桥谈Git 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈�

如何在页面调用utility bar并传递参数至lwc组件

1.在app的utility item中添加lwc组件: 2.调用utility bar api的方式有两种: 方法一,通过lwc调用: import {LightningElement,api ,wire } from 'lwc';import { publish, MessageContext } from 'lightning/messageService';import Ca

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n

uva 1342 欧拉定理(计算几何模板)

题意: 给几个点,把这几个点用直线连起来,求这些直线把平面分成了几个。 解析: 欧拉定理: 顶点数 + 面数 - 边数= 2。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#inc

uva 11178 计算集合模板题

题意: 求三角形行三个角三等分点射线交出的内三角形坐标。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <