partial tag

 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def split_contents2(token):
	"""
	Is an updated way of splitting contents for a token,
	it does the split, but fixes the list.. 
	EX:
	From a tag call like this: {% partial "partials/template.html" %}
	usually it's like: ['partial','"partials/template.html"'] (notice the " double quotes)
	fixes it with: ['partial','partials/template.html'] (takes out the " quotes)
	"""
	value=token.split_contents()
	newvalues=[]
	for val in value:
		if (type(val)==types.UnicodeType or isinstance(val,str)) and val[0]=='"' and val[-1]=='"':
			val=re.sub(r'^\"','',val)
			val=re.sub(r'\"$','',val)
		newvalues.append(val)
	return newvalues

class PartialTemplateNode(Node):
	"""
	Node renderer for partial tag
	"""
	def __init__(self,template):
		self.template=template
	def render(self,context):
		template=loader.get_template(self.template)
		if context: return template.render(context)
		else: return template.render()

@register.tag
def partial(parser,token):
	"""
	Renders partials
	EX:
	{% load tags %}
	{% partial template-name %}
	"""
	tag,template_name=split_contents2(token)
	if not template_name: raise template.TemplateSyntaxError, "the 'partial' tag needs a template to render"
	return PartialTemplateNode(template_name)

More like this

  1. render_partial by hachaboob 5 years, 7 months ago
  2. split_contents2 for template tags by bl4th3rsk1t3 4 years, 10 months ago
  3. Partial templates, combine with and include by koblas 4 years ago
  4. Partial Tag by mnbayazit 3 years, 9 months ago
  5. "Partial Templates" - an alternative to "include" by vigrid 5 years, 2 months ago

Comments

carljm (on June 2, 2009):

Maybe I'm dense, but I don't understand how this is any different from the built-in "include" tag.

#

(Forgotten your password?)