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

Reducing number of newlines in rendered templates #856

Closed
reubenwong97 opened this issue Aug 27, 2023 · 1 comment
Closed

Reducing number of newlines in rendered templates #856

reubenwong97 opened this issue Aug 27, 2023 · 1 comment

Comments

@reubenwong97
Copy link

reubenwong97 commented Aug 27, 2023

Hi,

I have read through the askama book but couldn't figure out how I could reduce the number of newlines that get rendered in the output. I have a template:

{% for score in scores %}
{% if loop.index == 1 %}
🥇 {{ loop.index }}. {{ score }}
{% else if loop.index == 2 %}
🥈 {{ loop.index }}. {{ score }}
{% else if loop.index == 3 %}
🥉 {{ loop.index }}. {{ score }}
{% else if loop.last %}
🤡 {{ loop.index }}. {{ score }}
{% else %}
  {{ loop.index }}. {{ score }}
{% endif %}
{% endfor %}

That gets rendered as:

"\n\n🥇 1. a 5 20\n\n\n\n🥈 2. b 2 10\n\n\n\n🥉 3. c 1 1\n\n\n\n  4. d 1 0.2\n\n\n\n
 🤡 5. r 2 0.1\n\n"

Is there a way I can make it such that it renders as:

"\🥇 1. a 5 20\n🥈 2. b 2 10\n🥉 3. c 1 1\n  4. d 1 0.2\n
 🤡 5. e 2 0.1\n"

Thanks!

@reubenwong97
Copy link
Author

I was dumb, jinja2 docs made it slightly clearer:

{% for score in scores -%}
{% if loop.index == 1 -%}
🥇 {{ loop.index }}. {{ score }}
{% else if loop.index == 2 -%}
🥈 {{ loop.index }}. {{ score }}
{% else if loop.index == 3 -%}
🥉 {{ loop.index }}. {{ score }}
{% else if loop.last -%}
🤡 {{ loop.index }}. {{ score }}
{% else -%}
  {{ loop.index }}. {{ score }}
{% endif -%}
{% endfor -%}

Fixes the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant