Skip to main content

UNKNOWN

Project description

django-multiforloop provides an enhancement to django's builtin forloop
template tag, which makes it easier to iterate over multiple lists
simultaneously (acting similarly to Python's `zip`).

multiforloop allows this Python idiom to be used in django templates:

for x,y in zip(x_list, y_list):
print x,y

Normally, to iterate over multiple lists simultaneously in django templates,
the lists must be zipped in the view and passed in as an extra context
variable. When this is the only extra processing needed in a view (eg. with
generic views), this can result in a fair bit of unneeded boilerplate code.

##Usage

Rendering this template

{% load multifor %}
{% for x in x_list; y in y_list %}
{{ x }}:{{ y }}
{% endfor %}

with this context

context = {
"x_list": ('one', 1, 'carrot'),
"y_list": ('two', 2, 'orange')
}

will output

one:two
1:2
carrot:orange

The multifor tag library also includes a `for_longest` tag that functions
similarly to izip_longest from Python's itertools library. Whereas the
normal for loop will truncate all inputs to the length of the shortest,
for_longest will iterate over all values of the longest, filling any shorter
inputs with the value in settings.TEMPLATE_STRING_IF_INVALID ('' by default).

Observe the difference:


Rendering this template

{% load multifor %}
{% for x in x_list; y in y_list %}
{{ x }}:{{ y }}
{% endfor %}

with this context

context = {
"x_list": ('one', 1, 'carrot'),
"y_list": ('two', 2)
}

will output

one:two
1:2

While rendering this template

{% load multifor %}
{% for_longest x in x_list; y in y_list %}
{{ x }}:{{ y }}
{% endfor %}

with the same context

context = {
"x_list": ('one', 1, 'carrot'),
"y_list": ('two', 2)
}

will output

one:two
1:2
carrot:

## Installation

1. pip install django-multiforloop
2. Include 'multiforloop' in your settings.py's list of installed apps
3. Add `{% load multifor %}` to the top of any templates which use the multiforloop

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-multiforloop-0.2.1.tar.gz (2.1 kB view details)

Uploaded Source

Built Distribution

django-multiforloop-0.2.1.linux-i686.exe (66.4 kB view details)

Uploaded Source

File details

Details for the file django-multiforloop-0.2.1.tar.gz.

File metadata

File hashes

Hashes for django-multiforloop-0.2.1.tar.gz
Algorithm Hash digest
SHA256 4a4c1832fbb2769e1b07e8c03c6cdcbfb2d361e22f15363007d311a914e7dd6b
MD5 58beef37ed2ff9f90734eb0c5d8958a2
BLAKE2b-256 d0a384965eb80d65ddc1b08aae655caa509edf951f55fe14e4054b779c054c85

See more details on using hashes here.

File details

Details for the file django-multiforloop-0.2.1.linux-i686.exe.

File metadata

File hashes

Hashes for django-multiforloop-0.2.1.linux-i686.exe
Algorithm Hash digest
SHA256 d7b25b1ce58787c2bead6dad1aed5f63eabb2eeacb009d7090d67e964b29d6fb
MD5 3f0036c6f254a3995489c0f509c2b24c
BLAKE2b-256 aafa57badc1cbe6661e804572fe364067c352b37c113c90b0590d858c4334553

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page