Skip to content

Instantly share code, notes, and snippets.

@adambard
Created April 17, 2012 03:41
Show Gist options
  • Save adambard/2403238 to your computer and use it in GitHub Desktop.
Save adambard/2403238 to your computer and use it in GitHub Desktop.
Create template.js
#!/usr/bin/env python
"""
The filename is grandiose. All this does is find the files in the
web/templates/mustache directory, and dump them all into web/static/js/templates.js as
members of the window.templates object.
"""
import os
import re
import json
TEMPLATE_DIR = os.path.abspath("web/templates/mustache")
files = os.listdir(TEMPLATE_DIR)
first = True
templates = {}
for fn in files:
m = re.match(r"^(?P<varname>.*)\.mustache$", fn)
if m:
with open(os.path.join(TEMPLATE_DIR, fn), "rb") as f:
tmpl_str = f.read()
templates[m.group('varname')] = tmpl_str.replace("\n", " ")
with open("web/static/js/templates.js", "wb") as outfile:
outfile.write("window.templates = ")
outfile.write(json.dumps(templates))
outfile.write(";")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment