In the template design of AnQi CMS, flexibly using filters is the key to improving content display efficiency and user experience. Especially text processing filters, such asreplaceused for content replacement, as well astruncatechars/truncatewordsThis is a filter used for text truncation, and the combination of multiple filters can achieve more refined content control.Understanding the independent functions of these filters and their combined effects can help us better plan and optimize the presentation of website content.
Understanding Core Tools:replaceWith text excerpt filter
Firstly, let's briefly review these several important text processing filters:
replaceFilter:As the name implies, its main function is to find and replace specific content in strings. The format is usually{{ 字符串变量|replace:"旧内容,新内容" }}.It is very suitable for content standardization, correcting errors, or hiding certain information.For example,统一文中出现的 specific brand names to abbreviations, or replace certain sensitive words with asterisks, etc.Text extraction filter:The AnQi CMS provides various text truncation methods to meet the needs of different scenarios:
truncatechars:数字:Truncate a string by character count. It will count a specified number of characters from the beginning of the string and add an ellipsis (...) at the end. This ellipsis is included in the total character count.truncatewords:数字:Extract a string based on word count. It will take a specified number of words from the string and add an ellipsis at the end....).truncatechars_html:数字andtruncatewords_html:数字:These are the HTML safe version truncation filters.They can intelligently close incomplete tags when extracting text containing HTML tags, avoiding HTML structure chaos or page display anomalies caused by truncation.This is particularly important when handling rich text content.
The effect of combining: the order is crucial
WhenreplaceWhen the filter and text truncation filter are used together, the execution order will directly determine the final effect.This is like in the kitchen, whether it's to cut the vegetables first and then stir-fry, or to stir-fry first and then cut, the result is quite different.
1. FirstreplaceAfter cutting (usually recommended)
This order means that we first process and modify the original content, and then extract the processed content.This is a more reasonable and recommended approach in most content operation scenarios.
Execution process:Original content →replaceFilter → Cut filter → Final output
Example scenario:Assuming there is an abstract of an article about the 'AnQiCMS Content Management System', the original text is: 'AnQiCMS Content Management System is dedicated to providing efficient and customizable solutions for small and medium-sized enterprises, helping them carry out content marketing and SEO optimization efficiently.'
We hope to replace all 'AnQiCMS' with a more concise 'AnQiCMS', and finally display a summary of only 25 characters.
{% set original_text = "AnQiCMS 内容管理系统致力于为中小企业提供高效、可定制的解决方案,帮助企业高效地开展内容营销和SEO优化。" %}
{% set replaced_text = original_text|replace:"AnQiCMS,安企CMS" %}
{{ replaced_text|truncatechars:25 }}
Effect Analysis:
original_text|replace:"AnQiCMS,安企CMS"The original text 'AnQiCMS' will be replaced with '安企CMS', resulting in: '安企CMS Content Management System is dedicated to providing efficient and customizable solutions for small and medium-sized enterprises, helping them to carry out content marketing and SEO optimization efficiently.'|truncatechars:25Extract characters from this long text that has already been replaced.- The final output result may be: 'AnQi CMS, a content management system dedicated to...'
This order ensures that all necessary replacement operations are completed before the text is truncated.No matter where the content to be replaced is located in the string, it will be processed, ensuring consistency and accuracy of the content, especially suitable for content purification, brand name unification, and other operations.
2. Cut first.replace[Use with caution]
This order is to replace after the content has been truncated. Usually, this approach may lead toreplaceThe filter cannot find the target string because the target string may have been deleted or become incomplete during the slicing process.
Execution process:Original content → Slicing filter →replaceFilter → Final Output
Example scenario:This is still the original text, but this time we first truncate and then replace:
{% set original_text = "AnQiCMS 内容管理系统致力于为中小企业提供高效、可定制的解决方案,帮助企业高效地开展内容营销和SEO优化。" %}
{% set truncated_text = original_text|truncatechars:10 %}
{{ truncated_text|replace:"AnQiCMS,安企CMS" }}
Effect Analysis:
original_text|truncatechars:10The original text will be truncated to: ”AnQiCMS …”|replace:"AnQiCMS,安企CMS"At this momentreplaceThe filter will search for “AnQiCMS” in “AnQiCMS …”. Since “AnQiCMS” is incomplete after truncation (this example may happen to be complete) or truncated by an ellipsis,replaceMay not find a complete match item.- The final output result may be: ”AnQiCMS …“ (i.e., replacement failed, content remains as truncated).
This order is only applicable under very specific requirements, such as when you only care about replacing within a very short preview text and are sure that the target of replacement will not be truncated.But in most cases, this will lead to unpredictable results.
Application scenarios and **practice
Understanding the importance of execution order allows us to better apply these filters:
Content standardization and purification (
replacePriority):In scenarios where abstracts need to be displayed, such as in article lists, product introductions, etc., it is often necessary to uniformly process the title or description.For example, a website may crawl content from different channels, and the name of a product or company may be written differently (such as 'AnQiCMS', 'AnQiCMS System', 'AnQiCMS').replaceTranslate these different writings into a standardized form, and then usetruncatecharsortruncatewordsGenerate a neat summary.If the content contains HTML tags and needs to be replaced and truncated, be sure to use
truncatechars_htmlortruncatewords_htmlto avoid truncation from destroying the HTML structure. FirstreplaceEnsure that the text within the label is replaced before performing HTML-safe truncation.Optimize display length (
replaceAuxiliary truncation):If some commonly used words or phrases are naturally quite long, they may take up too many characters when extracting summaries, leading to truncated valid information. You might consider usingreplaceReplace these long phrases with more concise synonyms (if the context allows), so that in subsequenttruncatecharscutting, more core information can be retained.Prevent semantic issues caused by cutting:For example, you have a description that says 'Please visit our help document center to get more AnQiCMS usage guides.' If you directly
truncatechars(15)It might receive "Please visit our help document…".AnQiCMSThis critical information is missing. If it is firstreplacePlease visit our help document center to get more information on the Safe CMS user guide. Then, if you截取, you might get "Please visit our help document center…", although安企CMSUnable to display completely, but at least the brand name was processed. A better way would be,AnQiCMS[en] Make sure it is a keyword, ensuring it is at the beginning of the text, or replace it with a shorter word to ensure the key information is still displayed.
[en] In summary, when using the Anqi CMS template, combinereplaceFilter and text truncation filters should always bereplaceOperation placed before the text truncation operation.This ensures that all necessary text modifications are applied to the complete original content, thus achieving the expected and consistent final display.At the same time, choosing the appropriate extraction filter based on the nature of the content (plain text or HTML) is crucial for ensuring the page is aesthetically pleasing and functions normally.
Common Questions (FAQ)
Q1: Why sometimesreplaceFilters are appliedtruncatecharsWhy do they not
A1:usually due to the order of filter execution. IftruncatecharsBeforereplaceIf it is executed, thenreplaceThe filter will only be able to search for and replace the target in the truncated text. If the original target string is partially deleted during truncation or included within ellipses,replaceThe filter cannot find and replace it. To ensurereplaceto take effect, it should be executed first.replaceThe filter, and then executetruncatecharsFilter.
Q2:replacethe new content after replacement will affecttruncatecharsthe character count?
A2:Will do. WhenreplaceAfter the filter completes the replacement, it generates a new string.truncatecharsOr any other slicing filter will calculate the character count and perform the slice based on this newly modified string.Therefore, if the replacement operation makes the text longer or shorter, it will directly affect the length and display of the final cut content.
Q3: Using in HTML content at the same timereplaceHow to avoid breaking the HTML structure when cutting filters?
A3:It is strongly recommended to use when dealing with text containing HTML tags.truncatechars_htmlortruncatewords_htmlThis