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 ConstantEmbedNode(template.Node):
11 """Analogous to the ConstantIncludeNode, this node precompiles several variables necessary for correct rendering - namely the referenced instance or the included template."""
12 def __init__(self, content_type, varname, object_pk=None, template_name=None, kwargs=None):
13 assert template_name is not None or object_pk is not None
14 self.content_type = content_type
15 self.varname = varname
18 for k, v in kwargs.items():
19 kwargs[k] = template.Variable(v)
22 if object_pk is not None:
23 self.compile_instance(object_pk)
24 if self.instance is False and settings.TEMPLATE_DEBUG:
29 if template_name is not None:
30 self.compile_template(template_name[1:-1])
31 if self.template is False and settings.TEMPLATE_DEBUG:
36 def compile_instance(self, object_pk):
37 self.object_pk = object_pk
38 model = self.content_type.model_class()
40 self.instance = model.objects.get(pk=object_pk)
41 except model.DoesNotExist:
44 def compile_template(self, template_name):
46 self.template = template.loader.get_template(template_name)
47 except template.TemplateDoesNotExist:
50 def render(self, context):
51 if self.template is not None:
52 if self.template is False:
53 return settings.TEMPLATE_STRING_IF_INVALID
55 if self.varname not in context:
56 context[self.varname] = {}
57 context[self.varname][self.content_type] = self.template
61 # Otherwise self.instance should be set. Render the instance with the appropriate template!
62 if self.instance is None or self.instance is False:
63 return settings.TEMPLATE_STRING_IF_INVALID
65 return self.render_template(context, self.instance)
67 def render_template(self, context, instance):
69 t = context[self.varname][self.content_type]
71 return settings.TEMPLATE_STRING_IF_INVALID
74 context['embedded'] = instance
76 for k, v in self.kwargs.items():
77 kwargs[k] = v.resolve(context)
78 context.update(kwargs)
79 t_rendered = t.render(context)
84 class EmbedNode(ConstantEmbedNode):
85 def __init__(self, content_type, varname, object_pk=None, template_name=None, kwargs=None):
86 assert template_name is not None or object_pk is not None
87 self.content_type = content_type
88 self.varname = varname
91 for k, v in kwargs.items():
92 kwargs[k] = template.Variable(v)
95 if object_pk is not None:
96 self.object_pk = template.Variable(object_pk)
101 if template_name is not None:
102 self.template_name = template.Variable(template_name)
104 self.template_name = None
107 def render(self, context):
108 if self.template_name is not None:
109 template_name = self.template_name.resolve(context)
110 self.compile_template(template_name)
112 if self.object_pk is not None:
113 object_pk = self.object_pk.resolve(context)
114 self.compile_instance(object_pk)
116 return super(EmbedNode, self).render(context)
119 def get_embedded(self):
123 setattr(ConstantEmbedNode, LOADED_TEMPLATE_ATTR, property(get_embedded))
126 def do_embed(parser, token):
128 The {% embed %} tag can be used in three ways:
129 {% embed as <varname> %} :: This sets which variable will be used to track embedding template names for the current context. Default: "embed"
130 {% embed <app_label>.<model_name> with <template> %} :: Sets which template will be used to render a particular model.
131 {% 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.
133 args = token.split_contents()
137 raise template.TemplateSyntaxError('"%s" template tag must have at least three arguments.' % tag)
138 elif len(args) == 3 and args[1] == "as":
139 parser._embedNodeVarName = args[2]
140 return template.defaulttags.CommentNode()
142 if '.' not in args[1]:
143 raise template.TemplateSyntaxError('"%s" template tag expects the first argument to be of the form app_label.model' % tag)
145 app_label, model = args[1].split('.')
147 ct = ContentType.objects.get(app_label=app_label, model=model)
148 except ContentType.DoesNotExist:
149 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)
151 varname = getattr(parser, '_embedNodeVarName', 'embed')
153 if args[2] == "with":
155 raise template.TemplateSyntaxError('"%s" template tag may have no more than four arguments.' % tag)
157 if args[3][0] in ['"', "'"] and args[3][0] == args[3][-1]:
158 return ConstantEmbedNode(ct, template_name=args[3], varname=varname)
160 return EmbedNode(ct, template_name=args[3], varname=varname)
163 remaining_args = args[3:]
165 for arg in remaining_args:
167 raise template.TemplateSyntaxError("Invalid keyword argument for '%s' template tag: %s" % (tag, arg))
168 k, v = arg.split('=')
171 return EmbedNode(ct, object_pk=object_pk, varname=varname, kwargs=kwargs)
174 register.tag('embed', do_embed)