platzhersh
platzhersh

Reputation: 1582

Problems with simple_tag array values in Django Template

I'm trying to pass an array to my template with a simple tag. I created my module under app/templatetags/pages_navigation.py and in my opinion the code should be alright:

from django import template
from pages.models import Page

register = template.Library()

@register.simple_tag(name='links')
def pages_navigation():
  pages = Page.objects.all()
  links = [['Events','/']]

  for page in pages:
    links.append([page.title, '/'+page.url])

  return {'links':links}

In my template I try to access links like this:

<ul>
{% if links %}
  {% for link in links %}
    <a href="{{link.1}}"><li>{{link.0}}</li></a>
  {% endfor %}
{% else %}
  <li>no pages found</li>
{% endif%}
</ul>

However, somehow it seems like links is always empty. When I tried the pages_navigation method in the python shell it worked fine..

Is it possible that you can't return arrays from simple tag methods?

Upvotes: 0

Views: 2469

Answers (2)

okm
okm

Reputation: 23871

In new Django 1.4, you could use Assignment Tags https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#howto-custom-template-tags-assignment-tags

from django import template

register = template.Library()

@register.assignment_tag
def get_links():
    return (('Events', '/'),) + tuple((p.title, '/'+p.url) for p in Page.objects.all())

{# in template #}
{% get_links as links %}

You could port it to your Django version if you like it.

Furthermore, if your page URLs is like /?page=2, you could try django-pagination http://pypi.python.org/pypi/django-pagination

Upvotes: 4

Timmy O&#39;Mahony
Timmy O&#39;Mahony

Reputation: 53999

From the docs:

Many template tags take a number of arguments -- strings or template variables -- and return a page_navigation after doing some processing

simple tags are for printing a piece of information, not assigning some result to a variable (a list in your case)

So, you would be better of using an inclusion tag:

@register.inclusion_tag('links.html')
def page_navigation(a, b, *args, **kwargs):
    pages = Page.objects.all()
    links = [['Events','/']]
    for page in pages:
        links.append([page.title, '/'+page.url])
    return {'links':links}

and creating a links.html file in your template directory:

<ul>
{% if links %}
    {% for link in links %}
    <a href="{{link.1}}"><li>{{link.0}}</li></a>
    {% endfor %}
{% else %}
    <li>no pages found</li>
{% endif%}
</ul>

and in your original template you can include this:

{% load pages_navigation %}
{% page_navigation %}

which will call the template tag, render it and insert it into your template

Upvotes: 5

Related Questions