Skip to content

Instantly share code, notes, and snippets.

@wd15
Last active August 29, 2015 14:16
Show Gist options
  • Select an option

  • Save wd15/aade0e327c7494876e47 to your computer and use it in GitHub Desktop.

Select an option

Save wd15/aade0e327c7494876e47 to your computer and use it in GitHub Desktop.
Using precompiled Handlebars and Coffeescript with D3

Using Handlebars and Coffeescript with D3

This Gist demonstrates how to use D3 with with precompiled Handlebars templates. A Makefile is used to precompile the templates into template.js. My goal here was to understand how to keep separate files with Handlebar templates and to render that template using D3.

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" ccoontent="text/html;charset=utf-8">
<title>Trivial example</title>
<script src="//cdn.jsdelivr.net/g/d3js,coffeescript,handlebarsjs"></script>
<style>
@import "/style.css";
</style>
</head>
<body>
<script src="template.js"></script>
<script type="text/coffeescript" src="names.coffee"></script>
</body>
</html>
TEMPLATE_DIR = .
TEMPLATES = $(wildcard $(TEMPLATE_DIR)/*.handlebars)
TEMPLATE_JS = ./template.js
.PHONY: handlebars clean
all: handlebars
clean:
rm -rf $(TEMPLATE_JS)
$(TEMPLATE_JS): $(TEMPLATES)
handlebars $(TEMPLATES) -f $(TEMPLATE_JS)
handlebars: $(TEMPLATE_JS)
theData = [
{
class: 'shift1'
name0: 'tim'
name1: 'tom'
name2: 'james'
}
{
class: 'shift2'
name0: 'tracy'
name1: 'pippy'
name2: 'loki'
}
{
class: 'shift3'
name0: 'luna'
name1: 'chloe'
name2: 'jamie'
}
]
template = Handlebars.templates.names
d3.select("body").selectAll("p")
.data(theData)
.enter()
.append("div")
.html((d) -> template(d))
<div class={{ class }}>
<p>{{ name0 }}</p>
<p>{{ name1 }}</p>
<p>{{ name2 }}</p>
</div>
.shift1 {
text-indent: 50px;
}
.shift2 {
text-indent: 100px;
}
.shift3 {
text-indent: 150px;
}
(function() {
var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
templates['names'] = template({"compiler":[6,">= 2.0.0-beta.1"],"main":function(depth0,helpers,partials,data) {
var helper, alias1=helpers.helperMissing, alias2="function", alias3=this.escapeExpression;
return "<div class="
+ alias3(((helper = (helper = helpers['class'] || (depth0 != null ? depth0['class'] : depth0)) != null ? helper : alias1),(typeof helper === alias2 ? helper.call(depth0,{"name":"class","hash":{},"data":data}) : helper)))
+ ">\n <p>"
+ alias3(((helper = (helper = helpers.name0 || (depth0 != null ? depth0.name0 : depth0)) != null ? helper : alias1),(typeof helper === alias2 ? helper.call(depth0,{"name":"name0","hash":{},"data":data}) : helper)))
+ "</p> \n <p>"
+ alias3(((helper = (helper = helpers.name1 || (depth0 != null ? depth0.name1 : depth0)) != null ? helper : alias1),(typeof helper === alias2 ? helper.call(depth0,{"name":"name1","hash":{},"data":data}) : helper)))
+ "</p> \n <p>"
+ alias3(((helper = (helper = helpers.name2 || (depth0 != null ? depth0.name2 : depth0)) != null ? helper : alias1),(typeof helper === alias2 ? helper.call(depth0,{"name":"name2","hash":{},"data":data}) : helper)))
+ "</p> \n</div>\n\n";
},"useData":true});
templates['template'] = template({"compiler":[6,">= 2.0.0-beta.1"],"main":function(depth0,helpers,partials,data) {
var helper, alias1=helpers.helperMissing, alias2="function", alias3=this.escapeExpression;
return "<div class="
+ alias3(((helper = (helper = helpers['class'] || (depth0 != null ? depth0['class'] : depth0)) != null ? helper : alias1),(typeof helper === alias2 ? helper.call(depth0,{"name":"class","hash":{},"data":data}) : helper)))
+ ">\n <p>"
+ alias3(((helper = (helper = helpers.name0 || (depth0 != null ? depth0.name0 : depth0)) != null ? helper : alias1),(typeof helper === alias2 ? helper.call(depth0,{"name":"name0","hash":{},"data":data}) : helper)))
+ "</p> \n <p>"
+ alias3(((helper = (helper = helpers.name1 || (depth0 != null ? depth0.name1 : depth0)) != null ? helper : alias1),(typeof helper === alias2 ? helper.call(depth0,{"name":"name1","hash":{},"data":data}) : helper)))
+ "</p> \n <p>"
+ alias3(((helper = (helper = helpers.name2 || (depth0 != null ? depth0.name2 : depth0)) != null ? helper : alias1),(typeof helper === alias2 ? helper.call(depth0,{"name":"name2","hash":{},"data":data}) : helper)))
+ "</p> \n</div>\n\n";
},"useData":true});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment