render_partial

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from django import template

register = template.Library()

class RenderPartialNode(template.Node):
	def __init__(self, object_name, template_name=None):
		self.object_name = object_name
		self.template_name = template_name
	def render(self, context):
		if not self.template_name:
			self.template_name = 'partials/' + context[self.object_name].__class__.__name__.lower() + '.html'
		t = template.loader.get_template(self.template_name)
		return t.render(context)

	
def do_render_partial(parser, token):
	bits = token.split_contents()
	if len(bits) == 2:
		return RenderPartialNode(bits[1])
	elif len(bits) == 3:
		return RenderPartialNode(bits[1], bits[2])
	else:
		raise template.TemplateSyntaxError, "%r requires one or two arguments" % bits[0]

register.tag('render_partial', do_render_partial)

More like this

  1. inclusion tag with template as variable by forgems 4 years, 3 months ago
  2. partial tag by bl4th3rsk1t3 3 years, 11 months ago
  3. Feed Reader Inclusion Tag by baumer1122 5 years, 10 months ago
  4. "Partial Templates" - an alternative to "include" by vigrid 4 years, 3 months ago
  5. Subdirectory and subcontext include template tag with examples by t_rybik 3 years, 2 months ago

Comments

(Forgotten your password?)