Hacker News new | ask | show | jobs
by adambard 4573 days ago
Edit: The below is actually wrong

> For years I’ve wanted to be able to write my own control flow structures, such as an each...else that runs an else block if the each is empty (Handlebars templates have this).

Actually, python does have a (little-known) `for...else` structure that does what you want.

http://psung.blogspot.ca/2007/12/for-else-in-python.html

1 comments

python's for/else doesn't do what the grandparent post asked for -- that post asked for the else clause to execute if the container that was being iterated over is empty, and in python's for/else (as your link notes) the else clause executes whenever the for clause terminates normally (that is, other than by a break statement). These are very different constructs.
So it does. I wonder what language I was thinking of then.
This is a long shot, but could you be thinking of the Django template language?

https://docs.djangoproject.com/en/dev/ref/templates/builtins...

    <ul>
    {% for athlete in athlete_list %}
        <li>{{ athlete.name }}</li>
    {% empty %}
        <li>Sorry, no athletes in this list.</li>
    {% endfor %}
    </ul>