You understood perfectly. It was just another case of my not understanding the documentation. Your explanation makes perfect sense.
Many thanks. On Jan 27, 6:45 pm, Eric Abrahamsen <gir...@gmail.com> wrote: > On Jan 28, 2009, at 1:52 AM, phoebebright wrote: > > > > > > > I have duplicated as best I can the processing for handling variables > > incustomtemplatetagsmentioned 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 thetemplatefrom 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 '' > > I might be missing something you're trying to do here, but the usual > method for resolving a variable in acustomtemplatetag is to first > put it into the tag (without quotes) like so: > {% page_links category %} > > Then attach that to your PageLink node in its __init__ method as a > Variable: > self.cat =template.Variable(cat) > > Then inside the render method first resolve it: > category = self.cat.resolve(context) > > and then check the value of category against your various models. That > all has to be done in render, not __init__, because context is only > available in render. > > This is all straight out of the > docs:http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#pass... > > Apologies in advance if I've misunderstood what you're trying to do... > > Eric > > > > > 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 django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---