Skip to content

Instantly share code, notes, and snippets.

@Deepwalker
Created March 28, 2016 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Deepwalker/6ecd03dd1c1a4c8d128c to your computer and use it in GitHub Desktop.
Save Deepwalker/6ecd03dd1c1a4c8d128c to your computer and use it in GitHub Desktop.
from webassets.filter import Filter
class AngularTemplatesFilter(Filter):
input_template = "$templateCache.put('%s', '%s');"
output_template = """
(function() {
angular.module('%s').run(['$templateCache', function($templateCache) {
%s
}]);
})();
"""
def __init__(self, template_module, **kwargs):
super().__init__(**kwargs)
self.template_module = template_module
def output(self, _in, out, **kwargs):
out.write(self.output_template % (self.template_module, _in.read()))
def input(self, _in, out, **kwargs):
source_path = kwargs['source_path']
rel_path = os.path.relpath(source_path, application.root_path)
path = os.path.join('/', rel_path)
data = _in.read().replace("\n", "\\n").replace("'", "\\'")
out.write(self.input_template % (path, data))
assets.register('application_templates',
Bundle(
'angular/templates/some_template.html',
'angular/templates/some_template.html',
'angular/templates/some_template.html',
'angular/templates/some_template.html',
'angular/templates/some_template.html',
'angular/templates/some_template.html',
'angular/templates/some_template.html',
filters=AngularTemplatesFilter('MyApp.templates'),
output='assets/my-app-templates.js'
),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment