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.0.tar.gz (2.1 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Source

File details

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

File metadata

File hashes

Hashes for django-multiforloop-0.2.0.tar.gz
Algorithm Hash digest
SHA256 1ad792e1681d03e623b5fbf12042c88be91f245c19b05eb4c7deb8d1f5fdf78a
MD5 70a5d6a59ee8fcb9094dd3b6c104c486
BLAKE2b-256 f82f922bedbd6795c20c60298818cb19358e57d37994bb71984cde6ca78ae916

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django-multiforloop-0.2.0.linux-i686.exe
Algorithm Hash digest
SHA256 f08cac08f0a7451fc8261349ac19cd0fe5743d81dcd5b2d057626348ba7cfdcb
MD5 a3bae7d9f5a51b315b823271a920ed01
BLAKE2b-256 0ef4039062e1a5379eaca4cfd97e42ed123948241acaba136a5899855bb01e67

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