Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jinja - how to force truncate when value exceeds specified field width

I have started with the following ninja2 template, but If the value of the 2nd column is > the specified width, it simply prints the entire string and the rest of the columns shift right.

{{ "%-7s"|format(tbl[3]) }}      {{ "%-12s"|format(tbl[5]) }}    {{ "%14s"|format(tbl[6]) }}

The problem I want to solve is how to truncate the second column when (and only when) the tbl[5] value exceeds the column width.

Any ideas. I've googled, but can't find an answer.

like image 809
Thom Rogers Avatar asked Oct 29 '25 19:10

Thom Rogers


1 Answers

Have you tried the truncate function:

{{ "foo bar baz qux"|truncate(9,true,'') }}

Like first parameter is the length the second tells you if you want to truncate exactly at length(if true) or truncate from a complete word. The third is the ellipsis, in this case a I removed the default ... and I put a blank instead.

Let me know if this helps.

like image 194
rick Avatar answered Nov 02 '25 14:11

rick