美文网首页
1.handlebars(基础一)

1.handlebars(基础一)

作者: 大胡子111 | 来源:发表于2017-08-23 17:22 被阅读621次

一个使用了就会爱上的模板,真的很方便,强烈推荐。静态页面是房子的一个骨架,动态数据就是家里的门,家里的窗,家里的沙发...这些可以随之而动的东西,想让其怎么动,它就能怎么动。

从后台请求回来数据,(1.使用字符串拼接的方式2.es6模板字符串),渲染到页面。我只使用过这两种方法,第一种方法个人觉得操作繁琐,页面中大量的str+=“”页面很乱,写错一个符号,整个都渲染不出来。第二种方法很方便可以让页面html保持原有格式,直接反引号进行操作(str+=${obj1[i].h_year})不足的是目前浏览器对于es6的支持不足,需要用编译工具(babel)编译成es5浏览器才能认识它。handlebars模板可以让html保持原有格式,且不需要经过编译,体积小。

注意:handlebars是一款jquery插件

一:引入1.引入jquery 2.引入handlebars

<!--引入jquery/handlebars-->
<script type="text/javascript" src="./js/jquery-3.0.0.min.js"></script>
<script type="text/javascript" src="./js/handlebars-v4.0.10.js"></script>

二:写handlebars模板

注意:在需要渲染的html外层包上script标签,1.type要写,且要写对。2.给这个模板添加一个唯一的id值,不添加会找不到

 <tbody class="student-temp">
    <script type="text/x-handlebars-template" id="student-temp">
        <tr>
            <td>aa</td>
            <td>bb</td>
            <td>cc</td>
            <td>dd</td>
        </tr>
    </script>
    </tbody>

三:handlebars的取值 {{变量名}}

 <tbody class="student-temp">
    <script type="text/x-handlebars-template" id="student-temp">
        <tr>
            <td>{{name}}</td>
            <td>{{age}}</td>
            <td>{{gender}}</td>
            <td>{{fraction}}</td>
        </tr>
    </script>
    </tbody>

四:渲染数据

 <script>
    //    模拟数据
    var data = {
            "name": "柳2",
            "age": '8',
            "gender": '女',
            "fraction": '89'
    };
    console.log(data);

    //    获取到handlebars这个模板中的全部html内容
    var studentTemp = $('#student-temp').html();
    //    编译
    var HanStudent = Handlebars.compile(studentTemp);

    //把编译完成的代码放入到 .student-temp 的这个容器中
    $('.student-temp').html(HanStudent(data))


</script>

整个页面代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>demo</title>
    <style>
        table, th, td {
            border: 1px solid red;
            text-align: center;
        }
    </style>
</head>
<body>
<table>
    <thead>
    <tr>
        <th>姓名</th>
        <th>年龄</th>
        <th>性别</th>
        <th>分数</th>
    </tr>
    </thead>
    <tbody class="student-temp">
    <script type="text/x-handlebars-template" id="student-temp">
        <tr>
            <td>{{name}}</td>
            <td>{{age}}</td>
            <td>{{gender}}</td>
            <td>{{fraction}}</td>
        </tr>
    </script>
    </tbody>
</table>


<!--引入jquery/handlebars-->
<script type="text/javascript" src="./js/jquery-3.0.0.min.js"></script>
<script type="text/javascript" src="./js/handlebars-v4.0.10.js"></script>

<script>
    //    模拟数据
    var data = {
            "name": "柳2",
            "age": '8',
            "gender": '女',
            "fraction": '89'
    };
    console.log(data);

    //    获取到handlebars这个模板中的全部html内容
    var studentTemp = $('#student-temp').html();
    //    编译
    var HanStudent = Handlebars.compile(studentTemp);

    //把编译完成的代码放入到 .student-temp 的这个容器中
    $('.student-temp').html(HanStudent(data))


</script>
</body>
</html>

相关文章

  • 1.handlebars(基础一)

    一个使用了就会爱上的模板,真的很方便,强烈推荐。静态页面是房子的一个骨架,动态数据就是家里的门,家里的窗,家里的沙...

  • 1.handlebars(基础一)

    一个使用了就会爱上的模板,真的很方便,强烈推荐。静态页面是房子的一个骨架,动态数据就是家里的门,家里的窗,家里的沙...

  • 一、基础

    1. 计算机关键的硬件 CPU、内存、I/O控制芯片 2. SMP与多核 SMP: Symmetrical Mul...

  • 一、基础

    基础篇为python基础内容

  • 基础一

    词法结构 JavaScript程序是用Unicode字符集编写的 JavaScript是区分大小写的, 而HTML...

  • 基础(一)

    1.整数类型,小数类型,字符串类型,字符类型,布尔类型,空类型 2.\t,制表符,凑齐8个字符或8的倍数 3.计算...

  • pytorch基础一(基础运算)

    本人学习pytorch主要参考官方文档和 莫烦Python中的pytorch视频教程。后文主要是对pytorch官...

  • Javaweb基础(一)前端基础

    全部学习可以参考:http://www.w3school.com.cn/ 一.HTML 1.HTML网页介绍 JA...

  • TypeScript基础(一) 类型基础

    介绍 为了让程序有价值,我们需要能够处理最简单的数据单元:数字,字符串,结构体,布尔值等。 TypeScript支...

  • HTML基础一基础概念

    学习纲领 浏览器是什么? 1.将网页渲染出来给用户查看 2.和网页进行交互 5大主流浏览器 Internet Ex...

网友评论

      本文标题:1.handlebars(基础一)

      本文链接:https://www.haomeiwen.com/subject/yucjdxtx.html