Skip to main content
edited body
Source Link

For exactly the same reason that most imperative programming languages that do iteration with for loops accept an empty collection to iterate over instead of throwing an error. It’s a very common case to need to account for, and it also is much easier to read if you don’t need the check for an empty collection.

Similarly, the general construct you show in your examples of doing something different if a collection of items is empty is a very common need when doing presentational templating (which accounts for a vast majority of template language usage), and it’s much easier to read a for/else syntax than it is to read an if/for/else construct.

Using Jinja as a point of comparison, here0shere’s what that example would look like without for/else syntax:

{% if users|length > 0 %} {% for user in users %} {{ user }} {% endfor %} {% else %} This is only evaluated if `users` is empty {% endif %} 

It’s not horrible, but there’s a lot more template language in there than in the for/else case, which is less than ideal especially given that this type of thing is likely to show up very frequently if you’re rendering things that users will be seeing.

For exactly the same reason that most imperative programming languages that do iteration with for loops accept an empty collection to iterate over instead of throwing an error. It’s a very common case to need to account for, and it also is much easier to read if you don’t need the check for an empty collection.

Similarly, the general construct you show in your examples of doing something different if a collection of items is empty is a very common need when doing presentational templating (which accounts for a vast majority of template language usage), and it’s much easier to read a for/else syntax than it is to read an if/for/else construct.

Using Jinja as a point of comparison, here0s what that example would look like without for/else syntax:

{% if users|length > 0 %} {% for user in users %} {{ user }} {% endfor %} {% else %} This is only evaluated if `users` is empty {% endif %} 

It’s not horrible, but there’s a lot more template language in there than in the for/else case, which is less than ideal especially given that this type of thing is likely to show up very frequently if you’re rendering things that users will be seeing.

For exactly the same reason that most imperative programming languages that do iteration with for loops accept an empty collection to iterate over instead of throwing an error. It’s a very common case to need to account for, and it also is much easier to read if you don’t need the check for an empty collection.

Similarly, the general construct you show in your examples of doing something different if a collection of items is empty is a very common need when doing presentational templating (which accounts for a vast majority of template language usage), and it’s much easier to read a for/else syntax than it is to read an if/for/else construct.

Using Jinja as a point of comparison, here’s what that example would look like without for/else syntax:

{% if users|length > 0 %} {% for user in users %} {{ user }} {% endfor %} {% else %} This is only evaluated if `users` is empty {% endif %} 

It’s not horrible, but there’s a lot more template language in there than in the for/else case, which is less than ideal especially given that this type of thing is likely to show up very frequently if you’re rendering things that users will be seeing.

Source Link

For exactly the same reason that most imperative programming languages that do iteration with for loops accept an empty collection to iterate over instead of throwing an error. It’s a very common case to need to account for, and it also is much easier to read if you don’t need the check for an empty collection.

Similarly, the general construct you show in your examples of doing something different if a collection of items is empty is a very common need when doing presentational templating (which accounts for a vast majority of template language usage), and it’s much easier to read a for/else syntax than it is to read an if/for/else construct.

Using Jinja as a point of comparison, here0s what that example would look like without for/else syntax:

{% if users|length > 0 %} {% for user in users %} {{ user }} {% endfor %} {% else %} This is only evaluated if `users` is empty {% endif %} 

It’s not horrible, but there’s a lot more template language in there than in the for/else case, which is less than ideal especially given that this type of thing is likely to show up very frequently if you’re rendering things that users will be seeing.