diff --git a/compressor/__init__.py b/compressor/__init__.py index e52b92a..248bbe3 100644 --- a/compressor/__init__.py +++ b/compressor/__init__.py @@ -151,12 +151,15 @@ class Compressor(object): context['url'] = self.storage.url(self.new_filepath) return render_to_string(self.template_name, context) + def output_inline(self): + return render_to_string(self.template_name_inline, {'content': settings.COMPRESS and self.combined or self.concat()}) class CssCompressor(Compressor): def __init__(self, content, output_prefix="css"): self.extension = ".css" self.template_name = "compressor/css.html" + self.template_name_inline = "compressor/css_inline.html" self.filters = ['compressor.filters.css_default.CssAbsoluteFilter'] self.filters.extend(settings.COMPRESS_CSS_FILTERS) self.type = 'css' @@ -208,6 +211,7 @@ class JsCompressor(Compressor): def __init__(self, content, output_prefix="js"): self.extension = ".js" self.template_name = "compressor/js.html" + self.template_name_inline = "compressor/js_inline.html" self.filters = settings.COMPRESS_JS_FILTERS self.type = 'js' super(JsCompressor, self).__init__(content, output_prefix) diff --git a/compressor/templates/compressor/css_inline.html b/compressor/templates/compressor/css_inline.html new file mode 100644 index 0000000..83253f8 --- /dev/null +++ b/compressor/templates/compressor/css_inline.html @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/compressor/templates/compressor/js_inline.html b/compressor/templates/compressor/js_inline.html new file mode 100644 index 0000000..6931349 --- /dev/null +++ b/compressor/templates/compressor/js_inline.html @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/compressor/templatetags/compress.py b/compressor/templatetags/compress.py index ae45bdd..ee6526a 100644 --- a/compressor/templatetags/compress.py +++ b/compressor/templatetags/compress.py @@ -5,12 +5,17 @@ from django.core.cache import cache from compressor import CssCompressor, JsCompressor from compressor.conf import settings + +OUTPUT_FILE = 'file' +OUTPUT_INLINE = 'inline' + register = template.Library() class CompressorNode(template.Node): - def __init__(self, nodelist, kind=None): + def __init__(self, nodelist, kind=None, mode=OUTPUT_FILE): self.nodelist = nodelist self.kind = kind + self.mode = mode def cache_get(self, key): packed_val = cache.get(key) @@ -41,7 +46,10 @@ class CompressorNode(template.Node): output = self.cache_get(compressor.cachekey) if output is None: try: - output = compressor.output() + if self.mode == OUTPUT_FILE: + output = compressor.output() + else: + output = compressor.output_inline() self.cache_set(compressor.cachekey, output) except: from traceback import format_exc @@ -92,11 +100,18 @@ def compress(parser, token): args = token.split_contents() - if not len(args) == 2: - raise template.TemplateSyntaxError("%r tag requires either 1, 3 or 5 arguments." % args[0]) + if not len(args) in (2, 3): + raise template.TemplateSyntaxError("%r tag requires either one or two arguments." % args[0]) kind = args[1] if not kind in ['css', 'js']: raise template.TemplateSyntaxError("%r's argument must be 'js' or 'css'." % args[0]) - return CompressorNode(nodelist, kind) + if len(args) == 3: + mode = args[2] + if not mode in (OUTPUT_FILE, OUTPUT_INLINE): + raise template.TemplateSyntaxError("%r's second argument must be '%s' or '%s'." % (args[0], OUTPUT_FILE, OUTPUT_INLINE)) + else: + mode = OUTPUT_FILE + + return CompressorNode(nodelist, kind, mode)