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 fixed layouts. AnQiCMS's powerful template engine provides a variety of filters to help us meet these needs, whereljustAlign LeftrjustAlign Right is a practical utility used to control string alignment and padding. They can make our content look more orderly and unified, enhancing the user 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 length we specified?Is it truncated or displayed in full?ljustandrjustThe specific behavior of the filter.

UnderstandingljustandrjustFilter

Firstly, let's review.ljustandrjustThe basic function.

ljustThe function of the filter is to pad spaces to the right of the string, thus achieving the specified total length.This means the string will be displayed to the left.{{ "AnQi"|ljust:10 }}You will get'AnQi '(Note, there are 6 spaces at the end).

rjustFilter does 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).

They are primarily used to ensure that the output string reaches the minimum length set, to achieve a visual alignment effect.

核心问题:How to handle string when it exceeds the specified length?

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

According to the setting of AnQiCMS template engine,ljustandrjustthe filter will be triggered when the length of the original string exceeds or is equal to the specified padding length,Will not truncate any stringThey will chooseOutput the complete string content as is, without any modification.

This design concept pays great attention to the completeness of content and the accuracy of data.Imagine if the key product name, article title, or any important information were unexpectedly truncated due to alignment requirements, it could lead to inaccurate information, even misleading users.AnQiCMS template engine handles it this way to avoid potential data loss and information miscommunication.It ensures that the content itself is not damaged even if there are limitations in 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 long for left alignment:{{ "AnQiCMS内容管理系统"|ljust:8 }}

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

Actual application scenarios

In actual website operations, this feature allows us to use it with greater peace of mind.ljustandrjustTry to beautify the layout while not worrying about important information being hidden.

For example, on a product list page or article summary card, you may want the title to be visually aligned, but absolutely not want the long title to be truncated. At this time, you can useljustorrjustAlign short names to make them look neat, and 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 needWhen the string exceeds the specified length, it will be truncatedAnQiCMS also providestruncatechars(truncated by character) ortruncatewordsEnglish filters. You can use these truncation filters first, and then applyljustorrjustalignment. This allows you to flexibly control the display of content according to specific needs.

How to use these filters

ljustandrjustThe syntax of the filter usage is very intuitive:

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

Among them:

  • 变量: is the string variable you want to process.
  • 过滤器名称: Can beljustorrjust.
  • 指定长度: is the total character length you want the string to fill.

Let's see 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 demonstrates its respect for content integrity when aligning string pairs.They will not truncate strings when they exceed the specified length, but choose to present the original text in full, which provides great flexibility and protection for website content operators.By using it reasonably, we can easily create a web layout that is both aesthetically pleasing and informative.


Common Questions (FAQ)

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

Answer: You can usetruncatechars(Character truncation) Ortruncatewords(Word truncation) Filter inljustorrjustBefore processing the string. For example, `{{ “long text