Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Django's {% empty %} in for-loops #395

Closed
mxmerz opened this issue Mar 13, 2019 · 3 comments
Closed

Add support for Django's {% empty %} in for-loops #395

mxmerz opened this issue Mar 13, 2019 · 3 comments

Comments

@mxmerz
Copy link

mxmerz commented Mar 13, 2019

I am missing support for an {% empty %} statement in for-loops. I know the {% empty %} statement from Django's templating language:

The for tag can take an optional {% empty %} clause whose text is displayed if the given array is empty or could not be found:

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

Keats commented Mar 27, 2019

I think I would rather wrap the forloop in a if rather than add the empty statement to keep things simple

@mxmerz
Copy link
Author

mxmerz commented Mar 27, 2019

Wrapping the for-loop in an if is, of course, possible – but template authors would have to do this for practically every for-loop.

<ul>
{% if athlete_list is defined and athlete_list|length > 0 %}
	{% for athlete in athlete_list %}
		<li>{{ athlete.name }}</li>
	{% endfor %}
{% else %}
	<li>Sorry, no athletes in this list.</li>
{% endif %}
</ul>

For my taste, this is too much boilerplate code in the template.

@Keats
Copy link
Owner

Keats commented Mar 28, 2019

I'll take a PR for it

Keats pushed a commit that referenced this issue Jul 5, 2019
* Fixed trait objects without an explicit  are deprecated

* Added {% else %} tag to for loops triggered in cases where the container being looped over is empty

* Made for-loops over undefined variables with explicitly defined {% else %} clauses act the same as iteration over empty containers: it renders the contents of the {% else %} block.

* rustfmt

* Cleaned up deep if block

* Code cleanup

* Unified closing statement in render_for_loop
Removed corner case for undefined containers in implementation of for-loop {% else %} block
Removed error test for above corner case
@Keats Keats added done and removed help wanted labels Jul 5, 2019
@Keats Keats closed this as completed Dec 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants