Created
May 25, 2011 07:04
-
-
Save dqminh/990487 to your computer and use it in GitHub Desktop.
JS template filter for webassets
This file contains 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
""" | |
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