Handlebars 介绍

时间:2023-03-10 04:24:09
Handlebars 介绍

最新项目用到了Ember.js前端框架,第一次使用这样的框架,准备国庆节花2天时间,研究一下它的用法。

Ember框架的模板引擎用到了handlebars, 先看国外的一篇介绍文章:An Introduction to Handlebars

手动写了一个测试示例:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<script id="blogs" type="text/x-Handlebars-template1">
<div class="entry">
<h1>{{title}}</h1>
<div class="body">
{{body}}
</div>
</div>
</script>
<div id="temp"></div>
</body>
</html>
<script src="scripts/handlebars-v4.0.2.js"></script>
<script type="text/javascript">
var context = {
title: "handlebars introduction",
body: "handlebar is js template..."
};
var template = Handlebars.compile(document.getElementById("blogs").innerHTML);
document.getElementById("temp").innerHTML = template(context);
</script>