I have duplicated as best I can the processing for handling variables
in custom template tags mentioned in an earlier post but it's not
working for me as I need the value of the variable before I parse it.
Is there another way of making the variable available?
The tag is being called from the tempalte with:
{% page_links "category" %}
Where category is being passed into the template from a view. (also
tried "{{category}}" and {{category}} and category)
I want to use the value of category to determine which list of pages
to return.
I KNOW THIS DOESN"T WORK:
@register.tag(name="page_links")
def get_page_links(parser,token):
tag_name, cat = token.contents.split()
return PageLink(cat[1:-1])
class PageLink(template.Node):
def __init__(self, cat):
cat= template.Variable(cat)
print cat <--------------------- outputs category
if cat=='business':
pages=BusinessPage.objects.filter
(is_live=True).select_related().order_by('order')
elif cat=='community':
pages=CommunityPage.objects.filter
(is_live=True).select_related().order_by('order')
elif cat=='tourism':
pages=TourismPage.objects.filter
(is_live=True).select_related().order_by('order')
else:
pages = False
self.pages = pages
def render(self, context):
context['pages'] = self.pages
return ''
This code works fine as long as I call it like this:
{% page_links "business" %}
@register.tag(name="page_links")
def get_page_links(parser,token):
tag_name, cat = token.contents.split()
cat = cat[1:-1]
if cat=='business':
pages=BusinessPage.objects.filter
(is_live=True).select_related().order_by('order')
elif cat=='community':
pages=CommunityPage.objects.filter
(is_live=True).select_related().order_by('order')
elif cat=='tourism':
pages=TourismPage.objects.filter
(is_live=True).select_related().order_by('order')
else:
pages = False
return PageLink(pages)
class PageLink(template.Node):
def __init__(self, pages):
self.pages = pages
def render(self, context):
context['pages'] = self.pages
return ''
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---