Calls the extender.
(self, node, frame)
| 841 | self.outdent(level) |
| 842 | |
| 843 | def visit_Extends(self, node, frame): |
| 844 | """Calls the extender.""" |
| 845 | if not frame.toplevel: |
| 846 | self.fail('cannot use extend from a non top-level scope', |
| 847 | node.lineno) |
| 848 | |
| 849 | # if the number of extends statements in general is zero so |
| 850 | # far, we don't have to add a check if something extended |
| 851 | # the template before this one. |
| 852 | if self.extends_so_far > 0: |
| 853 | |
| 854 | # if we have a known extends we just add a template runtime |
| 855 | # error into the generated code. We could catch that at compile |
| 856 | # time too, but i welcome it not to confuse users by throwing the |
| 857 | # same error at different times just "because we can". |
| 858 | if not self.has_known_extends: |
| 859 | self.writeline('if parent_template is not None:') |
| 860 | self.indent() |
| 861 | self.writeline('raise TemplateRuntimeError(%r)' % |
| 862 | 'extended multiple times') |
| 863 | |
| 864 | # if we have a known extends already we don't need that code here |
| 865 | # as we know that the template execution will end here. |
| 866 | if self.has_known_extends: |
| 867 | raise CompilerExit() |
| 868 | else: |
| 869 | self.outdent() |
| 870 | |
| 871 | self.writeline('parent_template = environment.get_template(', node) |
| 872 | self.visit(node.template, frame) |
| 873 | self.write(', %r)' % self.name) |
| 874 | self.writeline('for name, parent_block in parent_template.' |
| 875 | 'blocks.%s():' % dict_item_iter) |
| 876 | self.indent() |
| 877 | self.writeline('context.blocks.setdefault(name, []).' |
| 878 | 'append(parent_block)') |
| 879 | self.outdent() |
| 880 | |
| 881 | # if this extends statement was in the root level we can take |
| 882 | # advantage of that information and simplify the generated code |
| 883 | # in the top level from this point onwards |
| 884 | if frame.rootlevel: |
| 885 | self.has_known_extends = True |
| 886 | |
| 887 | # and now we have one more |
| 888 | self.extends_so_far += 1 |
| 889 | |
| 890 | def visit_Include(self, node, frame): |
| 891 | """Handles includes.""" |