Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jinja2 Padding and Aligning Strings

I would like to achieve something similar to this (https://pyformat.info/#string_pad_align) in Jinja2.

In python if I want a string to always be a certain length I would do something like this:

'{:>10}'.format('test')

How can I do this in Jinja2?

like image 753
APorter1031 Avatar asked Aug 15 '17 17:08

APorter1031


2 Answers

From jinja2 documentation

In most cases it should be more convenient and efficient to use the % operator or str.format().

So you can always write

{{ "{:>10}".format("test") }}

so as to keep the modern format string syntax, which is usually feature-richer than the printf-style formatting from the other answer.

like image 183
N1ngu Avatar answered Nov 15 '22 17:11

N1ngu


I found a solution! You can use the builtin filter for Jinja like so

{{ "%-10s" | format("test",) }}
like image 34
APorter1031 Avatar answered Nov 15 '22 15:11

APorter1031