Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MartinJHammer/a6fb91f66069c1b770aac080b5971adb to your computer and use it in GitHub Desktop.
Save MartinJHammer/a6fb91f66069c1b770aac080b5971adb to your computer and use it in GitHub Desktop.
/*
* 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