Skip to content

Instantly share code, notes, and snippets.

@MartinJHammer
Last active December 29, 2016 18:11
Show Gist options
  • Save MartinJHammer/4ef6c698f73d803fbf4b3ea7414db131 to your computer and use it in GitHub Desktop.
Save MartinJHammer/4ef6c698f73d803fbf4b3ea7414db131 to your computer and use it in GitHub Desktop.
Requires jquery, handlebars
<!-- A handlebar template -->
<div class="entry"></div>
<script id="entry-template" type="text/x-handlebars-template">
<h1>{{title}}</h1>
<div class="body">
{{body}}
</div>
</script>
<!-- A handlebar template containing a block expression -->
<div class="customers"></div>
<script id="customers-template" type="text/x-handlebars-template">
<ul>
{{#each customers}}
<li> {{ firstName }} {{ lastName }} </li>
{{/each}}
</ul>
</script>
(function (templater, $, undefined) {
"use strict";
// vars
var entryTemplate = function () { return; };
var blockExpression = function () { return; };
/**
* Populates the entry-template.
* @public
*/
templater.entryTemplate = function () {
// vars
var source = "";
var template = function () { return; };
var data = {};
var html = "";
// implementation
source = $("#entry-template").html();
template = Handlebars.compile(source);
data = {
title: "My New Post",
body: "This is my first post!"
};
html = template(data);
$(".entry").append(html);
};
/**
* Populates the block expression.
* @public
*/
templater.blockExpression = function () {
// vars
var source = "";
var template = function () { return; };
var data = {};
var html = "";
// implementation
source = $("#customers-template").html();
template = Handlebars.compile(source);
data = {
customers: [
{ firstName: "Yehuda", lastName: "Katz" },
{ firstName: "Carl", lastName: "Lerche" },
{ firstName: "Alan", lastName: "Johnson" }
]
};
html = template(data);
$(".customers").append(html);
};
}(window.templater = window.templater || {}, jQuery));
$(function () {
templater.entryTemplate();
templater.blockExpression();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment