|
6 | 6 |
|
7 | 7 | from static_precompiler import settings, utils |
8 | 8 |
|
9 | | -from . import base |
10 | | - |
11 | 9 | register = django.template.Library() |
12 | 10 |
|
13 | 11 |
|
@@ -35,24 +33,50 @@ def compile_tag(source_path, compiler=None): |
35 | 33 | return compiled |
36 | 34 |
|
37 | 35 |
|
38 | | -@base.container_tag(register) |
39 | | -def inlinecompile(nodelist, context, compiler): |
40 | | - source = nodelist.render(context) |
| 36 | +class InlineCompileNode(django.template.Node): |
| 37 | + def __init__(self, nodelist, compiler): |
| 38 | + self.nodelist = nodelist |
| 39 | + self.compiler = compiler |
| 40 | + |
| 41 | + def render(self, context): |
| 42 | + source = self.nodelist.render(context) |
| 43 | + |
| 44 | + if self.compiler[0] == self.compiler[-1] and self.compiler[0] in ('"', "'"): |
| 45 | + compiler = self.compiler[1:-1] |
| 46 | + else: |
| 47 | + compiler = django.template.Variable(self.compiler).resolve(context) |
| 48 | + |
| 49 | + if isinstance(compiler, six.string_types): |
| 50 | + compiler = utils.get_compiler_by_name(compiler) |
| 51 | + |
| 52 | + if settings.USE_CACHE: |
| 53 | + cache_key = utils.get_cache_key("{0}.{1}".format( |
| 54 | + compiler.__class__.__name__, |
| 55 | + utils.get_hexdigest(source) |
| 56 | + )) |
| 57 | + cache = utils.get_cache() |
| 58 | + cached = cache.get(cache_key, None) |
| 59 | + if cached is not None: |
| 60 | + return cached |
| 61 | + output = compiler.compile_source(source) |
| 62 | + cache.set(cache_key, output, settings.CACHE_TIMEOUT) |
| 63 | + return output |
41 | 64 |
|
42 | | - if isinstance(compiler, six.string_types): |
43 | | - compiler = utils.get_compiler_by_name(compiler) |
| 65 | + return compiler.compile_source(source) |
44 | 66 |
|
45 | | - if settings.USE_CACHE: |
46 | | - cache_key = utils.get_cache_key("{0}.{1}".format( |
47 | | - compiler.__class__.__name__, |
48 | | - utils.get_hexdigest(source) |
49 | | - )) |
50 | | - cache = utils.get_cache() |
51 | | - cached = cache.get(cache_key, None) |
52 | | - if cached is not None: |
53 | | - return cached |
54 | | - output = compiler.compile_source(source) |
55 | | - cache.set(cache_key, output, settings.CACHE_TIMEOUT) |
56 | | - return output |
57 | 67 |
|
58 | | - return compiler.compile_source(source) |
| 68 | +@register.tag |
| 69 | +def inlinecompile(parser, token): |
| 70 | + bits = token.split_contents() |
| 71 | + tag_name = bits[0] |
| 72 | + try: |
| 73 | + compiler, = bits[1:] |
| 74 | + except ValueError: |
| 75 | + raise django.template.TemplateSyntaxError( |
| 76 | + '%r tag requires exactly one argument.' % tag_name |
| 77 | + ) |
| 78 | + if compiler.startswith('compiler='): |
| 79 | + compiler = compiler[len('compiler='):] |
| 80 | + nodelist = parser.parse(('end' + tag_name,)) |
| 81 | + parser.delete_first_token() |
| 82 | + return InlineCompileNode(nodelist, compiler) |
0 commit comments