Skip to content

Commit 35cd608

Browse files
beniwohlijezdez
authored andcommitted
Added an inline render mode.
1 parent 72338c8 commit 35cd608

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

compressor/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,15 @@ def output(self):
151151
context['url'] = self.storage.url(self.new_filepath)
152152
return render_to_string(self.template_name, context)
153153

154+
def output_inline(self):
155+
return render_to_string(self.template_name_inline, {'content': settings.COMPRESS and self.combined or self.concat()})
154156

155157
class CssCompressor(Compressor):
156158

157159
def __init__(self, content, output_prefix="css"):
158160
self.extension = ".css"
159161
self.template_name = "compressor/css.html"
162+
self.template_name_inline = "compressor/css_inline.html"
160163
self.filters = ['compressor.filters.css_default.CssAbsoluteFilter']
161164
self.filters.extend(settings.COMPRESS_CSS_FILTERS)
162165
self.type = 'css'
@@ -208,6 +211,7 @@ class JsCompressor(Compressor):
208211
def __init__(self, content, output_prefix="js"):
209212
self.extension = ".js"
210213
self.template_name = "compressor/js.html"
214+
self.template_name_inline = "compressor/js_inline.html"
211215
self.filters = settings.COMPRESS_JS_FILTERS
212216
self.type = 'js'
213217
super(JsCompressor, self).__init__(content, output_prefix)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<style type="text/css">{{ content }}</style>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<script type="text/javascript">{{ content }}</script>

compressor/templatetags/compress.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55
from compressor import CssCompressor, JsCompressor
66
from compressor.conf import settings
77

8+
9+
OUTPUT_FILE = 'file'
10+
OUTPUT_INLINE = 'inline'
11+
812
register = template.Library()
913

1014
class CompressorNode(template.Node):
11-
def __init__(self, nodelist, kind=None):
15+
def __init__(self, nodelist, kind=None, mode=OUTPUT_FILE):
1216
self.nodelist = nodelist
1317
self.kind = kind
18+
self.mode = mode
1419

1520
def cache_get(self, key):
1621
packed_val = cache.get(key)
@@ -41,7 +46,10 @@ def render(self, context):
4146
output = self.cache_get(compressor.cachekey)
4247
if output is None:
4348
try:
44-
output = compressor.output()
49+
if self.mode == OUTPUT_FILE:
50+
output = compressor.output()
51+
else:
52+
output = compressor.output_inline()
4553
self.cache_set(compressor.cachekey, output)
4654
except:
4755
from traceback import format_exc
@@ -92,11 +100,18 @@ def compress(parser, token):
92100

93101
args = token.split_contents()
94102

95-
if not len(args) == 2:
96-
raise template.TemplateSyntaxError("%r tag requires either 1, 3 or 5 arguments." % args[0])
103+
if not len(args) in (2, 3):
104+
raise template.TemplateSyntaxError("%r tag requires either one or two arguments." % args[0])
97105

98106
kind = args[1]
99107
if not kind in ['css', 'js']:
100108
raise template.TemplateSyntaxError("%r's argument must be 'js' or 'css'." % args[0])
101109

102-
return CompressorNode(nodelist, kind)
110+
if len(args) == 3:
111+
mode = args[2]
112+
if not mode in (OUTPUT_FILE, OUTPUT_INLINE):
113+
raise template.TemplateSyntaxError("%r's second argument must be '%s' or '%s'." % (args[0], OUTPUT_FILE, OUTPUT_INLINE))
114+
else:
115+
mode = OUTPUT_FILE
116+
117+
return CompressorNode(nodelist, kind, mode)

0 commit comments

Comments
 (0)