1 from django import template
2 from django.contrib.contenttypes.models import ContentType
3 from django.conf import settings
4 from philo.utils import LOADED_TEMPLATE_ATTR
7 register = template.Library()
10 class EmbedNode(template.Node):
11 def __init__(self, content_type, varname, object_pk=None, template_name=None, kwargs=None):
12 assert template_name is not None or object_pk is not None
13 self.content_type = content_type
14 self.varname = varname
17 for k, v in kwargs.items():
18 kwargs[k] = template.Variable(v)
21 if object_pk is not None:
22 self.object_pk = object_pk
24 self.instance = content_type.get_object_for_this_type(pk=object_pk)
25 except content_type.model_class().DoesNotExist:
30 if template_name is not None:
32 self.template = template.loader.get_template(template_name)
33 except template.TemplateDoesNotExist:
38 def render(self, context):
39 if self.template_name is not None:
40 if self.template is False:
41 return settings.TEMPLATE_STRING_IF_INVALID
43 if self.varname not in context:
44 context[self.varname] = {}
45 context[self.varname][self.content_type] = self.template
49 # Otherwise self.instance should be set. Render the instance with the appropriate template!
50 if self.instance is None or self.instance is False:
51 return settings.TEMPLATE_STRING_IF_INVALID
54 t = context[self.varname][self.content_type]
56 return settings.TEMPLATE_STRING_IF_INVALID
59 context['embedded'] = self.instance
60 for k, v in self.kwargs.items():
61 self.kwargs[k] = v.resolve(context)
62 context.update(self.kwargs)
63 t_rendered = t.render(context)
68 def get_embedded(self):
69 return template.loader.get_template(self.template_name)
72 setattr(EmbedNode, LOADED_TEMPLATE_ATTR, property(get_embedded))
75 def do_embed(parser, token):
77 The {% embed %} tag can be used in three ways:
78 {% embed as <varname> %} :: This sets which variable will be used to track embedding template names for the current context. Default: "embed"
79 {% embed <app_label>.<model_name> with <template> %} :: Sets which template will be used to render a particular model.
80 {% embed <app_label>.<model_name> <object_pk> [<argname>=<value> ...]%} :: Embeds the instance specified by the given parameters in the document with the previously-specified template. Any kwargs provided will be passed into the context of the template.
82 args = token.split_contents()
86 raise template.TemplateSyntaxError('"%s" template tag must have at least three arguments.' % tag)
87 elif len(args) == 3 and args[1] == "as":
88 parser._embedNodeVarName = args[2]
89 return template.defaulttags.CommentNode()
91 if '.' not in args[1]:
92 raise template.TemplateSyntaxError('"%s" template tag expects the first argument to be of the form app_label.model' % tag)
94 app_label, model = args[1].split('.')
96 ct = ContentType.objects.get(app_label=app_label, model=model)
97 except ContentType.DoesNotExist:
98 raise template.TemplateSyntaxError('"%s" template tag option "references" requires an argument of the form app_label.model which refers to an installed content type (see django.contrib.contenttypes)' % tag)
100 if args[2] == "with":
102 raise template.TemplateSyntaxError('"%s" template tag may have no more than four arguments.' % tag)
104 if args[3][0] not in ['"', "'"] and args[3][-1] not in ['"', "'"]:
105 raise template.TemplateSyntaxError('"%s" template tag expects the template name to be in quotes.' % tag)
106 if args[3][0] != args[3][-1]:
107 raise template.TemplateSyntaxError('"%s" template tag called with non-matching quotes.' % tag)
109 template_name = args[3].strip('"\'')
111 return EmbedNode(ct, template_name=template_name, varname=getattr(parser, '_embedNodeVarName', 'embed'))
113 varname = getattr(parser, '_embedNodeVarName', 'embed')
115 remaining_args = args[3:]
117 for arg in remaining_args:
119 raise template.TemplateSyntaxError("Invalid keyword argument for '%s' template tag: %s" % (tag, arg))
120 k, v = arg.split('=')
123 return EmbedNode(ct, object_pk=object_pk, varname=varname, kwargs=kwargs)
126 register.tag('embed', do_embed)