| 117 | class RecursiveIncludeNode(Node): |
| 118 | cache = {} |
| 119 | @staticmethod |
| 120 | def _get_template(template_name): |
| 121 | if not RecursiveIncludeNode._has_template(template_name): |
| 122 | RecursiveIncludeNode._load_template(template_name) |
| 123 | return RecursiveIncludeNode.cache[template_name] |
| 124 | @staticmethod |
| 125 | def _has_template(template_name): |
| 126 | return RecursiveIncludeNode.cache.has_key(template_name) |
| 127 | @staticmethod |
| 128 | def _load_template(template_name): |
| 129 | RecursiveIncludeNode.cache[template_name] = None |
| 130 | try: |
| 131 | RecursiveIncludeNode.cache[template_name] = get_template(template_name) |
| 132 | except: |
| 133 | del RecursiveIncludeNode.cache[template_name] |
| 134 | raise |
| 135 | def __init__(self, template_name): |
| 136 | self.template_name = Variable(template_name) |
| 137 | def render(self, context): |
| 138 | try: |
| 139 | template_name = self.template_name.resolve(context) |
| 140 | t = RecursiveIncludeNode._get_template(template_name) |
| 141 | return t.render(context) |
| 142 | except TemplateSyntaxError, e: |
| 143 | if settings.TEMPLATE_DEBUG: |
| 144 | raise |
| 145 | return '' |
| 146 | except: |
| 147 | return '' # Fail silently for invalid included templates. |
| 148 | |
| 149 | def clear_template_include_cache(): |
| 150 | RecursiveIncludeNode.cache = {} |
| 151 | |
| 152 | dispatcher.connect(clear_template_include_cache, signal=signals.request_finished) |
| 153 | dispatcher.connect(clear_template_include_cache, signal=signals.got_request_exception) |
| 154 | |
| 213 | |
| 214 | def rec_include(parser, token): |
| 215 | bits = token.contents.split() |
| 216 | if len(bits) != 2: |
| 217 | raise TemplateSyntaxError, "%r tag takes one argument: the name of the template to be included" % bits[0] |
| 218 | path = bits[1] |
| 219 | from django.template.loader_tags import IncludeNode |
| 220 | if path[0] in ('"', "'") and path[-1] == path[0]: |
| 221 | template_name = path[1:-1] |
| 222 | if not RecursiveIncludeNode._has_template(template_name): |
| 223 | RecursiveIncludeNode._load_template(template_name) |
| 224 | return RecursiveIncludeNode(bits[1]) |