Skip to content

Instantly share code, notes, and snippets.

@dqminh
Created May 25, 2011 07:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dqminh/990487 to your computer and use it in GitHub Desktop.
Save dqminh/990487 to your computer and use it in GitHub Desktop.
JS template filter for webassets
"""
Webasset filter to combine JQuery Templates files into javascript-safe strings,
and put them into a namespace
"""
import os
from webassets.filter import Filter
class JqtFilter(Filter):
name = 'jqt'
def setup(self):
self.compiled = []
self.before = "(function(){"
self.after = "})();"
self.namespace = "JQT"
self.extension = ".jqt"
def input(self, _in, out, source_path, output_path):
# just pass the strings
fmt = "%(namespace)s['%(name)s']='%(template)s';"
item = {
'namespace': self.namespace,
'template': _in.read().replace('\n', '').replace("'", r"\'"),
'name': os.path.basename(source_path).replace(self.extension, "")
}
self.compiled.append(fmt % item)
def output(self, _in, out, **kw):
# setup namespace
namespace = "%s=%s||{};" % (self.namespace, self.namespace)
output = self.before + namespace + "".join(self.compiled) + self.after
out.write(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment