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
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-multiforloop-patched-0.2.0.tar.gz
.
File metadata
- Download URL: django-multiforloop-patched-0.2.0.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1f42cddf2707f91f958e29492a7be279cdf5dcaaff0cbc69ef06dcb424237cac |
|
MD5 | cc0076e7e0e4cbb86e9f7acda0b6afa5 |
|
BLAKE2b-256 | 1d99a2b13b36939f97ad3e0418bf09fd30aaf262619ad929ebc5d28f18858059 |
File details
Details for the file django-multiforloop-patched-0.2.0.macosx-10.9-intel.tar.gz
.
File metadata
- Download URL: django-multiforloop-patched-0.2.0.macosx-10.9-intel.tar.gz
- Upload date:
- Size: 7.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ef2de8989935582999aff682f322ebb8499c426330ec66019624f16bd0fb49d |
|
MD5 | 6f36573226258f3057e3a320c9c3fb2d |
|
BLAKE2b-256 | dfd58547a691a5ab270377fc16471985fb70cd8adc2c4978a562cb180aa0e16c |