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 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
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