Jinja has a “center” option, but what about the “right alignment”?

Say I have

{% for key,value in adict %} {{key}}:{{value}} {% endfor %} 

How to ensure that all keys are filled so that the output

  something: 1 someotherthing: 3 thelastthing: 2 

edit: This is not the webpage I'm working on, I just get the string output for printing. A.

+6
source share
2 answers

{{ key.rjust(20) }}:{{value}} did the trick

I did not know that you can just call python string commands from a window. If someone has a more "jinja" solution using pipes, I will give an answer to this.

+11
source

Use the built-in Jinja2 filter called format . For instance:

Left-aligned line of width 20:

 {{ "%-20s"|format(variable) }} 

Right aligned row of width 20:

 {{ "%20s"|format(variable) }} 

Your case:

 {{ "%20s:%s"|format(key, value) }} 
+2
source

Source: https://habr.com/ru/post/948474/


All Articles