In AnQi CMS template design, flexibly using filters is the key to improving content display efficiency and user experience. In particular, text processing filters such asreplaceare used for content replacement, as well astruncatechars/truncatewordsA filter used for text truncation, their combination 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.
Get to know the core tools:replaceWith text extraction filter
First, let's briefly review these 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, error correction, or hiding certain information.For example, unify the names of specific brands in the text to abbreviations, or replace certain sensitive words with asterisks, etc.Text extraction filter:AnQi CMS provides various text truncation methods to meet the needs of different scenarios:
truncatechars:数字:Truncate 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 (...), and this ellipsis is included in the total character count.truncatewords:数字:Truncate string by word count. It will truncate 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 of the truncation filters. They can intelligently close incomplete tags when truncating text containing HTML tags, avoiding HTML structure chaos or display exceptions caused by truncation.This is particularly important when handling rich text content.
The effect of combination: order is crucial
WhenreplaceThe order of execution when combining the filter and text truncation filter will directly determine the final effect.This is like in the kitchen, whether you chop the vegetables first and then stir-fry, or stir-fry first and then chop, the result is quite different.
1. FirstreplacePost-capture (usually recommended)
This order means that we first process and modify the original content, and then we extract the processed content.This is a more reasonable and recommended practice in most content operation scenarios.
Execution process:Original content →replaceFilter → Capture filter → Final output
Example scenario:There is an article summary about the "AnQiCMS Content Management System", the original text is: "AnQiCMS Content Management System is committed 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 the 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 committed to providing efficient and customizable solutions for small and medium-sized enterprises, helping them carry out content marketing and SEO optimization efficiently.'|truncatechars:25Rerun the character truncation on this long text that has already been replaced.- The final output result may be: "AnQi CMS Content Management System is committed to..."
This order ensures that all necessary replacement operations have been completed before the text is truncated.No matter where the content to be replaced is located in the string, it can be processed, thus ensuring consistency and accuracy of the content, especially suitable for content purification, brand name unification, and other operations.
2. Truncate first.replace(Use with caution)
This order is after the content is truncated and then replaced. 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 truncation process.
Execution process:Original content → Truncate filter →replaceFilter → Final Output
Example scenario:It is the original text above, but this time we first extract 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 thisreplaceThe filter will search for "AnQiCMS" within "AnQiCMS …". Since the "AnQiCMS" has been incomplete after truncation (this example may happen to be complete), or truncated by an ellipsis,replaceIt may not be possible to find a complete match.- The final output result may be: ”AnQiCMS …“ (i.e., replacement failed, content remains as truncated).
This order is only applicable under very specific requirements, for example, if you only care about replacing in a very short preview text, and are sure that the target of replacement will not be truncated.But in most cases, this can lead to unpredictable results.
Application scenarios and **practice
Understood the importance of execution order, we can better apply these filters:
Content standardization and purification(
replacePriority):In scenarios where abstracts need to be displayed in article lists, product introductions, etc., it is often necessary to unify the processing of titles or descriptions.For example, a website may collect content from different channels, and the wording of a product or company name may vary (such as “AnQiCMS”, “AnQiCMS System”, “AnQiCMS”).You can use it firstreplaceUnify these different styles into a standard 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破坏HTML structure. FirstreplaceEnsure that the text within the tags 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 can consider usingreplaceReplace these long phrases with more concise synonyms (if the context allows), so that in subsequenttruncatecharswhen extracted, more core information can be retained.Prevent semantic issues caused by extraction:For example, you have a description that says 'Please visit our help document center for more AnQiCMS usage guides.' If you directly
truncatechars(15)You might get 'Please visit our help document...'AnQiCMSThis key information is missing. If firstreplacePlease visit our help document center to get more information on the use of our security CMS. When extracted, it may result in something like 'Please visit our help document center...' although安企CMSPartly visible, but at least the brand name was processed. A better way would be ifAnQiCMSIs a keyword, make sure it is in the front of the text, or replace it with a shorter word to ensure that the key information is still displayed.
In summary, combine the use of Anqi CMS templates.replaceThe filter should always be placed before the text extraction filter.replaceThe operation should be placed before the text extraction operation. This ensures that all necessary text modifications are applied to the complete original content, thus obtaining the expected and consistent final display effect.At the same time, choosing the appropriate extraction filter based on the nature of the content (plain text or HTML) is the key to ensuring the page is beautiful and functional.
Frequently Asked Questions (FAQ)
Q1: Why sometimes?replaceThe filter is appliedtruncatecharsWhy does it not work after?
A1:This is usually due to the order of filter execution. Iftruncatecharsbeforereplacethen executereplaceThe filter will only be able to search and replace the target in the truncated text. If the original target string is partially deleted or contained in an ellipsis during truncation,replaceThe filter cannot find and replace it. To ensurereplacetake effect, you should first executereplacethe filter, and then executetruncatecharsfilter.
Q2:replacethe new content after replacement will affecttruncatecharsthe character count?
A2:WillreplaceThe filter generates a new string after completing the replacement.truncatecharsOr any other cut filter will be based on this modified new string to calculate the number of characters and perform truncation.Therefore, if the replacement operation makes the text longer or shorter, it will directly affect the length and display of the final extracted content.
Q3: Use within the HTML content at the same timereplaceHow to avoid destroying the HTML structure when extracting filters?
A3:It is strongly recommended to use when processing text containing HTML tagstruncatechars_htmlortruncatewords_htmlThis