• Handlebars 是 JavaScript 一个语义模板库,通过对view和data的分离来快速构建Web模板。它采用"Logic-less template"(无逻辑模版)的思路,在加载时被预编译,而不是到了客户端执行到代码时再去编译, 这样可以保证模板加载和运行的速度。
  • 简单的说就是:Handlebars是一个很好的前后端的分离的方案

引入:

代码写在body下的script标签里面,并且id="template",type的类型为type="text/x-handlebars-template"

记得还要去js中去配置一些东东:

算了吧,来一篇直接粘贴过去就能拿来用的,以后一号拿来复习再巩固

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>handlebars</title>
<style>
.big {
width: 100px;
height: 100px;
border: 1px solid green;
border-radius: 100px;
position: relative;
}
.small {
width: 50px;
height: 50px;
border: 1px solid green;
border-radius: 50px;
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
text-align: center;
line-height: 50px;
}
</style>
</head>
<body>
<div id="container"></div>
<script id="template" type="text/x-handlebars-template">
<h1>Hello</h1>
<h2>Handlebars</h2>
<table border="1">
<thead>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{name}}</td>
<td>{{age}}</td>
<td>{{gender}}</td>
</tr>
</tbody>
</table>
<p>hello, {{name}}</p>
<p>{{hello}}</p>
<table border="1">
<thead>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
</tr>
</thead>
<tbody>
{{#each listOfStudents}}
<tr>
<td>{{name}}</td>
<td>{{age}}</td>
<td>{{gender}}</td>
</tr>
{{/each}}
</tbody>
</table> <p>hello, {{{person.a.name}}}</p>
<p>hello, {{person/a/name}}</p> <div class="big">
<div class="small">
2
</div>
</div>
<div class="big">
<div class="small">
3
</div>
</div>
<div class="big">
<div class="small">
4
</div>
</div> <!-- {{circle name}}
{{circle name}}
{{circle name}} -->
</script>
<script src="./handlebars-v4.0.5.js"></script>
<script>
// 获取模板的源代码
var source = document.getElementById('template').innerHTML;
// 把模板的源代码,编译成模板对象
var template = Handlebars.compile(source);
// 创建helper
Handlebars.registerHelper('circle', function(data ) {
// 告诉系统,这个返回值要解析成真正的标签
var obj = new Handlebars.SafeString('<div class="big"><div class="small">' + data + '</div></div>'); return obj;
});
// 通过模板对象,获取最终html代码
var html = template({
person: {
a: {
name: 'Tom<input type="text">'
},
b: 'hello'
},
name: 'Bob',
age: 20,
listOfStudents: [
{
name: 'bob',
age: 20,
gender: 'male'
},
{
name: 'tom',
age: 22,
gender: 'male'
}]
}); // console.log(html);
// 把html代码插入到网页中去
document.getElementById('container').innerHTML = html; // helper
</script>
</body>
</html>

  

handlebars的更多相关文章

  1. Handlebars 模板引擎之前后端用法

    前言 不知不觉间,居然已经这么久没有写博客了,坚持还真是世界上最难的事情啊. 不过我最近也没闲着,辞工换工.恋爱失恋.深圳北京都经历了一番,这有起有落的生活实在是太刺激了,就如拿着两把菜刀剁洋葱一样, ...

  2. handlebars自定义helper的写法

    handlebars相对来讲算一个轻量级.高性能的模板引擎,因其简单.直观.不污染HTML的特性,我个人特别喜欢.另一方面,handlebars作为一个logicless的模板,不支持特别复杂的表达式 ...

  3. handlebars.js的运用与整理

    最近在做部门的技术分享网站,主要是一些文章的列表和演讲信息展示,内容比较规律,复用性较高.同事推荐了 handlebars.js.用来看看. handlebars.js是一种静态JS模板,用起来还蛮简 ...

  4. Express 4 handlebars 不使用layout写法

    Express 4 handlebars 不使用layout写法 Express node nodejs handlebars layout 最近刚开始学习使用nodejs. 使用express搭建了 ...

  5. Handlebars.js的学习

    写在开头的话: 在使用Ghost搭建自己的博客的时候,发现不会Handlebars.js寸步难行,所以本人决定学习下Handlebars.js,因此在此做个记录 为什么选择Handlebars.js ...

  6. js模版引擎handlebars.js实用教程——目录

    写在开头的话: 阅读本文需要了解基本的Handlebars.js概念,本文并不是Handlebars.js基础教程,而是注重于实际应用,为读者阐述使用过程中可能会遇到的一些问题. 实际上,小菜写这篇文 ...

  7. Handlebars块级Helpers

    1.Handlebars简单介绍: Handlebars是JavaScript一个语义模板库,通过对view和data的分离来快速构建Web模板.它采用"Logic-less templat ...

  8. js模版引擎handlebars.js实用教程——为什么选择Handlebars.js

    返回目录 据小菜了解,对于java开发,涉及到页面展示时,比较主流的有两种解决方案: 1. struts2+vo+el表达式. 这种方式,重点不在于struts2,而是vo和el表达式,其基本思想是: ...

  9. handlebars.js 用 <br>替换掉 内容的换行符

    handlebars.js 用 <br>替换掉 内容的换行符 JS: Handlebars.registerHelper('breaklines', function(text) { te ...

  10. 【前端】制作一个handlebars的jQuery插件

    (function($) { var compiled = {}; $.fn.handlebars = function($srcNode, data) { // 取出模版内容 var src = $ ...

随机推荐

  1. S7-1200 与 S7-200 的对比PPT

  2. UVA 10003 切木棍(普通DP)

    切木棍 紫书P278 算是简单的dp了吧,当然,这是看完别人题解后的想法,呵呵,我仍然是想了半小时,没思路,啥时候能自个整个dp啊!!→_→ dp的时候,输入数组必须从1开始,一定要注意状态的设计,和 ...

  3. Windows下查看进程及结束进程命令[转]

    Windows下查看进程及结束进程命令 1)查看占用8080端口的进程号 >netstat –aon | findstr “8080” 结果:TCP    0.0.0.0:8080        ...

  4. Spring Boot 静态资源处理

    spring Boot 默认的处理方式就已经足够了,默认情况下Spring Boot 使用WebMvcAutoConfiguration中配置的各种属性. 建议使用Spring Boot 默认处理方式 ...

  5. UNIX网络编程-send、recv、sendto、recvfrom详解

    send.recv和sendto.recvfrom,一般情况下,send.recv在TCP协议下使用,sendto.recvfrom在UDP协议下使用,也可以在TCP协议下使用,不过用的很少. 1.s ...

  6. 10. Max Points on a Line

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  7. iOS 获取文件的目录路径的几种方法 [转]

    iOS 获取文件的目录路径的几种方法 2 years ago davidzhang iphone沙箱模型的有四个文件夹,分别是什么,永久数据存储一般放在什么位置,得到模拟器的路径的简单方式是什么. d ...

  8. openldap加密传输 nslcd

    http://www.openldap.org/faq/data/cache/185.html https://www.ibm.com/developerworks/cn/linux/1312_zha ...

  9. 转(Delphi 新窑洞):使用delphi 开发多层应用(十七)使用RTC web 服务器返回JSON

    RTC作为delphi 的最专业的web 应用服务器,如果客户端要使用JSON 的话,那么使用RTC 应该也是一种 非常好的选择.下面我们做一个使用RTC web 服务器返回数据库JSON 的例子. ...

  10. TCP控制拥塞的四种算法:慢开始,拥塞避免,快重传,快恢复

    我们在开始假定: 1:数据是单方向传递,另一个窗口只发送确认. 2:接收方的缓存足够大,因此发送方的大小的大小由网络的拥塞程度来决定. 一:慢开始算法和拥塞避免算法 发送方会维持一个拥塞窗口,刚开始的 ...