=== django/core/template/loader.py
==================================================================
|
|
|
160 | 160 | parent_block.nodelist = block_node.nodelist |
161 | 161 | return compiled_parent.render(context) |
162 | 162 | |
| 163 | class ConstantIncludeNode(Node): |
| 164 | def __init__(self, template_path): |
| 165 | try: |
| 166 | t = get_template(template_path) |
| 167 | self.nodelist = t.nodelist |
| 168 | except Exception, e: |
| 169 | pass |
| 170 | |
| 171 | def render(self, context): |
| 172 | if hasattr(self, 'nodelist'): |
| 173 | return self.nodelist.render(context) |
| 174 | else: |
| 175 | return '' |
| 176 | |
| 177 | class IncludeNode(Node): |
| 178 | def __init__(self, template_path_var): |
| 179 | self.template_path_var = template_path_var |
| 180 | |
| 181 | def render(self, context): |
| 182 | try: |
| 183 | template_path = resolve_variable(self.template_path_var, context) |
| 184 | print "IncludeNode rendering %s" % template_path |
| 185 | t = template_loader.get_template(template_path) |
| 186 | return t.render(context) |
| 187 | except Exception, e: |
| 188 | return '' # Fail silently for invalid included templates. |
| 189 | |
| 190 | |
163 | 191 | def do_block(parser, token): |
164 | 192 | """ |
165 | 193 | Define a block that can be overridden by child templates. |
… |
… |
|
202 | 230 | raise TemplateSyntaxError, "'%s' cannot appear more than once in the same template" % bits[0] |
203 | 231 | return ExtendsNode(nodelist, parent_name, parent_name_var) |
204 | 232 | |
| 233 | def do_include(parser, token): |
| 234 | """ |
| 235 | Loads a template using standard resolution mechanisms, and renders it in the current context. |
| 236 | """ |
| 237 | bits = token.contents.split() |
| 238 | if len(bits) != 2: |
| 239 | raise TemplateSyntaxError, "'include' tag takes one argument: the path to the template to be included" |
| 240 | |
| 241 | path = bits[1] |
| 242 | if path[0] in ('"', "'") and path[-1] == path[0]: |
| 243 | return ConstantIncludeNode(path[1:-1]) |
| 244 | return IncludeNode(bits[1]) |
| 245 | |
205 | 246 | register_tag('block', do_block) |
206 | 247 | register_tag('extends', do_extends) |
| 248 | register_tag('include', do_include) |