How to display a string centered, left-aligned, or right-aligned with a specified length in the Anqi CMS template?
center
Filters can center strings in specified length format. If the string length is greater than the specified length, it will be displayed according to the actual length of the string. If the string length is less than the specified length, the same number of spaces will be added to both sides of the string to achieve centering. If the number of spaces that need to be supplemented is singular, the spaces on the right will be assigned one less than the left.
ljust
The filter can display the string to the left in the specified length format. If the string length is greater than the specified length, it is displayed according to the actual length of the string. If the string length is less than the specified length, spaces will be added to the right of the string. .
rjust
The filter can display the string to the right in the specified length format. If the string length is greater than the specified length, it is displayed according to the actual length of the string. If the string length is less than the specified length, spaces will be added to the left of the string.
How to use
center
How to use filters:
{{ obj|center:number }}
ljust
How to use filters:
{{ obj|ljust:number }}
rjust
How to use filters:
{{ obj|rjust:number }}
For exampletest
To display a string centered with 20 characters, you can write it like this:
'{{ "test"|center:20 }}'
# 显示结果
' test '
Sample Demo
center
Filter
'{{ "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
' 你好世界 '
ljust
Filter
'{{ "test"|ljust:"2" }}'
'{{ "test"|ljust:"20" }}'
{{ "test"|ljust:"20"|length }}
'{{ "你好世界"|ljust:10 }}'
# 显示结果
'test'
'test '
20
'你好世界 '
rjust
Filter
'{{ "test"|rjust:"2" }}'
'{{ "test"|rjust:"20" }}'
{{ "test"|rjust:"20"|length }}
'{{ "你好世界"|rjust:10 }}'
# 显示结果
'test'
' test'
20
' 你好世界'