The AnQiCMS template system provides a rich set of filters (filters) to help us format and process content. When performing text alignment operations,center/ljustandrjustThese filters are commonly used tools. They can center, left-align, or right-align our text content within a fixed width.When first encountering these filters, many users may be curious, what characters will the system use to fill the blank areas when the text length is shorter than the specified width?

Below, we will discuss the functions of these three filters and their default padding characters one by one.

centerFilter: Align text to center

centerThe filter's role is to center a string within a specified total length.If the length of the original string is less than the total length we set, it will automatically fill characters on both sides of the string to achieve centering.

Default padding character: ForcenterFor a filter, when the string length is insufficient, it will use by defaulthalf-width spaceFill in the blanks on both sides. If the total number of spaces to be filled is odd, the right side is usually one less than the left to maintain visual centering balance.

Usage example:

Suppose we want to center the string"test"within a 20-character wide area:

'{{ "test"|center:20 }}'

Output Result:

'        test        '

It is clearly visible from the results that the string"test"It is placed in the middle, with half-width spaces on both sides, making the total length reach 20 characters.

ljustFilter: Implement text left alignment

ljustThe filter is used to left-align a string within a specified total length.This means that if the length of the original string is less than the set total length, it will automatically fill the characters on the right side of the string.

Default padding character: andcenterFilter is the same,ljustThe filter will also be used by default when it needs to be filledhalf-width spaceto fill the right side of the string.

Usage example:

To string"test"Left-aligned within a width of 20 characters:

'{{ "test"|ljust:"20" }}'

Output Result:

'test                '

As you can see,"test"The string is close to the left, and enough half-width spaces are filled on the right to reach a total length of 20.

rjustFilter: Implement text right alignment

rjustThe filter is used to right-align a string within a specified total length. If the length of the original string is less than the set total length, it will automatically fill characters on the left side.

Default padding character: Similarly,rjustThe filter also uses the default fillinghalf-width spaceto fill the spaces on the left side of the string.

Usage example:

To string"test"Right-aligned within a width of 20 characters:

'{{ "test"|rjust:"20" }}'

Output Result:

'                test'

This time,"test"The string is right-aligned, the left side is filled with half-width spaces, ensuring the entire output length is 20.

Summary

Through the above introduction, we can clearly understand that the AnQiCMS template includescenter/ljustandrjustThese text alignment filters, when performing character padding, use the default and unified padding characters arehalf-width spaceThis makes text alignment in templates simple and intuitive, achieving neat formatting without additional configuration.


Frequently Asked Questions (FAQ)

Q1:center/ljust/rjustDoes the filter support custom padding characters?

A1:According to the design of the AnQiCMS template filter,center/ljust/rjustthese filters align text by default and only use half-width spaces,default and only use half-width spacesAs a fill character. Currently, the template syntax does not provide a direct parameter to specify or modify the fill character.If you need to use other characters to fill, you may need to combine other string processing methods (such as first generating a string with spaces, then usingreplaceFilter to replace spaces) or create a custom filter to do so, but this is not within the scope of these native filters.

Q2: If the specified length parameter is less than or equal to the actual length of the original string, how will these filters handle?

A2:When we arecenter/ljustorrjustThe filter provided length parameter is less than or equal to the actual length of the original string, the filter will not truncate the string or add any padding.It will directly return the original string itself. This means that these filters are mainly used for extending and aligning strings when their length is insufficient, rather than for truncating them.

Q3: Do these filters have a difference in the way they calculate the length of Chinese and English characters?

A3:AnQiCMS's template system calculates the length of strings uniformly for Chinese characters and English characters.Whether it is Chinese or English, each character is counted as a unit. For example,"你好世界"and"test"The length is inlengthThe filters will be calculated as 4. Therefore, when usingcenter/ljust/rjustWhen a filter specifies a length, it calculates the number of spaces needed to fill to reach the target width based on a unified character count. For example,"你好世界"|center:20It will still be calculated according to the length of 4 characters and filled with 16 spaces.