4

My Custom tag:

# app/templatetags/ctags.py

def getgenre():
    genre = ["Test1", "Test2"]
    return genre

register.simple_tag(getgenre)

My html:

# app/templates/base.html

{% load ctags %}
<!-- {% getgenre %} -->
{% for genre in getgenre %}
    <li>{{genre}}</li>
{% endfor %}

This renders a blank page for me. If I uncomment {% getgenre %}, django renders ["Test1", "Test2"] as expected. I've tried countless variations of setting up my tag (including the non-simple_tag way) to no avail. I am simply unable to iterate over any value returned by one of my custom-tags.

Am I missing something fundamental here?

MoorzTech
  • 380
  • 4
  • 17
  • Might be related to: http://stackoverflow.com/questions/9860717/problems-with-simple-tag-array-values-in-django-template – MoorzTech Jan 16 '15 at 02:51

3 Answers3

8

You shoud use assignment_tag instead of simple_tag:

@register.assignment_tag
def getgenre():
    genre = ["Test1", "Test2"]
    return genre

And then in the template:

{% load ctags %}
{% getgenre as genre_list %}
{% for genre in genre_list %}
    <li>{{genre}}</li>
{% endfor %}
catavaran
  • 44,703
  • 8
  • 98
  • 85
  • 1
    Works! Awesome, thanks...should've read the documentation to the end -.- Though I do think that they could've just mentioned the limitations of simple_tag...didn't find anything about that in the simple_tag section. – MoorzTech Jan 16 '15 at 12:50
  • Thank you! I was trying to use simple_tags too because the docs say that assignment_tags are deprecated since 1.9, but I couldn't get those to work, while your solution does! – Sebastián Vansteenkiste Aug 01 '18 at 17:54
3

Custom tags write in the base.py in any app

from django import template
register = template.Library()
@register.inclusion_tag('templte/genre.html')
def getgenre():
    genre = ["Test1", "Test2"]
    return genre

and then the template genre.html

{% load ctags %}
{% for genre in genre_list %}
    <li>{{genre}}</li>
{% endfor %}
django-renjith
  • 3,280
  • 2
  • 13
  • 16
  • I'm afraid this doesn't work for me. Neither does changing `genre_list` to `getgenre` or using `{% getgenre as genre_list %}`. – MoorzTech Jan 16 '15 at 12:59
  • 1
    {% getgenre as genre_list %} – django-renjith Jan 16 '15 at 13:04
  • my way to code custom template tag in django, i create a folder inside the any apps , templatetags folder then create a base.py(it is common for all the custom tags, ) , here i code all tags then , i create a folder tag inside the templates , there i create a html page and coding after that , i just load base in base.html[ main template in template folder ] after that , i incude the template tags function name inside the template, i hope you understood – django-renjith Jan 17 '15 at 03:47
1

As @register.assignment_tag has been removed after Django >= 1.9. And like said in the link "The simple_tag() helper has gained this same ability". True but doesn't work with for loops at least in my case.

Instad of register.simple_tag use register.filter decorator (reference).

@register.filter
def getgenre(arg=None):
    genre = ["Test1", "Test2"]
    return genre

In template (.html):

{% load ctags %}
{% for genre in ""|genre_list %}
    <li>{{genre}}</li>
{% endfor %}
Saad
  • 3,340
  • 2
  • 10
  • 32