-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimages.html
45 lines (43 loc) · 1.54 KB
/
images.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>How to render html with Handlebars.js</title>
<!-- Load handlerbars.js -->
<script src="handlebars.js"></script>
<!-- Load Jquery from CDN for easy DOM manipulations -->
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<!-- Simple handlebars template for a blog post, inside {{variable}} are variables we can afect with JS objects-->
<script id="simple-template" type="text/x-handlebars-template">
{{#each images}}
<li><a href="{{url}}">{{url}}</a></li>
{{/each}}
</script>
<script type="text/javascript">
//wait for page to load
$(document).ready(function(){
// Extract the text from the template .html() is the jquery helper method for that
var raw_template = $('#simple-template').html();
// Compile that into an handlebars template
var template = Handlebars.compile(raw_template);
// Retrieve the placeHolder where the Posts will be displayed
var placeHolder = $("#main ol");
// Fetch all Blog Posts data from server in JSON
$.get("http://links.dev/links.json",function(data,status,xhr){
$.each(data,function(index,element){
// Generate the HTML for each post
var html = template(element);
// Render the posts into the page
placeHolder.append(html);
});
});
});
</script>
</head>
<body>
<!-- Insertion point for handlebars template -->
<div id="main" style="margin-left:100px">
<ol></ol>
</div>
</body>
</html>