Git Product home page Git Product logo

Comments (5)

mitsuhiko avatar mitsuhiko commented on May 22, 2024 8

That already exists. Use {% else %} on a for loop. Unfortunately we had else before Django created empty and renaming it now to match Django semantics is kinda weird.

from jinja.

priestc avatar priestc commented on May 22, 2024

oh, i see. Maybe put a note of that in the "Switching from other Template Engines" under then django section?

from jinja.

inactivist avatar inactivist commented on May 22, 2024

I don't see this in the switching documentation. It is mentioned in the control structures:for documentation but it would be helpful to have it in the switching doc...

from jinja.

WloHu avatar WloHu commented on May 22, 2024

If list in code is a generator and for and if blocks work in the same way as in Python then the if-for construct doesn't solve the problem because generators are always truthy, plus such construct is less idiomatic than empty block.

I don't know if you can add a flag to check if loop has run, check for NameError: name 'item' is not defined or add helper variable to list(generator) at Jinja level. If not, then the only solutions is to add the empty block or convince Python devs that empty generators should be falsy (to avoid ugly checking by handling StopIteration and "pushing back" popped item).

from jinja.

mitsuhiko avatar mitsuhiko commented on May 22, 2024

The else block in Jinja works like the empty block in Django already. No change is necessary here.

from jinja.

Related Issues (20)

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.