AnQiCMS's template engine provides powerful filter functions, which can easily format and process output variables. For the need of string truncation, we mainly usetruncatecharsandtruncatewordsThese two filters.

Truncate string by character length and add an ellipsis.

When we want to limit the display of a piece of text to a fixed number of characters, we can usetruncatecharsFilter.This filter will truncate the string according to the number of characters you specify. If the length of the original string exceeds the specified number, it will automatically add an ellipsis (‘…’) at the end of the truncation, thus clearly indicating that the content has been omitted.

For example, you need to display the introduction of each article on the article list page, but in order to maintain the consistency of the page layout, it is desired that the introduction should not exceed one hundred characters. At this time, you can apply it in the template as followstruncatecharsFilter:

{{ item.Description|truncatechars:100 }}

In this example,item.Descriptionrepresented the summary content of the article. Through|truncatechars:100AnQiCMS will automatically truncate the introduction to a maximum of one hundred characters and intelligently add an ellipsis at the end. If the introduction itself is less than one hundred characters, it will be displayed in full without adding an ellipsis.

Truncate string by word count and add an ellipsis

With different from character truncation, sometimes we are more inclined to control the text length by the number of words, which is especially common in English or other languages that use words as the basic unit, and can better maintain the integrity of semantics.truncatewordsThe filter is designed for this purpose. It calculates the number of words in a string and truncates it according to the number you specify, and also automatically adds an ellipsis at the end.

If you have a product description field and want to display a short description containing twenty words on the product list page, you can use it in the template like thistruncatewords:

{{ product.Description|truncatewords:20 }}

Here are theproduct.DescriptionRepresents the product description.|truncatewords:20Ensure that the description content is no more than twenty words and also add an ellipsis when necessary.

Process the content truncation of content containing HTML tags.

In the operation of actual websites, many content fields (such as article content, and summaries generated by rich text editors) may contain HTML tags. If used directly,truncatecharsortruncatewordsExtracting content from this may disrupt the HTML structure, causing the page to display abnormally. To solve this problem, AnQiCMS also provides a filter optimized for HTML content:truncatechars_htmlandtruncatewords_html.

These filters intelligently parse HTML structures when truncating HTML strings, ensuring that all opened HTML tags are correctly closed while truncating text, thus avoiding page chaos caused by unclosed tags. When outputting this type of truncated content, it is usually necessary to use them in conjunction with other methods to ensure that HTML tags can be rendered normally rather than being displayed as plain text.|safeFilter.

For example, to extract a segment of text containing HTML and limit it to two hundred characters while maintaining the HTML structure:

{{ archive.Content|truncatechars_html:200|safe }}

Similarly, if you want to truncate HTML content by word count:

{{ archive.Content|truncatewords_html:30|safe }}

Through these filters, you can flexibly control the display length of AnQiCMS website content, whether it is plain text or content rich in HTML structure, which can be presented to users in an elegant and structurally complete manner, thereby optimizing the visual effects of the page and the user reading experience.When performing content operation and template design, making reasonable use of these string slicing functions will greatly enhance the professionalism and attractiveness of your website content.

Frequently Asked Questions

问:If my text content is shorter than the set truncation length, will the ellipsis still be displayed? Answer: No.AnQiCMS these extractors are very smart.If the length of the original content (whether in terms of characters or words) does not exceed the limit you set, the filter will output the text as is without adding an ellipsis at the end.An ellipsis will only appear when the content is actually truncated to indicate that the content is incomplete.

问:Can I use multiple filters on the same template variable in succession? Answer: Yes, AnQiCMS's template engine supports chained filter calls.|Concatenate them together, they will process the variables in order from left to right. For example, you may want to convert the text to lowercase before truncating:{{ item.Title|lower|truncatechars:50 }}.

Q:truncatechars_htmlandtruncatewords_htmlCan the filter handle all complex HTML structures? For example, nested?div/scriptTags, etc.? Ans:_htmlSeries filters are designed to maintain the integrity of the HTML structure as much as possible, particularly skilled at handling common text format tags (such as<b>/<i>/<a>/<p>EnglishHowever, for very complex or HTML structures containing embedded scripts, although they strive to close tags to prevent rendering errors on the page, overly complex HTML content may still cause slight differences in display when forced to be truncated.Therefore, for content that includes complex interactions or scripts, it is recommended to preprocess it or consider not extracting it before, to ensure that the functionality is not affected.