Last active
December 30, 2016 16:25
-
-
Save MartinJHammer/a6fb91f66069c1b770aac080b5971adb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Module : templater | |
* Purpose : Builds and populates javascript templates | |
* Created by : Martin Jul Hammer | |
* Date : 28-12-2016 | |
*/ | |
module.exports = (function (templater, undefined) { | |
"use strict"; | |
/* Dependencies */ | |
var $ = require('jquery'); | |
var handlebars = require('handlebars'); | |
/** | |
* Populates the entry-template. | |
* @public | |
*/ | |
templater.entryTemplate = function () { | |
var data = { | |
title: 'My New Post', | |
body: 'This is my first post!' | |
}; | |
handleBarTemplate('#entry-template', '.entry', data); | |
}; | |
/** | |
* Populates the block expression. | |
* @public | |
*/ | |
templater.blockExpression = function () { | |
var data = { | |
customers: [ | |
{ firstName: "Yehuda", lastName: "Katz" }, | |
{ firstName: "Carl", lastName: "Lerche" }, | |
{ firstName: "Alan", lastName: "Johnson" } | |
] | |
}; | |
handleBarTemplate('#customers-template', '.customers', data); | |
}; | |
/* | |
* Gets a template, populate it with data, and insert into target selector. | |
* @private | |
*/ | |
function handleBarTemplate(templateSelector, insertSelector, data) { | |
var source = $(templateSelector).html(); | |
var template = handlebars.compile(source); | |
var html = template(data); | |
$(insertSelector).append(html); | |
} | |
return templater; | |
}({})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment