0

I got a problem with iteration on zinnia tag outcome. Let's say that that tag returns a list of some categories, I tried to manage it in few ways:

{% with categories=get_plain_categories %}
    {% for category in categories %}
        <h1>{{ category }}</h1>
    {% endfor %}
{% endwith %}

or simply:

{% for category in get_plain_categories %}
    <h1>{{ category }}</h1>
{% endfor %}

But in both ways, it seems to not even run get_plain_categories tag (I made few prints in it), but when I write : {% get_plain_categories %}, it returns list as it's supposed to.

How should I get that working?

Valentin Flachsel
  • 10,795
  • 10
  • 44
  • 67
mdargacz
  • 1,267
  • 18
  • 32

1 Answers1

0

Unfortunatelly with tag is not that powerful, you can't use it with output of other tags. You'll have to create your own tag.

As an example you can have a look at the static. It lets you insert a path to a static file with {% static "images/hi.jpg" %} but you can't easily save it to a variable for later use. That's why in Django 1.5 it got a new syntax {% static "images/hi.jpg" as myphoto %} and this way you can later use {{ myphoto }}. This can't be achieved with with.

That said, I can't find any mentions of get_plain_categories in Google, which is weird.

kirelagin
  • 13,248
  • 2
  • 42
  • 57
  • the issue is, that i want insert into HTML every single object from returned by zinnia tag list of objects, that must be somehow possible. As far as I'm concerned creating a tag which will return ready html code, isn't so "clean programming" style – mdargacz Jun 08 '13 at 11:50
  • @user2466146 that's why I've told you how it was done in `static`. You could create tag that will assign this list to a variable and then just use the code you've shown in the question. The only wrong thing in your code is that you can't use `with` this way. But you should create a tag that will bind this list to a name and use it like `{% my_get_plain_categories as categories %}`. – kirelagin Jun 08 '13 at 12:23
  • {% my_get_plain_categories as categories %} returns: 'my_get_plain_categories' received too many positional arguments – mdargacz Jun 08 '13 at 16:47