In AnQiCMS's flexible template system, dynamic content processing is an indispensable part of daily operations.Sometimes, we may need to replace a specific keyword in a string when displaying content, such as displaying a unified brand name in a consistent manner, or adjusting the presentation of certain text without modifying the original data.replaceThe filter is our powerful assistant.
UnderstandingreplaceFilter
replaceThe filter is a practical tool in AnQiCMS template language, which allows you to find a 'old keyword' in a string and replace it with a 'new keyword'.This process is completely performed during template rendering, and does not touch the original data in the database, thus providing you with great flexibility to make various adjustments to the display layer of content without affecting the content source.
Its basic usage is very intuitive, you can take the string you need to process as input, then through the pipe symbol|InvokereplaceFilter, and specify the "old keyword" and "new keyword" in a comma-separated manner.
For example, if you have an array named文章标题The variable includes "Welcome to AnQi CMS
{{ 文章标题|replace:"安企CMS,AnQiCMS" }}
After such processing, the content displayed on the final page will be “Welcome to AnQiCMS”.
replaceFilter has some special behaviors that need to be noted.If the old keyword is set to empty, it will match at the beginning of the string and after each UTF-8 character sequence, which is usually used to insert content between each character.If the new keyword is set to empty, then the old keyword will be removed.{{ "安企CMS"|replace:"安企," }}The result will be “CMS”.
Actual application scenarios and examples.
In daily website operations,replaceThe filter can be applied to various scenarios:
Unified brand or term name:When the company name, product name, or some industry terminology varies in different periods or needs to be unified in a specific style on a particular page, this filter is particularly important. 假设你的文章详情页中有一段描述提到了“安企内容管理系统”,你希望统一显示为“AnQiCMS”。
<p>{{ archive.Description|replace:"安企内容管理系统,AnQiCMS" }}</p>Adjust the display of the URL or link:Sometimes, the URL you get from the backend might contain the full
http://orhttps://Prefix, while you only want to display the domain part on the page.<a href="{{ item.Link }}">{{ item.Link|replace:"https://,"|replace:"http://," }}</a>Here, we used it twice consecutively.
replaceFilter, it removed first.https://And then removed again.http://To ensure that any protocol can be displayed uniformly.Format the separators in the content:If the content output of a field is a date string, for example
2023-10-26,while you want to display as2023/10/26,replaceThe filter can help you quickly convert.<span>发布日期:{{ stampToDate(item.CreatedTime, "2006-01-02")|replace:"-","/" }}</span>This example combines
stampToDateThe filter first formats the timestamp into a date string, thenreplaceFilter and replace the separator in it.Simple content correction on the display level:Although it is recommended to correct the original content in the background, for some minor typos or temporary content adjustments that do not affect the core data,
replacethe filter can also provide immediate display corrections.<h1>{{ archive.Title|replace:"错别字,正确词" }}</h1>
Combined with other filters.
replaceFilter can be combined with other filters to achieve more powerful functionality.
Process strings containing HTML:When you need to replace content containing HTML tags (such as the content output by a rich text editor), it is very important to use
|safeFilter.AnQiCMSThe template engine defaults to escaping the output HTML content to prevent XSS attacks. If you directly applyreplacewithout adding|safeThe browser may display the HTML tags in your replacement result as plain text.<div class="article-content"> {{ archive.Content|replace:"旧的HTML内容,新的HTML内容"|safe }} </div>To perform case-insensitive replacement:If the case of the old keyword is uncertain, you can first use
|loweror|upperThe filter converts the string to uppercase or lowercase and then replaces it.{{ item.Title|lower|replace:"cms,内容管理系统" }}So, whether the original title is “CMS” or “cms”, it will be replaced with “Content Management System”.
Important Notice
UsereplaceThe filter needs to specify its scope clearly. It merely modifies the output string during template rendering.on the display level.It will not change the original content stored in the database.如果你需要进行大规模、永久性的内容修改,例如全站的品牌词更新,那么更应该使用AnQiCMS后台提供的“全站内容替换”功能(可在“内容管理”模块中找到),那是一个后端执行的批量操作,直接修改数据库中的内容,对SEO也更友好。
In addition, althoughreplaceThe filter is very convenient, but it also needs to consider the potential performance impact when handling very large strings or using it frequently in loops.Use moderately, combined with background features, it can help you better manage and display website content.
Common Questions (FAQ)
1.replaceCan the filter implement batch replacement of all site content?Cannot.replaceThe filter is provided by AnQiCMS template engineFront-end display layerThe tool, it temporarily modifies the display of the string only when the content is rendered to the page.To implement batch modification of all site content (i.e., directly changing the data in the database), you need to use the "Full Site Content Replacement" feature of the AnQiCMS backend.
2. I have used it in the template.replaceFilter, but the front-end page does not change, what could be the reason?This usually has several reasons. First, check if your template syntax is correct, including comma separators, quotes, and pipe characters|The use of.其次,confirm you have modified the template file that is currently in use on the website, as well as whether the AnQiCMS system cache has been cleared after the modification.Sometimes, browser cache may also cause you not to see the latest changes. Try clearing the browser cache or accessing in incognito mode.
3.replaceFilter does the support regular expressions for more complex replacements?AnQiCMS template'sreplaceFilter does not support regular expressions at the moment.It performs a simple string match and replacement.If you need to use regular expressions for advanced matching and replacement and want to modify the original data, you should consider using the "Document Keyword Replacement" feature of AnQiCMS backend, which supports batch replacement using regular expression rules.