In AnQiCMS content operation, "Content Keyword Replacement" is undoubtedly a powerful tool to improve efficiency and optimize content quality.It allows operators to batch and globally adjust specific words or phrases in the website content, whether for brand consistency, SEO optimization, or information updates.However, relying solely on the backend replacement feature may sometimes be insufficient to meet the refined needs of front-end display.This cleverly combines AnQiCMS template filters to bring more flexibility and control to content display, achieving a better user experience.
AnQiCMS background content keyword replacement feature overview
First, let's review the "Content Keyword Replacement" feature of AnQiCMS backend.This feature is located under the "Content Management" module and allows you to define a set of replacement rules.
- Global and batch processing:You can set up a pair of 'Find' and 'Replace with' keywords, and the system will be able to batch process the entire site's content, greatly reducing the burden of manual modification.
- supports regular expressionsFor complex replacement logic, such as matching specific formats of phone numbers, email addresses, etc., the system supports the use of regular expressions and provides powerful pattern matching capabilities.This makes the replacement operation more flexible and accurate.
- Built-in rules and convenient operations:AnQiCMS even pre-sets some common rules, such as matching for email, date, phone numbers, etc., making it easy for non-technical personnel to get started.
This feature is usually executed automatically when content is published or updated, directly affecting the content stored in the database, or processing it before the content is read from the database and rendered to the frontend.Its goal is to ensure the uniformity and accuracy of web content.
The role and value of template filters
Different from the keyword replacement in the background, the template filter takes effect when the content is rendered by AnQiCMS's template engine to the front-end page. Its features are:
- Display-side processing:The filter does not change the original content stored in the database. It only processes the data again according to predefined rules when the content is presented to the user.
- Flexible and versatile:AnQiCMS的模板引擎(similar to Django template engine)提供了丰富的过滤器,可以对字符串、数字、数组等多种数据类型进行格式化、截取、拼接、判断等操作。
- English qualitySince it does not affect the original data, even if the filter logic fails, it only affects the front-end display and will not damage the core data of the website.
The backend keyword replacement is considered 'root treatment'——it modifies the source of the content; then, the template filter is 'surface treatment'——it makes refined modifications and supplements to the content in the display phase.Both combined, it can achieve more efficient and personalized content operation.
Combine practice: Apply filters for validation or preprocessing in front-end templates
In AnQiCMS, the content keyword replacement has greatly improved the uniformity of the content.On this basis, we can apply filters in the front-end template for more detailed "front-end validation or preprocessing".
1. Further beautify or format the replaced content
Assuming the backend has replaced all "AnQiCMS" with "AnQi Content Management System", you may wish to highlight this brand word on the frontend or abbreviate it in specific scenarios.
Highlighting/Style Enhancement of Local Text: You can use it in the template,
replaceto replace the content processed by the backend again, in order to achieve the beautification of the display level.{{ archive.Content|replace:"安企内容管理系统","<strong>安企内容管理系统</strong>"|safe }}Here,
safeFilter is necessary to ensure<strong>Tags can be parsed correctly by the browser rather than displayed as plain text.Dynamic content truncation and omission: If the replaced descriptive content in the background becomes too long and affects the page layout, you can use
truncatecharsortruncatewordsthe filter for intelligent truncation.<p>{{ archive.Description|truncatechars:100 }}</p>This will automatically truncate and add an ellipsis when the description exceeds 100 characters, keeping the page tidy.
2. Conditional display based on content check.
PasscontainFilter checks if the content contains specific keywords and then cooperatesifLogical tags for conditional rendering. This is very useful when the backend replacement may not cover all complex logic.
- Important information reminderFor example, you may want to display a prominent icon or text next to the article title when the article content contains a specific notification word (even if it has been standardized and replaced in the background).
<h3> {{ archive.Title }} {% if archive.Content|contain:"新政策发布" %} <span style="color: red; font-weight: bold;">[重要通知]</span> {% endif %} </h3>
3. Cleaning or supplementing replacement may produce unnecessary characters
Although the background replacement feature is powerful, in some special cases, the replacement rules may cause extra characters, or you may need to further format some numbers, or links.
Remove extra separatorsIf unnecessary hyphens or spaces are accidentally introduced during the background replacement process,
cutThe filter can easily remove them.{{ archive.Title|cut:"-" }}For example, to
标题-副标题Processed into标题副标题.Dynamic generation and link handling: Although AnQiCMS backend keyword replacement can generate links, but on the front end,
urlizeThe filter can automatically convert URLs in the text to clickable hyperlinks and add them by default.rel="nofollow"This is a useful supplement for SEO and user experience.<p>联系我们:{{ contact.Email|urlize|safe }}</p>
4. Content Security and Compliance Front-end Assistance
Even though AnQiCMS has built-in sensitive word filtering function, in specific display scenarios or as a final line of defense, front-end filters can also play an auxiliary role.
- Front-end sensitive word desensitization(As an addition rather than a substitute):
It is not recommended to handle core sensitive word filtering on the front end, but if there are some scenarios where you want to perform additional display processing for specific words, you can do so in the template.
Please note that this is only for demonstration purposes and should not be considered as a primary security measure.{% set displayed_content = archive.Content|replace:"公司内部名称","***" %} {{ displayed_content|safe }}
Attention Points and **Practice
- Clear division of labor:Assign global, persistent, complex, and data-altering tasks to background functions; assign lightweight, display-only, dynamically adjustable, and performance-insensitive processing tasks to frontend template filters.
- Performance considerationsAvoid using a large number of complex or nested filters in templates, especially within loops, as this may affect page loading speed. Prefer to complete performance-sensitive batch processing in the background.
- Debugging and TestingWhen applying filters, be sure to thoroughly test in the development environment to ensure the display is as expected, and avoid unexpected format errors or data display issues. Especially when dealing with HTML content,
|safeThe use of filters should be particularly cautious to prevent XSS risks. - Maintainability: Keep the filter logic simple and clear.