When building a website, we often need to finely format and align the text content on the page, especially when displaying lists, tables, or other areas that require a fixed layout. The powerful template engine of AnQiCMS provides various filters to help us meet these needs, among whichljustLeft aligned andrjustIt is a practical tool used to control string alignment and padding. They can make our content look more orderly and improve the user's reading experience.

However, a common issue when using these filters is: how will they handle it when the length of the original string exceeds the specified length?Is it truncated or displayed in full? Today, let's delve into the AnQiCMS ofljustandrjustThe specific behavior of the filter.

UnderstandingljustandrjustFilter

First, let's take a look backljustandrjustThe basic function.

ljustThe function of the filter is to fill the string with spaces on the right to reach the specified total length.This means the string will be displayed aligned to the left. For example, if you have a short string "AnQi" and you want it to occupy a width of 10 characters, use{{ "AnQi"|ljust:10 }}You will get'AnQi '(Note, there are 6 spaces at the end).

rjustThe filter works exactly the opposite, it fills spaces on the left side of the string to make it right-aligned. If you use “AnQi”{{ "AnQi"|rjust:10 }}, you will get' AnQi'(with 6 spaces at the beginning).

The core function is to ensure that the output string is at least the minimum length set, in order to achieve a visual alignment effect.

The core question: how to handle strings when they exceed the specified length?

Now, we return to the core issue of the article: when the length of the original string exceeds or is equal to the specified padding length,ljustandrjusthow will the filter handle it?

According to the setting of the AnQiCMS template engine,ljustandrjustthe filter will process when the length of the original string exceeds or is equal to the specified padding length,It will not truncate the stringThey will chooseIt will output the complete string content unchanged, without any modification.

This design concept pays great attention to the completeness of content and the accuracy of data.Imagine if a key product name, article title, or any important information were accidentally truncated due to alignment requirements, it could lead to inaccurate information or even mislead users.This processing method of AnQiCMS template engine is to avoid potential data loss and information miscommunication.It ensures that the content itself is not damaged even if there are restrictions on the layout.

For example, if you have a string “AnQiCMS content management system”, its length is 10 characters (Chinese characters are usually counted as 1 character in length), but you specify it as 8 characters in length for left alignment:{{ "AnQiCMS内容管理系统"|ljust:8 }}

At this moment, the output will be the complete original string:'AnQiCMS内容管理系统'Instead of being truncated to “AnQiCMS…“. Similarly,rjustThe filter will also handle it in the same way, displaying the original string in full.

Application scenarios in practice

In the actual operation of the website, this feature allows us to use it with more confidence.ljustandrjustTry to beautify the layout while not worrying about important information being hidden.

For example, on a product list page or an article summary card, you may want the title to be visually aligned, but not to be truncated for long titles. In this case, you can useljustorrjustAlign short names so that they look neat, and those titles that exceed the set value will be displayed in full, slightly breaking the alignment but ensuring the completeness of the information.

If you indeed need toTruncate the string when it exceeds the specified lengthAnQiCMS also providestruncatecharsCharacter truncate ortruncatewordsetc., truncation filters by word. You can use these truncation filters first, and then apply themljustorrjustAligning is performed. This allows flexible control of content display according to specific requirements.

How to use these filters

ljustandrjustThe syntax of using filters is very intuitive:

{{ 变量 | 过滤器名称: 指定长度 }}

Among them:

  • 变量This is the string variable you want to process.
  • 过滤器名称It can be.ljustorrjust.
  • 指定长度This is the total character length you want the string to fill.

Let's look at some code examples:

{# 字符串短于指定长度:会进行填充 #}
<p>左对齐(短字符串):'{{ "产品名称"|ljust:10 }}'</p> {# 输出: '产品名称  ' #}
<p>右对齐(短字符串):'{{ "AnQi"|rjust:10 }}'</p>   {# 输出: '      AnQi' #}

{# 字符串长于或等于指定长度:保持原样,不截断 #}
<p>左对齐(长字符串):'{{ "AnQiCMS内容管理系统"|ljust:8 }}'</p> {# 输出: 'AnQiCMS内容管理系统' #}
<p>右对齐(长字符串):'{{ "AnQiCMS内容管理系统"|rjust:8 }}'</p> {# 输出: 'AnQiCMS内容管理系统' #}

{# 如果确实需要先截断再对齐 #}
{% set long_string = "AnQiCMS是一款高性能的企业级内容管理系统" %}
<p>先截断后左对齐:'{{ long_string|truncatechars:10|ljust:15 }}'</p> {# 输出: 'AnQiCMS是一...     ' #}

In summary, AnQiCMS'sljustandrjustThe filter demonstrated its respect for the integrity of the content while aligning string pairs.They do not truncate strings when they exceed the specified length, but choose to present the original text in full, which provides great flexibility and security for website content operators.By using it reasonably, we can easily create a webpage layout that is both beautiful and informative.


Frequently Asked Questions (FAQ)

1. Ask: If I indeed want to truncate a string when it exceeds a specified length, what should I do?

Answer: You can usetruncatechars(Character truncation) ortruncatewords(Word truncation) filter is inljustorrjustBefore processing the string. For example, `{{ "long text"}}