Inside the AnQiCMS templatereplaceFilter, can it handle dynamic content?

During the process of using AnQiCMS to build and operate websites, we often need to display various content on the page.This content may come from background documents, products, category details, or may also be dynamically read brand information or contact information through system settings.With the continuous enrichment of website content and the adjustment of operation strategies, we may encounter the need to quickly and flexibly replace certain keywords or phrases when displaying on the page.For example, a brand name needs to be updated, or a keyword needs to be uniformly expressed, or the content contains outdated links that need to be corrected.

At this time, the template filter provided by AnQiCMS is particularly important. Among them,replaceThe filter, with its intuitive replacement feature, often becomes the focus of attention. However, a common question is: thisreplaceFilter, will it work effectively when processing the content we dynamically read from the background database?

UnderstandingreplaceThe basic function of the filter

First, let's take a look backreplaceThe basic usage of the filter. In AnQiCMS template syntax, it allows us to replace specific 'old words' with 'new words'. Its basic format is{{ 变量 | replace:"旧词,新词" }}For example, if you have a simple string"欢迎使用AnQiCMS"And if you want to replace 'AnQiCMS' with 'AnQi Content Management System', you can write it like this:

{{ "欢迎使用AnQiCMS" | replace:"AnQiCMS,安企内容管理系统" }}
{# 输出结果:欢迎使用安企内容管理系统 #}

This filter will traverse the target string, find all matches of the "old word", and then replace it with the specified "new word", and return the new string after replacement.If the old word is empty, it will match at the beginning of the string and after each UTF-8 sequence; if the new word is empty, it is equivalent to removing the old word.

What is the dynamic content in AnQiCMS?

What content is considered dynamic?In AnQiCMS, most of the data we publish and configure from the backend management interface is dynamic content.

  • Document content(archive.Content): The main content of articles, product detail pages, usually input by the editor.
  • Title and description(archive.Title,archive.Description,category.Titleetc.): Title, category name, and content summary read from the database.
  • Custom field (archiveParamsfield in: Customized according to business requirements, such as article authors, product models, etc.
  • System settings and contact information (system.SiteName,contact.Cellphoneetc.): The information configured in the background "Global Settings" and "Contact Information Settings".

This content is not hardcoded in the template file, but is dynamically retrieved and loaded into the template from the AnQiCMS backend database at runtime.

replaceThe perfect combination of filter and dynamic content

Return to our core question:replaceIs the filter effective for dynamic content? The answer is yes, and it works very well.

The logic behind it is actually very simple: The AnQiCMS template engine will first parse the value of the variable when processing filters.No matter whether this value is written directly in the template or dynamically read from the database, the filter receives it as a specific string.Once the filter takes this string, it will perform the replacement operation according to the established rules, and then return the processed new string to the template for display.

This means, you can make very flexible use ofreplacea filter to handle various dynamic scenarios:

  1. Unified SEO keyword expressionreplacethe filter.

    {# 动态获取文章标题并替换 #}
    <h1>{{ archive.Title | replace:"AnQiCMS,安企内容管理系统" }}</h1>
    
    
    {# 动态获取文章内容并替换 #}
    <div>{{ archive.Content | replace:"AnQiCMS,安企内容管理系统" | safe }}</div>
    
  2. Quickly adjust brand or product nameIf your product line is updated, the name of an old product model needs to be unifiedly changed to a new name without modifying the massive content in the background. In the product details page template, use the product name and related description fields forreplaceThe filter can take effect immediately, avoiding大规模的后台内容编辑work.

  3. Batch correct the old links in the content.After the website is reconstructed, the structure of some internal links may have changed, or the external partner has changed the domain name. You can use it in the template toarchive.Contentfor dynamic content containing linksreplaceFilter, replace the old link domain with the new link domain to ensure that users can access the correct page when they click.

    {# 假设文章内容中包含旧域名 old.example.com,需要替换为 new.example.com #}
    <div>{{ archive.Content | replace:"old.example.com,new.example.com" | safe }}</div>
    
  4. Dynamic content formattingSometimes dynamic content may contain some irregular formats, such as extra spaces or unified punctuation marks.replaceFilters can also be used to make such subtle format corrections.

Practical suggestions and **practice

ThoughreplaceThe filter function is powerful and flexible, but in actual application, we still need to pay attention to some details to ensure **effect and website performance:

  • Difference from the background "Site-wide Content Replacement": AnQiCMS backend itself provides a powerful "site-wide content replacement" function, located in the "Content Management" "Document Management" page, which can replace all keywords or links in all documents with one click. This function is for the database inOriginal dataModify once, the data will be permanently changed. And the template inreplaceThe filter is at the time of page renderingTemporarily replace the display levelIt will not modify the original data in the database.So, if your replacement is global, permanent, and you do not want to keep old data, it is more appropriate to use the background feature.replaceThe filter is the ideal choice.

  • cooperatesafeusing a filterIf your replacement target is HTML content (such as article textarchive.Content) and you want the replaced HTML code to be correctly parsed by the browser rather than displayed as plain text, then remember to cooperatesafeto use the filters together.safeThe filter tells the template engine that this content is safe and does not need to be HTML escaped.

  • Building the replacement chain: You can have multiplereplaceFilter connected together to form a replacement