dijxtra wrote:
> and here is what I want to put in my template:
>
> {% url day year={% now "Y"%} month={% now "m"%} day={% now "d"%} %}
>
> but, ofcourse, I won't put that in my template because you can't have
> tag inside of a tag.
This may fall into the "just because you can doesn't mean you
should" category, but, well...
A sufficiently underhanded custom templatetag can do anything. Doing
this sort of thing at a string level tends to lead to backslash
nightmares, but is easy enough really:
{% load djexpand_tags %}
<p>
{% expand 1 "\\{\\% url day year=\{\% now \"Y\" \%\} month=\{\% now
\"m\" \%\} day=\{\% now \"d\" \%\} \\%\\}" %}
</p>
<p>
{% expand 2 "\\{\\% url day year=\{\% now \"Y\" \%\} month=\{\% now
\"m\" \%\} day=\{\% now \"d\" \%\} \\%\\}" %}
</p>
# djexpand/templatetags/djexpand_tags.py:
from django import template
import re
register=template.Library()
class ExpandNode(template.Node):
def __init__(self, iterations, template_string):
self.iterations = iterations
self.template_string = template_string
def render(self, context):
s = self.template_string
# could just repeat until fixed point reached if you wanted...
for i in range(0, self.iterations):
# this is perhaps overly simplistic...
unesc = re.sub(r'\\(.)', r'\1', s)
t = template.Template(unesc)
s = t.render(context)
return s
@register.tag
def expand(parser, token):
try:
# split contents is apparently smart enough.
tag_name, iterations, template_string = token.split_contents()
except ValueError:
raise template.TemplateSyntaxError, \
"%r tag requires two arguments" % token.contents.split()[0]
try:
i = int(iterations)
except ValueError:
raise template.TemplateSyntaxError, \
"%r tag first argument should be an int" % tag_name
if not (template_string[0] == template_string[-1]
and template_string[0] in ('"',"'")):
raise template.TemplateSyntaxError, \
"%r tag second argument should be in quotes" % tag_name
return ExpandNode(i, template_string[1:-1])
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---