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)
Comments
Maybe I'm dense, but I don't understand how this is any different from the built-in "include" tag.
#