Say I have
{% for key,value in adict %}
{{key}}:{{value}}
{% endfor %}
How do I ensure that all of the keys are padded such that the output is
something: 1
someotherthing: 3
thelastthing: 2
edit: This is not a webpage I am working on, I am just getting string output for printing.
It is a text-based template language and thus can be used to generate any markup as well as source code. The Jinja template engine allows customization of tags, filters, tests, and globals. Also, unlike the Django template engine, Jinja allows the template designer to call functions with arguments on objects.
Jinja is one of the most used template engines for Python. This project is a JavaScript implementation with emphasis on simplicity and performance, compiling templates into readable JavaScript that minifies well.
Context contains the dynamic content that you want to inject in your template when it is being rendered.
Jinja, also commonly referred to as "Jinja2" to specify the newest release version, is a Python template engine used to create HTML, XML or other markup formats that are returned to the user via an HTTP response.
{{ key.rjust(20) }}:{{value}}
did the trick
I did not know you could just call python string commands from the box. If someone has a more "jinja" solution, using pipes, I will give the answer to that.
Use the build-in Jinja2 filter called format. For example:
Left aligned string of width 20:
{{ "%-20s"|format(variable) }}
Right aligned string of width 20:
{{ "%20s"|format(variable) }}
Your case:
{{ "%20s:%s"|format(key, value) }}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With