How to use AnQiCMS filter to batch modify specific attribute values in HTML content?

Calendar 👁️ 68

In website content management, we often encounter scenarios where we need to uniformly adjust or batch modify specific attribute values in a large amount of HTML content. For example, you may need to update the height attributes of all images, or add specific attributes to some links.relProperties, even adjusting the styles of some tags generated by the rich text editor.AnQiCMS provides flexible tools to meet these needs, among which the batch replacement feature is a direct tool for modifying stored content, and the template filter can dynamically convert content during output, combining both can efficiently manage and optimize website content.

The first part: Using the AnQiCMS background batch replacement function to directly modify the stored content

When your goal is to permanently modify the specific attribute value of the stored HTML content in the database, the "Document Keyword Replacement" feature of AnQiCMS backend is your preferred tool.This feature allows you to define replacement rules and apply them to the entire site content, supporting both simple text replacement and advanced regular expression replacement. Regular expressions are particularly powerful when it comes to precisely modifying HTML attribute values.

  1. Function entryIn AnQiCMS backend, navigate to "Content Management" -> "Document Management".Above the document list page, you will see the option 'Document keyword replacement'.Click to enter the feature interface.

  2. Configure replacement rulesHere, you need to enter the 'search word' and 'replacement word'.

    • Normal text replacementIf you just want to replace a fixed attribute value (such aswidth="100") with another fixed value (such aswidth="150"),can be directly entered.
    • Regular expression replacementThis is the core of batch modification of HTML specific attribute values. Regular expressions allow you to match complex patterns, thus precisely locating and modifying attribute values.
      • Syntax hintRegular expression rules need to be used{starts with}End. For example,{[0-9]+}You can match consecutive numbers. Combine capturing groups and backreferences to achieve very fine modifications.
  3. Practical example: Modify the image width attributeAssuming that all images used in the article content of your website,width="xxx"Such inline properties to define width, now you want to unify the width properties of all imagesmax-width: 100%; height: auto;To achieve better responsive layout, while removing the oldwidthProperty.

    • Search terms (regular expressions):(<img[^>]*?)\swidth="\d+"([^>]*?>)
      • The meaning of this regular expression is:
        • (<img[^>]*?): Capture<img>Label start, as well as any non->character, until encounteringwidththe front of the attribute. This is the first capture group.
        • \swidth="\d+": Match a space,width=", Any number(\d+), and".
        • ([^>]*?>): CapturewidthAfter the attribute to>All content to the end of the tag. This is the second capture group.
    • Replacement word:$1 style="max-width: 100%; height: auto;"$2
      • $1and$2Referenced the first and second capture groups in the regular expression.
      • This means we will retain the other part of the image tag, insert the newstyleattributes, and automatically remove the oldwidthProperty.
    • Apply replacement: Check the content range you want to apply the replacement to (such as all articles, products, etc.), then click to execute. The system will traverse all content and replace it in bulk according to the rules you define.

Important reminder: Be cautious when using regular expressions for batch replacements. It is recommended to test in a development environment first and to back up the database in advance to prevent irreversible content damage due to incorrect operations.

The second part: Combine AnQiCMS filter to dynamically convert and optimize HTML content display

AnQiCMS template filters provide the ability to dynamically transform content when outputting to the front-end page.This modification does not directly change the original HTML content stored in the database, but takes effect immediately when the user browses the page, which is very suitable for optimization and adjustment at the display level.

Filters are usually applied to the output of content variables, with the basic format of{{ 变量名|过滤器名称:参数 }}.

  1. replaceFilter: Text-level attribute value replacementAlthough batch replacement in the background is more powerful, in some cases, you may only want to replace on the front end,

Related articles

How to quickly view the original HTML content contained in the variable when debugging the AnQiCMS template?

During the template development process of Anqi CMS, we often need to view the content contained in variables, especially when variables may carry HTML structures. How to quickly and accurately see the original HTML content instead of the parsed or escaped result by the browser is the key to efficient debugging.Anqi CMS uses a template engine syntax similar to Django, providing several powerful tools to help us solve this problem.Understanding Debugging Needs: Why Do We Need to View the Original HTML?

2025-11-08

In AnQiCMS, can you set a default HTML content filtering strategy to be applied to all new published content?

In AnQiCMS, content management is one of the core functions, and ensuring the quality and security of published content is a focus for many website operators.About whether it is possible to set a default HTML content filtering strategy to apply to all new published content?This is indeed a question worth discussing. From the features provided by AnQiCMS, the system has adopted a multi-dimensional strategy in content security and filtering, and some of its functions indeed have an impact on the HTML of new published content.### Core Feature Exploration

2025-11-08

How to ensure that the rich text content of AnQiCMS back-end editor is safe HTML when displayed on the front end?

In daily website content operation, we often use the rich text feature of the AnQiCMS backend editor to carefully arrange articles, product details, or single-page content to present more beautiful and attractive page effects.From setting title styles, inserting images, creating lists, to embedding videos, rich text editors bring us great convenience and creative freedom.However, behind these flexible formatting capabilities, there is also a hidden, non-negligible security issue - how to ensure that the content containing custom HTML structures is displayed safely and correctly on the front end of the website? After all

2025-11-08

Why does the `escape` filter sometimes cause HTML entities to be doubly escaped?

When using AnQiCMS for website content management and template development, we may encounter some confusing display issues, one of which is the double escaping of HTML entities.This usually appears as HTML tags that should be displayed as formatted text on the page, but are instead become `&lt;`p & gt; `such visible characters, even worse & amp;lt;p&amp;gt;`. This phenomenon not only affects the visual appearance of the website, but may also cause the content to lose its original style

2025-11-08

How should AnQiCMS template files be named and organized to achieve **display effects??

In Anqi CMS, the display effect of the website is closely related to the naming and organization of the template files.A well-planned template structure not only makes the website look neat and beautiful but also greatly improves development efficiency, facilitates later maintenance, and ensures that content is presented in different scenarios. ### The Foundation of Template Files: `/template` Directory and `config.` All visual presentations of Anqi CMS website start from the `/template` directory.This is the home of all template files. Each set of independent templates

2025-11-08

How to ensure that AnQiCMS template files are encoded in UTF-8 to avoid garbled page display?

During the process of building a website with AnQiCMS, you may occasionally encounter the situation where the displayed content is garbled, especially Chinese characters.This not only affects the aesthetics and user experience of the website, but may also have a negative impact on search engine optimization (SEO).The problem of garbled characters is usually related to the inconsistent encoding format of template files, and ensuring that AnQiCMS template files are saved in UTF-8 encoding is a key step to solving this problem.Why UTF-8 Encoding is Crucial?

2025-11-08

How to use Django template engine syntax to display variables and logic structures?

AnQiCMS (AnQiCMS) uses a template engine syntax similar to Django in template creation. This design philosophy aims to provide content operators and developers with a powerful and easy-to-use tool, allowing them to more flexibly control the display of website content.By mastering this template syntax, you can easily display dynamic data on the website front end and control the presentation logic of content based on specific conditions.### One, the core composition of template syntax: variables and logical structures The template syntax of AnQi CMS mainly consists of two major parts

2025-11-08

What page adaptation modes are supported by AnQiCMS templates, and how to select and implement responsive display?

In the multi-screen era, users access websites through various devices, which has become the norm.To ensure that the website can provide a smooth and friendly experience on any device, page adaptation has become an indispensable part of website construction.AnQiCMS (AnQi Content Management System) fully considers this requirement, providing a variety of flexible template adaptation modes to help users easily cope with display challenges on different devices.

2025-11-08