Display the string centered, left-aligned, or right-aligned according to a specified length

How to display a string centered, left-aligned, or right-aligned by a specified length in AnQi CMS template?

centerThe filter can display the string centered according to the specified length.If the length of the string is greater than the specified length, it will be displayed according to the actual length of the string. If the length of the string is less than the specified length, an equal number of spaces will be added on both sides of the string to achieve centering.If the number of spaces to be added is odd, the spaces on the right will be fewer than on the left.

ljustThe filter can display strings to the left according to a specified length format.If the length of the string is greater than the specified length, it will be displayed according to the actual length of the string. If the length of the string is less than the specified length, spaces will be added to the right of the string...

rjustThe filter can display strings right-aligned according to a specified length.If the length of the string is greater than the specified length, it will be displayed according to the actual length of the string. If the length of the string is less than the specified length, spaces will be added to the left of the string.

Usage method

centerHow to use the filter:

{{ obj|center:number }}

ljustHow to use the filter:

{{ obj|ljust:number }}

rjustHow to use the filter:

{{ obj|rjust:number }}

For example, to converttestIf you want to display a string centered with 20 characters in length, you can write it like this:

'{{ "test"|center:20 }}'
# 显示结果
'        test        '

Example Demonstration

centerFilter

'{{ "test"|center:3 }}'
'{{ "test"|center:19 }}'
'{{ "test"|center:20 }}'
{{ "test"|center:20|length }}
'{{ "test2"|center:19 }}'
'{{ "test2"|center:20 }}'
{{ "test2"|center:20|length }}
'{{ "你好世界"|center:20 }}'
# 显示结果
'test'
'        test       '
'        test        '
20
'       test2       '
'        test2       '
20
'        你好世界        '

ljustFilter

'{{ "test"|ljust:"2" }}'
'{{ "test"|ljust:"20" }}'
{{ "test"|ljust:"20"|length }}
'{{ "你好世界"|ljust:10 }}'
# 显示结果
'test'
'test                '
20
'你好世界      '

rjustFilter

'{{ "test"|rjust:"2" }}'
'{{ "test"|rjust:"20" }}'
{{ "test"|rjust:"20"|length }}
'{{ "你好世界"|rjust:10 }}'
# 显示结果
'test'
'                test'
20
'      你好世界'