In the daily content operation of AnQi CMS, we often encounter situations where we need to adjust and format text content. Among which,replaceFilter is a very practical tool, it allows us to replace specific keywords in the string during template output. However,replaceDoes the filter support replacing multiple different keywords at once, which is indeed a concern for many users.
By referring to the documentation of Anqi CMS, especially the instructions on the template filter part, we can clearly understandreplacethe working method of filters. Its design philosophy is forSingle keyword pairPerform replacement. That is, you can only specify one 'old keyword' and one 'new keyword' to perform the replacement operation.
replaceThe basic syntax for using a filter is as follows:{{obj|replace:"old,new"}}
Here,objRepresents the string variable you want to operate on,"old,new"is the replacement rule, whereoldis the keyword you want to be replaced,newis the new keyword after replacement. They are separated by an English comma.,Split the content.
For example, if you have a text "Welcome to AnQi CMS" and you want to replace "AnQi" with "AnQi", you will use it in the template like this:{{ "欢迎使用安企CMS"|replace:"安企,AnQi" }}The output will be: 'Welcome to AnQiCMS'.
The document also mentions several special cases:
- If
oldThe content inside the loop will not be rendered, instead, the content of the block will be displayed.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-. - If
newIf it is empty, thenoldit will be removed. For example:{{ "欢迎使用安企CMS"|replace:"安企," }}It will output欢迎CMS.
From these examples and grammatical specifications,replaceThe filter is designed to perform precise "one-to-one" replacement, i.e., each operation targets a specific "old word" to be replaced with a "new word".not directly supportedIn a single filter call, multiple different keywords are replaced simultaneously by passing a keyword list or a mapping table.
What should I do 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 we cannot achieve a similar effect. A common practice ischained usagereplaceFilterYou can concatenate multiplereplaceFilters in order, and perform multiple replacements on the same string variable.
For example, if you want to replace “安企” with “AnQi”, and replace “CMS” with “Content Management System”, you can do it this way:{{ "欢迎使用安企CMS"|replace:"安企,AnQi"|replace:"CMS,Content Management System" }}This code will first replace '安企' with 'AnQi', then on this basis, replace 'CMS' with 'Content Management System', and finally output 'Welcome to AnQi Content Management System'.
Description of the补充说明 for the “Full Station Content Replacement” feature
It is worth noting that the 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 operatorsTranslate keywords or links across the entire site in bulkIt is usually used for:
- To adapt to changes in content strategy, it is necessary to unify the update of specific words.
- Perform SEO optimization, such as bulk adjusting keyword density or link structure.
- Correct error information throughout the site.
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 in the template ofreplaceFilter is a concept with two different meanings:
- Backend 'Full Site Content Replacement': It will directly modify the content in the database, which ispermanentchanges.
- Template
replaceFilter:It only performs temporary processing on the output string when the page is rendered.It does not change the original database content..
In summary, the security CMS ofreplaceThe template filter is a concise and efficient single keyword replacement tool.When you need to perform multiple replacements in the template, you can use the chaining method; for the need to make large-scale, permanent changes to the website content, it is recommended to use the 'Global Content Replacement' feature provided by the backend.Understanding the differences and application scenarios of these two will help us manage website content more efficiently and safely.
Common Questions (FAQ)
replaceDoes the filter support regular expressions?Currently described in the Aiqi CMS document.replaceThe 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.If I need to include commas in both the 'old word' and 'new word' for replacement?
,How should I handle this?Due toreplaceDoes the comma filter affect website performance?,chained usage
replaceWill the filter affect website performance?chained usagereplaceThe 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 a large number of chain replacements need to be performed on a large amount of text in a template, theoretically, it may increase the time it takes to render the page.In this extreme case, consider whether it is possible to move these replacement logic to the content publishing stage (using the backend 'document keyword replacement' feature) or process it in the controller layer of the Go language to optimize the front-end rendering performance.