<script id="myTemplate" type="text/x-handlebars-template"></script>
複製代碼
var source = $("#myTemplate").html();
var template = Handlebars.compile(source);
var context = {title: "My New Post", body: "This is my first post!"};
var html = template(context);
$('#box').html(html);
複製代碼
完整代碼javascript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.11/handlebars.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
</head>
<body>
<script id="myTemplate" type="text/x-handlebars-template"></script>
<div id="box"></div>
<script> var source = $("#myTemplate").html(); var template = Handlebars.compile(source); var context = {title: "My New Post", body: "This is my first post!"}; var html = template(context); $('#box').html(html); </script>
</body>
</html>
複製代碼
<div class="entry">
<h1>{{title}}</h1>
<div class="body">
{{{body}}}
</div>
</div>
複製代碼
<script id="myTemplate" type="text/x-handlebars-template"></script>
<div id="box"></div>
<script> var source = $("#myTemplate").html(); var template = Handlebars.compile(source); var context = { people: [ {firstName: "Yehuda", lastName: "Katz"}, {firstName: "Carl", lastName: "Lerche"}, {firstName: "Alan", lastName: "Johnson"} ] }; Handlebars.registerHelper('list', function(items, options) { var out = "<ul>"; for(var i=0, l=items.length; i<l; i++) { out = out + "<li>" + options.fn(items[i]) + "</li>"; } return out + "</ul>"; }); var html = template(context); $('#box').html(html); </script>
複製代碼
<script id="myTemplate" type="text/x-handlebars-template"></script>
<div id="box"></div>
<script> var source = $("#myTemplate").html(); var template = Handlebars.compile(source); var context = { title: "My First Blog Post!", author: { id: 47, name: "Yehuda Katz" }, body: "My first post. Wheeeee!" }; var html = template(context); $('#box').html(html); </script>
複製代碼
{{#with person}}
<h1>{{../company.name}}</h1>
{{/with}}
{
"person":
{ "name": "Alan" },
company:
{"name": "Rad, Inc." }
};
複製代碼
若是變量是false
, undefined
, null
, ""
, 0
, []
,將不會渲染html
<div class="entry">
{{#if author}}
<h1>{{firstName}} {{lastName}}</h1>
{{/if}}
</div>
複製代碼
和if相反java
<div class="entry">
{{#unless license}}
<h3 class="warning">WARNING: This entry does not have a license!</h3>
{{/unless}}
</div>
複製代碼
能夠用this指代子元素jquery
<ul class="people_list">
{{#each people}}
<li>{{this}}</li>
{{/each}}
</ul>
{
people: [
"Yehuda Katz",
"Alan Johnson",
"Charles Jolley"
]
}
複製代碼
渲染結果是ajax
<ul class="people_list">
<li>Yehuda Katz</li>
<li>Alan Johnson</li>
<li>Charles Jolley</li>
</ul>
複製代碼
能夠用else
渲染當列表爲空時的頁面less
{{#each paragraphs}}
<p>{{this}}</p>
{{else}}
<p class="empty">No content</p>
{{/each}}
複製代碼
若是須要引用列表的索引或者key
的話,能夠post
{{#each array}}
{{@index}}: {{this}}
{{/each}}
{{#each object}}
{{@key}}: {{this}}
{{/each}}
複製代碼
value 和 key 別名ui
{{#each array as |value key|}}
{{#each child as |childValue childKey|}}
{{key}} - {{childKey}}. {{childValue}}
{{/each}}
{{/each}}
複製代碼
<div class="entry">
<h1>{{title}}</h1>
{{#with author}}
<h2>By {{firstName}} {{lastName}}</h2>
{{/with}}
</div>
{
title: "My first post!",
author: {
firstName: "Charles",
lastName: "Jolley"
}
}
複製代碼
或者this
<div class="entry">
<h1>{{title}}</h1>
{{#with author as |myAuthor|}}
<h2>By {{myAuthor.firstName}} {{myAuthor.lastName}}</h2>
{{/with}}
</div>
複製代碼
<script id="myTemplate" type="text/x-handlebars-template"></script>
<div id="box"></div>
<script> var source = $("#myTemplate").html(); var template = Handlebars.compile(source); var context = { author: {firstName: "Alan", lastName: "Johnson"}, body: "I Love Handlebars", comments: [{ author: {firstName: "Yehuda", lastName: "Katz"}, body: "Me too!" }] }; Handlebars.registerHelper('fullName', function(person) { return person.firstName + " " + person.lastName; }); var html = template(context); $('#box').html(html); </script>
複製代碼
能夠使用this上下文spa
<script id="myTemplate" type="text/x-handlebars-template"></script>
<div id="box"></div>
<script> var source = $("#myTemplate").html(); var template = Handlebars.compile(source); var context = { items: [ {name: "Handlebars", emotion: "love"}, {name: "Mustache", emotion: "enjoy"}, {name: "Ember", emotion: "want to learn"} ] }; Handlebars.registerHelper('agree_button', function() { var emotion = Handlebars.escapeExpression(this.emotion), name = Handlebars.escapeExpression(this.name); console.log(emotion) return new Handlebars.SafeString( "<button>I agree. I " + emotion + " " + name + "</button>" ); }); var html = template(context); $('#box').html(html); </script>
複製代碼
<script id="myTemplate" type="text/x-handlebars-template"></script>
<div id="box"></div>
<script> var source = $("#myTemplate").html(); var template = Handlebars.compile(source); Handlebars.registerPartial('userMessage', '<{{tagName}}>By {{author.firstName}} {{author.lastName}}</{{tagName}}>' + '<div class="body">{{body}}</div>'); var context = { author: {firstName: "Alan", lastName: "Johnson"}, body: "I Love Handlebars", comments: [{ author: {firstName: "Yehuda", lastName: "Katz"}, body: "Me too!" }] }; var html = template(context); $('#box').html(html); </script>
複製代碼