Added an inline render mode.
This commit is contained in:
committed by
Jannis Leidel
parent
72338c80ed
commit
35cd608239
@@ -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)
|
||||
|
||||
1
compressor/templates/compressor/css_inline.html
Normal file
1
compressor/templates/compressor/css_inline.html
Normal file
@@ -0,0 +1 @@
|
||||
<style type="text/css">{{ content }}</style>
|
||||
1
compressor/templates/compressor/js_inline.html
Normal file
1
compressor/templates/compressor/js_inline.html
Normal file
@@ -0,0 +1 @@
|
||||
<script type="text/javascript">{{ content }}</script>
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user