如何优雅的把后台数据(通常是JSON)轻松渲染到html页面

在我们做前后端分离的时候,都有遇到过一些看起卡很简答,确无从下手的问题把。比方说后台给了前端一个list集合,集合里面有很多学生,我们现在要对这些学生在前端渲染展示。我们怎么办?

当然如果你是使用了强大的框架,完全不必要在意这些问题了。

但是当我们啥都没有的时候,我们可以考虑使用handlerbars这款强大的工具。可以非常轻松,侵入化很小,快捷的实现数据到页面的渲染。

1. 模板引擎handlerbars

- 官方网站:http://handlebarsjs.com/
- handlerbars简介:是一个纯js的库,主要功能通过对view和data的分离来快速构建Web模板,可以很方便简洁的使用
- 语法超级简单 {{key}},通过两个花括号包起来,更多语法参考官方文档[【官方文档】](http://handlebarsjs.com/guide/#what-is-handlebars)

2. 效果展示

3. 最简单的使用demo

  1. 准备个测试页面,页面引入handlebars.js

    • 引入handlerbars.js
    • 找个表格演示
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<!-- //todo 2 我们找个表格演示下 -->
<!-- <h3>学生信息表</h3> -->
<table width="500" align="center" cellspacing="0" cellpadding="6">
<caption>学生信息表</caption>
<thead>
<tr align="center">
<td>姓名</td>
<td>性别</td>
<td>芳龄</td>
<td>班级</td>
<td>备注</td>
</tr>
</thead>
<tbody>
<!--//todo 表格内容使用模板渲染 -->
</tbody>
</table> <!--//todo 1 引入handlerbars工具 -->
<script src="./js/handlebars.js"></script>
</body>
</html>
  1. 模拟后台的数据,假设后台返回了一个学生列表,如下(student)
var student = [
{name: '鲁班七号', gender: '未知', age: '20', class: '一年级射手班', note: '智商二百五的电玩小子,biubiubiu,就是腿短!'},
{name: '安琪拉', gender: '女', age: '18', class: '一年级法师班', note: '什么什么魔法书!'},
{name: '程咬金', gender: '男', age: '30', class: '一年级二班', note: '爱与正义之斧,偷偷推到'},
{name: '阿珂', gender: '女', age: '19', class: '三年级刺客班', note: '哪里亮了点哪里,都是我的人头!'},
{name: '李白', gender: '男', age: '20', class: '一年级酒酒仙班', note: '但愿长醉不愿醒,我是来无影去无踪,帅!'},
{name: '森瑶', gender: '女', age: '20', class: '皮肤班级', note: '不然梦会碎还是破!'}
]
  1. 定义模板,注意type="text/x-handlebars-template"必须这样写
    <!--//todo 表格内容使用模板渲染 -->
<script type="text/x-handlebars-template" id="template">
{{#each this}} <tr align="center">
<td>{{name}}</td>
<td>{{gender}}</td>
<td>{{age}}</td>
<td>{{classes}}</td>
<td>{{note}}</td>
</tr> {{/each}}
</script>
  1. 渲染数据
        //获取需要放数据的容器
var container = document.querySelector('#container');
//也就是获取我们定义的模板的dom对象。主要是想获取里面的内容(innerHTML)
var templateDom = document.querySelector('#template');
//编译模板的里的内容
var template = Handlebars.compile(templateDom.innerHTML);
//把后台获取到的数据student渲染到页面
container.innerHTML = template(student);

4. 附上完整代码

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"">
<title>m-web</title>
</head>
<body> <!-- <h3>学生信息表</h3> -->
<table border="1" width="700" align="center" cellspacing="0" cellpadding="6">
<caption>学生信息表</caption>
<thead>
<tr align="left">
<td>姓名</td>
<td>性别</td>
<td>芳龄</td>
<td>班级</td>
<td>备注</td>
</tr>
</thead>
<tbody id="container">
<!--//todo 表格内容使用模板渲染 -->
<script type="text/x-handlebars-template" id="template">
{{#each this}} <tr align="left">
<td>{{name}}</td>
<td>{{gender}}</td>
<td>{{age}}</td>
<td>{{classes}}</td>
<td>{{note}}</td>
</tr> {{/each}}
</script>
</tbody>
</table> <script src="./js/handlebars.js"></script>
<script>
var student = [
{name: '鲁班七号', gender: '未知', age: '20', classes: '一年级射手班', note: '智商二百五的电玩小子,biubiubiu,就是腿短!'},
{name: '安琪拉', gender: '女', age: '18', classes: '一年级法师班', note: '什么什么魔法书!'},
{name: '程咬金', gender: '男', age: '30', classes: '一年级二班', note: '爱与正义之斧,偷偷推到'},
{name: '阿珂', gender: '女', age: '19', classes: '三年级刺客班', note: '哪里亮了点哪里,都是我的人头!'},
{name: '李白', gender: '男', age: '20', classes: '一年级酒酒仙班', note: '但愿长醉不愿醒,我是来无影去无踪,帅!'},
{name: '森瑶', gender: '女', age: '20', classes: '皮肤班级', note: '不然梦会碎还是破!'}
] //获取需要放数据的容器
var container = document.querySelector('#container');
//也就是获取我们定义的模板的dom对象。主要是想获取里面的内容(innerHTML)
var templateDom = document.querySelector('#template');
//编译模板的里的内容
var template = Handlebars.compile(templateDom.innerHTML);
//把后台获取到的数据student渲染到页面
container.innerHTML = template(student);
</script>
</body>
</html>

官方说明文档

如何优雅的把后台数据(通常是JSON)轻松渲染到html页面的更多相关文章

  1. 用ajax获取后台数据,返回json数据,怎么在前台使用?

    用ajax获取后台数据,返回json数据,怎么在前台使用呢?后台 C# code   ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 if (dataType == &qu ...

  2. 在ssm框架中前后台数据交互均使用json格式

    前后台数据交互均使用json. 框架ssm(spring+springmvc+mybatis) @RequestBody注解实现接收http请求的json数据,将json数据转换为java对象,注解加 ...

  3. --@angularJS--$http服务与后台数据交互

    1.httpBasic.html: <!DOCTYPE HTML><html ng-app="app"><head>    <title& ...

  4. Echarts 获取后台数据 使用后台数据展示 柱形图

    后台数据要以json格式返回 页面:引用echarts.js , 然后data以ajax的数据请求并返回 <%@ page language="java" import=&q ...

  5. 在Vue中由后台数据循环生成多选框CheckBox时的注意事项

    多选框是一种非常常见的功能,有时候我们会根据后台返回的数据进行多选框渲染,之前做项目时遇到循环生成多选框时,v-model绑定的值会随着选中与取消改变,但页面却不会变化 的情况,后来测试了一下,发现多 ...

  6. python框架Django使用xadmin管理后台数据

    Django使用xadmin管理后台数据 关注公众号"轻松学编程"了解更多. 作用:xadmin比Django内置的admin更美观.更方便. 一.导入xadmin(第三方库) 方 ...

  7. ajax提交数据到java后台,并且返回json格式数据前台接收处理值

    1.前台html页面.有一段代码如下: 账  户:  <input type="text" name="userName" id="userN& ...

  8. AFNetworking 与 gbk 编码格式后台数据的使用

    仅针,后台数据为GBK编码时的AFNetWorking 使用情况: 1. Request failed: unacceptable content-type: text/html     soluti ...

  9. 关于用bootstrap显示查询的后台数据

    PrintWriter pw = response.getWriter(); pw.println(sb); pw.flush(); 由于用bootstrap查询数据,页面需要自身返回bootstra ...

随机推荐

  1. Halcon算子含义

    1.1 Gaussian-Mixture-Models 1.add_sample_class_gmm 功能:把一个训练样本添加到一个高斯混合模型的训练数据上. 2.classify_class_gmm ...

  2. By virtue of|sustain|post |scrape off |stretch|access to|take into account of|exploit|hasten|blur |idle|bored her to|account for|accused of|cruelty

    By virtue of this superior quality, this product is often sold out of stockin many areas. 我们的产品因其优秀的 ...

  3. [LC] 71. Simplify Path

    Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert it to the ca ...

  4. spring5.0.7.RELEASE配置jackson2.9.5

    概述 Jackson框架是基于Java平台的一套数据处理工具,被称为“最好的Java Json解析器”. 1.环境: jdk版本:jdk1.8spring版本:5.0.7.RELEASE jackso ...

  5. Javascript面试题&知识点汇总

    问题&答案 什么时候 a ==1 && a== 2 && a==3 为 true? var a = { i: 1, toString: function () ...

  6. spring+mybatis+shiro入门实例

    sql: 1 /* 2 SQLyog Ultimate v11.33 (64 bit) 3 MySQL - 5.1.49-community : Database - db_shiro 4 ***** ...

  7. Python---8函数(函数的参数&递归函数)

    一.函数的参数 Python的函数定义非常简单,但灵活度却非常大.除了正常定义的必选参数外,还可以使用默认参数.可变参数和关键字参数,使得函数定义出来的接口,不但能处理复杂的参数,还可以简化调用者的代 ...

  8. Yii2创建管理员登录

    1. 创建管理员表 进入项目根目录,在根目录执行命令: 1 $ ./yii migrate 2. 创建管理的控制器 1 $ cd console/controllers/ 编写代码如下: 123456 ...

  9. pycharm中无法导入pip安装的包

    https://blog.csdn.net/mdxiaohu/article/details/82430060 2020.1.20 练习通过python操作数据库的时候需要导入一个包,因为看代码写的是 ...

  10. SpringMVC引入CSS等文件

    在默认情况下Spring MVC 拦截了所有请求,所以自己要把静态资源配置起来,IDEA 在Spring-service 配置,eclipse在自己新建的SpringMVC配置文件里配置,如下代码 & ...