Anonymous avatar Anonymous committed 440b180

Added support for chunk rendering

Comments (0)

Files changed (1)

chunks/templatetags/chunks.py

         return content
 
 register.tag('chunk', do_get_chunk)
+
+@register.tag
+def render_chunk(parser, token):
+    """
+    Renders given chunk name
+    """
+    # split_contents() knows not to split quoted strings.
+    tokens = token.split_contents()
+    if len(tokens) < 2 or len(tokens) > 3:
+        raise template.TemplateSyntaxError, "%r tag should have either 2 or 3 arguments" % (tokens[0],)
+    if len(tokens) == 2:
+        tag_name, key = tokens
+        cache_time = 0
+    if len(tokens) == 3:
+        tag_name, key, cache_time = tokens
+    # Check to see if the key is properly double/single quoted
+    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 RenderChunkNode(key[1:-1], cache_time)
+
+
+class RenderChunkNode(template.Node):
+    def __init__(self, key, cache_time=0):
+        self.node = ChunkNode(key, cache_time)
+
+    def render(self, context):
+        try:
+            content = self.node.render(context)
+            t = template.Template(content)
+            return t.render(context)
+        except template.VariableDoesNotExist, template.TemplateSyntaxError:
+            return 'Error rendering', self.variable
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.