In the daily content operation of AnQi CMS, we often encounter situations where we need to adjust and format text content. Among them,replaceA filter is a very practical tool that allows us to replace specific keywords in the template output. However, regardingreplaceDoes the filter support replacing multiple different keywords at once, which is indeed a concern for many users.

By reviewing the Anqi CMS documentation, especially the section on template filters, we can clearly understandreplacethe way filters work. Its design philosophy is aimed atA single keyword pairPerform the replacement. That is, you can only specify an 'old keyword' and a 'new keyword' to perform the replacement operation.

replaceThe basic syntax of the filter is as follows:{{obj|replace:"old,new"}}

here,objRepresents the string variable you want to operate on,"old,new"which is the replacement rule, including,oldwhich is the keyword you want to be replaced,newIs the replacement new keyword. Use English comma between them.,It separates.

For example, if you have a text such as 'Welcome to AnQi CMS' and you want to replace 'AnQi' with 'AnQi', you would use it in the template like this:{{ "欢迎使用安企CMS"|replace:"安企,AnQi" }}The result will be: "Welcome to AnQiCMS".

The document also mentions several special cases:

  • IfoldIt is empty,replaceThe filter will insert at the beginning of the string and after each UTF-8 sequence (which can be understood as each character)newFor example:{{ "欢迎使用安企CMS"|replace:",-" }}It will output.-欢-迎-使-用-安-企-C-M-S-.
  • IfnewIf it is empty, thenoldwill be removed. For example:{{ "欢迎使用安企CMS"|replace:"安企," }}It will output.欢迎CMS.

Looking at these examples and grammar specifications,replaceThe filter is designed to perform precise 'one-to-one' replacement, that is, each operation targets a specific 'old word' to be replaced with a 'new word'.Does not support directlyIn a filter call, a list of keywords or a mapping table is passed to replace multiple different keywords simultaneously.

What if I need to replace multiple different keywords in the template?

AlthoughreplaceThe filter itself does not support replacing multiple different keyword pairs at once, but this does not mean that we cannot achieve a similar effect. A common practice isChainingreplaceFilter. You can replace multiplereplaceThe filter is connected in order, performing multiple replacements on the same string variable.

For example, if you want to replace “AnQi” with “AnQi”, and replace “CMS” with “Content Management System”, you can do this:{{ "欢迎使用安企CMS"|replace:"安企,AnQi"|replace:"CMS,Content Management System" }}This code will first replace 'AnQi' with 'AnQi', and then replace 'CMS' with 'Content Management System', and finally output 'Welcome to AnQi Content Management System'.

Supplemental description of the backend "full site content replacement" function

It is noteworthy that AnQi CMS provides a powerful feature named "Full Site Content Replacement" or "Document Keyword Replacement" in the backend management panel (under "Content Management" -> "Document List"). This feature allows operatorsReplace keywords or links on the entire site in bulkIt is usually used for:

  • To cope with changes in content strategy, it is necessary to update specific words uniformly.
  • Perform SEO optimization, such as adjusting keyword density or link structure in bulk.
  • Correct the error information in the entire site content.

This backend feature supports one-time configuration of multiple replacement rules, and also supports regular expressions, which means you can implement very complex and flexible batch replacement operations. However, it is different from the template in thereplaceFilters are two different concepts:

  • Backend 'full site content replacement': It will directly modify the content in the database, ispermanentthe change.
  • TemplatereplaceFilterIt only temporarily processes the output string during page rendering,It does not change the original database content.

In summary, AnQi CMS'sreplaceThe template filter is a concise and efficient single keyword replacement tool.When you need to perform multiple replacements in a template, you can use the chain calling method;For the need to make large-scale, permanent changes to website content, it is recommended to use the 'Full Site Content Replacement' function provided by the backend.Understanding the differences and application scenarios of these two will help us manage website content more efficiently and safely.


Frequently Asked Questions (FAQ)

  1. replaceDoes the filter support regular expressions?Currently described in the Anq CMS documentationreplaceThe template filter only supports the replacement of plain strings, and does not directly support regular expressions.If you need to use regular expressions in replacement, you need to use the 'Document Keyword Replacement' feature in the background management interface, which explicitly mentions support for regular expression rules.

  2. If I need to include commas in both the 'old word' and 'new word' I am replacing (,What should I do?due toreplaceDoes the filter use commas (,Therefore, if your keyword itself contains a comma, it may cause parsing errors.In this case, it is usually recommended to reconsider the design of the keywords, avoid using commas;Or in a custom template logic, try to handle strings containing commas, but this will increase the complexity of the template, so it is better to avoid it.

  3. ChainingreplaceWill the filter affect the website's performance?ChainingreplaceThe filter will process the same string variable multiple times.For a small number of replacement operations and strings of ordinary length, the performance impact is negligible.If you need to perform a large number of chained replacements on a lot of text in a template, theoretically, it may increase the rendering time of the page.In such extreme situations, priority should be given to whether these replacement logic can be moved to the content release stage (using the backend "document keyword replacement" function), or processed in the controller layer of the Go language to optimize the front-end rendering performance.