Created
May 28, 2010 20:51
-
-
Save wnielson/417732 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/chunks/templatetags/chunks.py b/chunks/templatetags/chunks.py | |
index 2b06cad..545b451 100644 | |
--- a/chunks/templatetags/chunks.py | |
+++ b/chunks/templatetags/chunks.py | |
@@ -7,7 +7,7 @@ register = template.Library() | |
Chunk = models.get_model('chunks', 'chunk') | |
CACHE_PREFIX = "chunk_" | |
-def do_get_chunk(parser, token): | |
+def parse_chunk_args(parser, token): | |
# split_contents() knows not to split quoted strings. | |
tokens = token.split_contents() | |
if len(tokens) < 2 or len(tokens) > 3: | |
@@ -21,23 +21,36 @@ def do_get_chunk(parser, token): | |
if not (key[0] == key[-1] and key[0] in ('"', "'")): | |
raise template.TemplateSyntaxError, "%r tag's argument should be in quotes" % tag_name | |
# Send key without quotes and caching time | |
- return ChunkNode(key[1:-1], cache_time) | |
- | |
+ return (key[1:-1], cache_time) | |
+ | |
+def do_get_chunk(parser, token): | |
+ args = parse_chunk_args(parser, token) | |
+ return ChunkNode(*args, include=False) | |
+ | |
+def do_get_include_chunk(parser, token): | |
+ args = parse_chunk_args(parser, token) | |
+ return ChunkNode(*args, include=True) | |
+ | |
class ChunkNode(template.Node): | |
- def __init__(self, key, cache_time=0): | |
+ def __init__(self, key, cache_time=0, include=False): | |
self.key = key | |
self.cache_time = cache_time | |
+ self.include = include | |
def render(self, context): | |
try: | |
cache_key = CACHE_PREFIX + self.key | |
- c = cache.get(cache_key) | |
- if c is None: | |
+ content = cache.get(cache_key) | |
+ if content is None: | |
c = Chunk.objects.get(key=self.key) | |
- cache.set(cache_key, c, int(self.cache_time)) | |
- content = c.content | |
+ content = c.content | |
+ if self.include: | |
+ t = template.Template(c.content) | |
+ content = t.render(context) | |
+ cache.set(cache_key, content, int(self.cache_time)) | |
except Chunk.DoesNotExist: | |
content = '' | |
return content | |
register.tag('chunk', do_get_chunk) | |
+register.tag('include_chunk', do_get_include_chunk) | |
\ No newline at end of file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment