有没有办法指定Jinja模板中每个字段的宽度?
例如,我希望我的输出显示如下:
Name Roll Address Subject1 Subject2
Run Code Online (Sandbox Code Playgroud)
我希望每个字段都具有与之关联的宽度。每个字段都有不同的宽度
我找到了一种借助Python格式化来做到这一点的方法
template = "{:20} {:2} {:30} {:10} {:10}"
fields = template.format("Name", "Roll", "Address","Subject1", "Subject1")
print(fields)
Run Code Online (Sandbox Code Playgroud)
但是,我需要在Jinja2中执行此操作的方法,因为我有很多字段,其中一些字段需要条件格式。有没有一种方法而不必对它们之间的空格进行硬编码?
{{Name}} {{Roll}} {{Address}} {{Subject1}} {{Subject2}}
Run Code Online (Sandbox Code Playgroud)
有一个用于百分比格式的过滤器format:
{{'%-20s'|format(Name)}} {{'%-2s'|format(Roll)}} {{'%-30s'|format(Address)}} {{'%-10s'|format(Subject1)}} {{'%-10s'|format(Subject2)}}
Run Code Online (Sandbox Code Playgroud)
要么
{{'%-20s %-2s %-30s %-10s %-10s'|format(Name, Roll, Address, Sbject1, Subject2)}}
Run Code Online (Sandbox Code Playgroud)
%-<num>s是右填充格式说明一样{:<num>}。
| 归档时间: |
|
| 查看次数: |
1589 次 |
| 最近记录: |