AnQiCMS template system provides rich 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 wonder, what characters will the system use to fill in the blank areas when the text length is insufficient to meet the specified width?

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

centerFilter: Implement text center alignment

centerThe function of the filter is to display a string centered within a specified total length.If the length of the original string is less than the total length we set, it will automatically supplement characters on both sides of the string to achieve a centered effect.

Default padding character:ForcenterFilter, when the string length is insufficient, it defaults to usingHalf-width spaceFill in the blanks on both sides. If the total number of spaces to be filled is odd, the right side will usually have one less space than the left to maintain visual centering balance.

Example Usage:

Suppose we want to center the string"test"in an area 20 characters wide:

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

Output result:

'        test        '

It can be clearly seen from the result that the string"test"Placed in the middle, both sides are filled with half-width spaces, making the total length reach 20 characters.

ljustFilter: Implement text left alignment

ljustFilter used to left-align strings 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 supplement characters on the right side.

Default padding character:andcenterFilter, the sameljustThe filter will also use the default value when it needs to be filledHalf-width spaceto fill the blank space on the right of the string.

Example Usage:

Align the string"test"left-aligned within a width of 20 characters:

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

Output result:

'test                '

As you can see,"test"String is tightly spaced to the left, with enough half-width spaces filled on the right to reach a total length of 20.

rjustFilter: Implement text right alignment

rjustThe filter is used to right-align strings 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 defaultHalf-width spaceto fill the spaces on the left side of the string.

Example Usage:

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

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

Output result:

'                test'

This time,"test"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 in the AnQiCMS template,center/ljustandrjustThese three text alignment filters, when performing character padding, use the default and unified padding character.Half-width spaceThis makes text alignment in templates simple and intuitive, achieving a neat layout effect without any additional configuration.


Common Questions and Answers (FAQ)

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

A1:According to the design of AnQiCMS template filter,center/ljust/rjustthese filters are used for text alignment,using half-width spaces by default and onlyAs a fill character.Currently, there is no direct parameter provided in the template syntax to specify or modify the fill character.replaceFilter replacement of spaces (or custom filters) can be achieved, 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 are forcenter/ljustorrjustThe length parameter provided by the filter 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 the length is insufficient, rather than for truncating strings.

Q3: Do these filters have different ways of calculating length when dealing with Chinese and English characters?

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