During the template creation process in AnQi CMS, we often encounter scenarios where we need to format and align text content.Whether it is the list display of product names and prices, or the layout of article titles and dates, a uniform layout can significantly enhance the professionalism and user experience of the website.The template engine of AnQi CMS supports syntax similar to Django, providing a series of powerful filters to help us meet these needs.ljustandrjustThey are used to implement left and right alignment of strings.
UnderstandingljustandrjustThe role of the filter
ljustandrjustThe core function of the filter is to process strings.Blank fillingMake it reach the total length specified by us, thus achieving the alignment effect of the text.
ljust(Left Justify) FilterljustBy name, it means 'left-aligned'. It will place the given string to the left and, based on the specified target length, fill in spaces on theright side,until the total length of the string reaches the predefined value.- Usage:
{{ 字符串变量 | ljust:目标长度 }} - Example:
The output result will be:{# 假设商品名称“AnQiCMS”长度为7,我们希望它占据10个字符的空间并左对齐 #} {{ "AnQiCMS"|ljust:10 }}"AnQiCMS "(Followed by 3 spaces after the string "AnQiCMS")
- Usage:
rjust(Right Justify) FilterrjustIt is 'right-aligned', which will place the given string to the right and, according to the specified target length, fill it with spaces on theleftto reach the preset total length.- Usage:
{{ 字符串变量 | rjust:目标长度 }} - Example:
The output result will be:{# 假设商品价格“99.00”长度为6,我们希望它占据8个字符的空间并右对齐 #} {{ "99.00"|rjust:8 }}" 99.00"(The string "99.00" is preceded by 2 spaces)
- Usage:
Key features: Length processing rules
When usingljustandrjustWhen filtering, there is a very important feature to note:
If the original string itself has a length ofexceededthe target length you specified, thenljustandrjustfilter andit will not truncateyour string, but will insteadautoEnglish
- Example:
The output will be:{# 目标长度为5,但原始字符串长度为7 #} {{ "AnQiCMS"|ljust:5 }}"AnQiCMS"English
Actual application scenarios
These filters are widely used in template development, especially in scenarios where structured or tabular data needs to be created:
Table data display:When you need to display multi-column data in plain text or preformatted text and want each column to be neatly aligned,
ljustandrjustCan show off your skills. For example, on an order detail page, you can align the product name and price like this:{% for item in order.items %} <p>商品名称:{{ item.name|ljust:20 }}单价:{{ item.price|rjust:8 }} 数量:{{ item.quantity|rjust:4 }}</p> {% endfor %}This will make each row's "product name
Fixed width layout:In certain specific scenarios, such as generating reports, printing templates, or integrating with certain fixed-width systems, it is very useful to have text blocks strictly adhere to a specific width. In this case, filling spaces for alignment is very helpful.
Visual effects optimization:Alignment can make the page elements look more orderly and professional. For example, when displaying points or balance in the user's personal center, the right alignment of numbers can be unified.
The difference with other related filters
The template engine of AnQi CMS also provides other filters related to string length and alignment, understanding their differences can help us choose them better:
centerFilter:Withljustandrjustis similar,centerUsed to center-align strings. It fills spaces on both sides of the string.truncatecharsandtruncatewordsFilter:These filters are used to truncate overly long strings and add an ellipsis at the end....)。They are mainly used to shorten text, not to fill. Therefore, when the string exceeds the target length,ljustandrjustthe original string will be retained, whiletruncatecharsetc. will be truncated.
Summary
ljustandrjustFilter is a powerful tool for string alignment in Auto CMS templates.Through simple parameter settings, we can make the text content more orderly and visually appealing, thus enhancing the overall aesthetics and user experience of the website.Master their usage and features, allowing your security CMS template to be more finely crafted in detail.
Common Questions (FAQ)
1.ljustandrjustWill the filter truncate a string that exceeds the target length?No. If the length of the original string has already exceeded the target length you specified,ljustandrjustThe filter returns the original string unchanged, without any truncation. They are mainly used to pad spaces on both sides (or one side) of a string to achieve alignment when the string is relatively short.
2. Besides spaces, I can use other characters to fill inljustandrjustFilter?Currently, the Aiqi CMS template inljustandrjustThe filter defaults and only supports usingspacesPerform filling. If you need to use other characters for filling or more complex alignment logic, you may need to combine other string processing filters or perform data preprocessing on the backend.
3. How to achieve the center alignment effect of a string?The AutoCMS template engine providescenterThe filter can be used to achieve the center alignment of a string. Its usage is the same asljustandrjust类似,例如:{{ "居中文字"|center:20 }}.centerThe filter will pad spaces on both sides of the string to center it within the specified length.