Login

Easier tags with parameters as dictionary values

Author:
polarbear
Posted:
December 23, 2007
Language:
Python
Version:
.96
Tags:
tag
Score:
0 (after 0 ratings)

This is just an example, NOT any particular tag. I was just tiered in examining every bits in list. I converted list to dictionary for easier manipulation of parameters. You can use this keeping in mind syntax:

{% tag_name 1_key 1_value 2_key 2_value %} and so on...

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@register.tag
def get_popular(parser, token):
    """
    Get highest rated objects

    Syntax::

        {% get_popular for [pkg].[module_name] as [varname] %}

    Usage::

        {% get_popular for blogs.entry as popular %}
    """
    bits = token.contents.split()
    tag = bits.pop(0)

    length = len(bits)
    keys = bits[0:length:2]
    values = bits[1:length:2]
    args = {}
    for k in keys:
        args[k] = values.pop(0)

    return PopularNode(args["for"], args["as"])

More like this

Comments

Please login first before commenting.