Login

Easier tags with parameters as dictionary values

Author:
polarbear
Posted:
December 23, 2007
Language:
Python
Version:
.96
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

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.