When using AnQi CMS for content operations, we often encounter scenarios where we need to replace specific text on the page. Among them,replaceThe filter is a very practical tool in the template, but there are indeed some points that users may confuse, such as its operation method with the template variables themselves and the "document keyword replacement" function provided by the system background. Today, let's delve into it in AnQiCMS.replaceThe priority of the filter's replacement operation, and whether it will conflict with the template variables.

AnQiCMSreplaceThe basic principle of the filter.

First, let's understandreplaceThe role of the filter in the template. In the AnQiCMS Django-like template engine syntax,replaceA filter is used to replace the value of a string variable, replacing the 'old word' with the 'new word'. Its basic usage is very intuitive, usually like this:

{{ obj|replace:"old,new" }}

For example, if you have a variable{{ archivetitle }}The value is "Welcome to anqi CMS", if you want to replace "anqi" with "anqi" when displaying on the page, you can write it like this:

{{ archivetitle|replace:"安企,anqi" }}

This will display on the page as 'Welcome to anqiCMS'.

It should be noted that,replaceThe filter only acts on the variable it is processing.Current valueThat is, the template engine will first parseobjThis variable, obtain its specific content (such as “Welcome to Anqi CMS”), and then use this content as input, applyreplaceThe filter performs the replacement. The result is used forDisplayon the page, whereasobjThe variable itself will not be permanently changed in the template.This is like copying a piece of text in a Word document, then editing the copied text, while the original document's content remains unchanged.

Therefore, from this perspective,replaceFilters and template variables do not cause any so-called 'conflicts'. Filters areacting onTools for variable values, they are collaborative rather than competitive relationships. Variables provide data, filters beautify or transform data for display.

Distinguishing and interacting with system-level content replacement.

AnQiCMS not only provides on the template levelreplaceThe filter, also provides more powerful "document keyword replacement" capabilities in the background management feature.In the 'Content Management' module under 'Document Management', we can find the function to batch replace keywords.This feature allows you to replace keywords or links in the entire site or specific document content at once, even supporting regular expression replacement.

Then, what are the differences between these two replacement methods? How will they interact?

  1. The execution time and scope are different:

    • replaceFilter:This is aTemplate rendering whenThe operation to be performed. It only temporarily processes the data passed to the template when generating the HTML page and does not modify the original content stored in the database.Its scope is limited to the variables applied within the current template filter.
    • System-level "Document Keyword Replacement":This is aDuring backend managementThe operation performed. It will directly modifyThe original document content stored in the databaseThis means that once you have performed the replacement in the background, the content has already changed before being loaded into any template.
  2. Priority and interaction:Understood the difference in execution timing between the two, their priority is obvious:The priority of system-level content replacement is higher than that of the templatereplacefilter.

    After a document content is replaced by a system-level feature, if this document content is then passed to the front end through a template variable, and a filter is applied to this variable,replacethen, is there still a place for filters?replacethe filter will act onhas been replaced by the backgroundnew content.

    For example:

    • Suppose the original content of a document is: "Our company sellsApplephone.
    • You are using the 'Document Keyword Replacement' feature in the background, replacing 'apple' with 'orange' in all documents. At this point, the content in the database has become: 'Our company sells'orangephone.
    • Now, you assign the content of this document to a variable in the template{{ article.content }}and applyreplacea filter to try to replace 'apple' with 'banana':
      
      {{ article.content|replace:"苹果,香蕉" }}
      
    • The result displayed on the page will be: 'Our company sells'orangephones.' Why? Because it is inarticle.contentThe variable has been parsed by the template engine before the "apple" in the database has turned into a "orange".replaceThe filter cannot find the word 'apple' during execution, so it will not replace anything.

    On the contrary, if the background replaces 'apple' with 'orange', and the template filter wants to replace 'orange' with 'banana':

    {{ article.content|replace:"橘子,香蕉" }}
    

    Then the page will display: "Our company sells"BananaMobile.

    This is not the so-called 'conflict', but a clear difference in 'execution order' and 'scope'.

Practical suggestions: How to reasonably use two replacement methods

UnderstoodreplaceAfter the characteristics of filters and system-level content replacement, we can better plan content operation strategies:

  • UsereplaceFilter (template level):

    • Application scenarios:The display content of a specific variable needs to be temporarily and dynamically formatted or corrected without affecting the original content.For example, replace sensitive words in the article summary with asterisks, or adjust the specific text of a field according to different front-end display requirements.This is usually a 'what you see is what you get' display requirement.
    • Advantage:Flexible, does not modify original data, easy to test and rollback, does not affect SEO (because search engines crawl the original content).
    • Shortcomings:Cannot process in bulk, there is a performance overhead for each rendering (although usually small).
  • Use background 'document keyword replacement' (at the system level):

    • Application scenarios:It is necessary to make global and permanent changes to a large amount of content, such as: changing the company name, upgrading the product brand, large-scale URL adjustments that require old links to be updated with new links, correcting spelling errors in bulk, and adjusting SEO keyword layout (for example, replacing some keywords with anchor text).This is usually the requirement for 'modifying the source of content'.
    • Advantage:Batch, efficient, directly modify the database, has a direct impact on SEO.
    • Shortcomings:Undoable (unless there is a backup), operate incorrectly.