Git Product home page Git Product logo

django-multiforloop's Introduction

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

django-multiforloop's People

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.