When operating website content on AnQiCMS, we often encounter situations where we need to adjust the website text content.Sometimes it's the change of brand words, sometimes it's the correction of misspellings, or perhaps it's the temporary adjustment of copy to cater to specific marketing activities.When these modifications need to be reflected at specific locations in the website front-end template rather than the global database, AnQiCMS provides a flexible and efficient solution.
Understand the need to replace keywords within the template
The AnQiCMS backend usually provides the 'Site-wide Content Replacement' feature, which is powerful and convenient, allowing for batch modification of document content or links stored in the database.But this global replacement will directly change the original data of the website and affect all pages that call this content.
However, in certain specific scenarios, we may need to perform local, dynamic keyword replacement for a specific string in a template or for text output by a tag. For example:
- Local tuning:A universal template snippet needs to display different keywords for specific pages, but does not want to modify the original data.
- Formatted output:The content output by the label needs to remove certain specific symbols or phrases to meet the style of the front-end display.
- Temporary modification:When conducting A/B tests or short-term activities, it is necessary to quickly adjust a word in the template, which can be easily restored after the event ends.
In these cases, directly using the built-in filter in the AnQiCMS template file to replace keywords becomes an ideal choice that does not affect the original data and can quickly respond to needs.
AnQiCMS solution:replaceFilter
AnQiCMS's template engine supports a rich set of filters (Filter), among which there is a special tool for string replacement—replaceA filter. Its core function is to replace the specified old keywords in a text string with new keywords and return the new string after replacement.
replaceThe basic usage of the filter is as follows:
{{ obj|replace:"旧关键词,新关键词" }}
HereobjRepresent the text string or variable you want to replace.replaceThe filter accepts a string parameter, which consists of two parts, separated by a comma in English.,Separate: The first part is what needs to be replaced旧关键词, The second part is used for replacement新关键词.
It is worth noting that if旧关键词is empty, it will match at the beginning of the string and after each UTF-8 sequence; if新关键词Empty, then it is equivalent to removing旧关键词.
Actual operation example
Let us understand through several specific examplesreplaceThe use of filters in AnQiCMS templates.
1. Basic text replacement
Suppose there is a piece of hard-coded text in the template, and one word needs to be replaced:
<p>欢迎使用安企CMS,您的内容管理专家。</p>
Now we want to replace “AnQi” with “AnQi”, and we can do it like this:
<p>{{ "欢迎使用安企CMS,您的内容管理专家。"|replace:"安企,AnQi" }}</p>
The result will be:
欢迎使用AnQiCMS,您的内容管理专家。
2. Replace keywords in dynamic content
The most common scenario is to replace keywords in the dynamic content output by AnQiCMS tags. For example, we have an article titlearchive.TitleIt is currently set to 'AnQiCMS template making guide', but we hope to replace 'AnQiCMS' with 'AnQiCMS' when displayed:
<h1>{{ archive.Title|replace:"AnQiCMS,安企CMS" }}</h1>
Ifarchive.TitleIs "AnQiCMS template creation guide", then the page will display:
<h1>安企CMS模板制作指南</h1>
3. Delete specific keywords
When you need to remove a specific keyword from a text, you can新关键词Leave blank. For example, from the article descriptionarchive.DescriptionRemove the word "exclusive":
<p>这篇文章提供了独家的AnQiCMS模板开发技巧。</p>
UsereplaceFilter deletion:
<p>{{ archive.Description|replace:"独家," }}</p>
Output Result:
<p>这篇文章提供了的AnQiCMS模板开发技巧。</p>
4. Continuously replace multiple keywords
If you need to replace the same string multiple times, you can chain the calls.replaceFilter:
<p>{{ archive.Content|replace:"公司,本公司"|replace:"版权所有,保留所有权利" }}</p>
This code will first replace "company" with "our company", and then replace "All rights reserved" with "Reserved all rights".Please note that the order of replacement may affect the final result because the result of the previous replacement will be used as the input for the next replacement.
Usage scenarios and precautions
Application scenarios:
- Text refinement:Quickly correct minor text errors displayed on the page.
- Brand/Term Standardization:Use the new brand name or term in the old template.
- Content Purification:Remove unnecessary or repetitive words from the specific template output.
- SEO Optimization Aid:For a specific page, dynamically adjust the keyword density (use with caution).
Note:
- Performance consideration: Although
replaceThe filter is highly efficient, but performing too many or too complex replacement operations on a single page may slightly affect the page rendering speed. It is recommended to use it selectively rather than abusively. - Maintainability:Too much replacement logic within templates can reduce the readability and maintainability of templates.If keyword replacement is a long-term and widespread need, the priority should be to perform global replacement in the content management area of the AnQiCMS backend, or modify the original data source.
- Exact match:
replaceThe filter is an exact string match. It does not use regular expressions and does not understand the context of words.Therefore, for fuzzy matching or context-aware replacement, it may be necessary to combine other AnQiCMS features or carry out secondary development.
In summary, AnQiCMS providesreplaceThe filter is a powerful and convenient tool for string operations at the template level, allowing content operation personnel to control content display more flexibly and achieve precise adjustments without touching the database.Mastering this skill will greatly enhance the efficiency of content updating and maintenance.
Frequently Asked Questions (FAQ)
Q1:replaceWhat is the difference between the filter and the 'Full Site Content Replacement' feature of AnQiCMS backend?
A1: replaceThe filter is a feature provided by the AnQiCMS template engine, used to perform local replacement on the output text string of a certain tag during the rendering of the front-end page.It does not change the original content data in the database. The "full-site content replacement" feature of the backend directly modifies the content stored in the database, affecting all pages that call this content, and supports more complex replacement rules such as regular expressions, which is designed for large-scale content adjustments.The choice of method depends on whether your need is for temporary, local display adjustments, or persistent, global content data modifications.
Q2: Can I usereplaceCan regular expressions be used for more complex replacements in the filter?
A2:Do not. AnQi