Skip to content
This repository

Add a Template.render_blocks method #120

Closed
wants to merge 1 commit into from

2 participants

John Simon Armin Ronacher
John Simon

I wrote a method to render each block of a template, and return them all in a dict. Would you consider merging this in if it had proper tests/docs?

Armin Ronacher
Owner

Closing this for the time being since I have not processed it in a long time. Do you still think it's useful?

Armin Ronacher mitsuhiko closed this May 19, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Apr 25, 2012
John Simon Added the Template.render_blocks method 6a19fb3
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 16 additions and 0 deletions. Show diff stats Hide diff stats

  1. 16  jinja2/environment.py
16  jinja2/environment.py
@@ -893,6 +893,22 @@ def render(self, *args, **kwargs):
893 893
             exc_info = sys.exc_info()
894 894
         return self.environment.handle_exception(exc_info, True)
895 895
 
  896
+    def render_blocks(self, *args, **kwargs):
  897
+        """
  898
+        This method renders each block of the template, and returns a dict where
  899
+        the keys are the block names, and the values are the corresponding
  900
+        rendered content.
  901
+        """
  902
+        context = self.new_context(dict(*args, **kwargs))
  903
+        try:
  904
+            blocks = {}
  905
+            for name, func in self.blocks.iteritems():
  906
+                blocks[name] = concat(func(context))
  907
+            return blocks
  908
+        except Exception:
  909
+            exc_info = sys.exc_info()
  910
+        return self.environment.handle_exception(exc_info, True)
  911
+
896 912
     def stream(self, *args, **kwargs):
897 913
         """Works exactly like :meth:`generate` but returns a
898 914
         :class:`TemplateStream`.
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.