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.
Last active
August 29, 2015 14:16
-
-
Save wd15/aade0e327c7494876e47 to your computer and use it in GitHub Desktop.
Using precompiled Handlebars and Coffeescript with D3
This file contains hidden or 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
| <!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> |
This file contains hidden or 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
| 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) |
This file contains hidden or 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
| 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)) |
This file contains hidden or 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
| .shift1 { | |
| text-indent: 50px; | |
| } | |
| .shift2 { | |
| text-indent: 100px; | |
| } | |
| .shift3 { | |
| text-indent: 150px; | |
| } |
This file contains hidden or 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
| (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