| 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 | self.nodelist = None |
| 170 | |
| 171 | def render(self, context): |
| 172 | if 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 | |
| 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 | |