In the process of operating a website, we often encounter scenarios where we need to flexibly adjust specific text on the page.Sometimes it is to correct typographical errors, sometimes it is to unify brand terms, or just for A/B testing.AnQiCMS (AnQiCMS) is an efficient content management system that fully considers these detailed requirements and provides simple yet powerful tools to help us achieve these goals.
Today, let's talk about how to easily replace a specific keyword with another keyword in a string and display it on your website page in AnQiCMS.
Understanding the core tool for string replacement:replaceFilter
In the AnQiCMS template system, we have a namedreplaceThe powerful filter.This filter can help us quickly and easily implement string replacement operations.It is like a 'text magic scissors' that can accurately find the word you want to replace and change it to new content.
UsereplaceFilter is very intuitive. Its basic syntax is:
{{ 你的字符串 | replace:"旧词,新词" }}
Here are the你的字符串It can be any text content you want to process, such as article titles, descriptions, or even custom field values.旧词is the keyword you want to be replaced with,新词English comma is used between the two.,.
Provide a simple example, if you have a string like "Welcome to AnQi CMS" and you want to replace "AnQi" with "AnQi", the code would look like this:
{{"欢迎使用安企CMS"|replace:"安企,AnQi"}}
On the page, it will display as 'Welcome to AnQiCMS'. Isn't it very convenient?
Useful scenarios and code examples
Understood the basic usage, let's take a look at how it is used in actual website operations,replaceWhat problems can the filter help us solve:
1. Replace specific words in the article title or description
Assuming you have an article on your website about "Free Website Building
First, you need to use AnQiCMS'sarchiveListorarchiveDetailget the article data by tags, thenreplaceapply the filter to the title or description field:
{# 假设这里获取到了文章列表中的每一篇文章 item #}
{% archiveList archives with type="list" limit="5" %}
{% for item in archives %}
<h3>
{{ item.Title | replace:"免费建站,高效建站" }}
</h3>
<p>
{{ item.Description | replace:"免费建站,高效建站" }}
</p>
{% endfor %}
{% endarchiveList %}
So, even if the background content is not modified, the article title and description displayed on the front end will be automatically updated to "Efficient Website Building".
2. Replace content containing HTML
When the content you need to replace is the article body (usually containing HTML tags), special attention is required. AnQiCMS defaults to HTML escaping for template output, which means that like<p>This label may be displayed as<p>. To ensure that the replaced HTML content is rendered correctly, we need to usesafeFilter.
safeThe filter will tell AnQiCMS that this part of the content is safe and does not need to be escaped; it can be output directly as HTML.
{# 假设获取到了一篇文章详情的Content字段 #}
{% archiveDetail articleContent with name="Content" %}
<div class="article-body">
{{ articleContent | replace:"旧内容,新内容" | safe }}
</div>
{% endarchiveDetail %}
Remember to usesafeThe filter means you need to ensure that the replaced content is reliable and harmless to prevent potential security risks (such as XSS attacks).
3. Combine variable assignment for multi-step operations
In more complex scenarios, you may need to perform multi-step operations, such as replacing a word first, then truncating the result, or storing the result for subsequent use in multiple places.setTags come into play.setTags allow you to define a temporary variable in the template.
{% set original_text = "这是一篇关于免费建站系统AnQiCMS的文章,AnQiCMS是您的好选择。" %}
{% set replaced_text = original_text | replace:"免费建站,高效建站" %}
{% set final_output = replaced_text | truncatechars:30 %} {# 再截取前30个字符 #}
<p>{{ final_output }}</p>
This code will first replace 'Free Website' with 'Efficient Website', then truncate the result to 30 characters (including ellipsis), and finally display the truncated text on the page.
注意事项 for using replacement function
When usingreplaceA few points to note when using the filter:
- Exact match:
replaceThe filter performs exact string replacement. This means it does not recognize regular expressions or perform fuzzy matching. The one you provided旧词The content must be identical to the target string in order to be replaced. - Empty string replacementIf
旧词The 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 original string and after each UTF-8 character sequence.新词.新词Empty, it will be removed from the string旧词. safe[en]The use of filters: When the content you replace involves HTML structure, be sure to usesafeFilter. But make sure that the source and target of replacement are both safe to avoid introducing malicious code.- Front-end and back-end differentiation:AnQiCMS的后台也提供了“全站内容替换”的功能(在“内容管理”下的“文档关键词替换”中),那是一个对数据库内容进行批量修改的强大工具。我们这里讨论的
replaceThe filter isFront-end template levelPerform real-time display replacement without modifying the original content in your database. Both methods have their own focuses, please choose according to your actual needs.
Summary
AnQiCMSreplaceThe filter is a simple yet powerful template tool that allows you to flexibly control the display of text content on web pages without modifying the original background data.Whether for brand unification, SEO optimization, or temporary adjustment, mastering this skill can greatly enhance your operational efficiency.This article is intended to help you better utilize AnQiCMS and create a more flexible and expressive website!
Common Questions (FAQ)
1.replaceFilter will modify the original article content on my website backend?No.replaceFilter onlyat the time of frontend page renderingPerform a string replacement operation, it will not make any modifications to the original content in your website database.If you need to batch modify the content in the database, you can go to the "Content Management" module of AnQiCMS backend and find the "Document Keyword Replacement" feature.
2. If I have a lot of words to replace, or the replacement rules are very complex,replaceFilter still applicable?
replaceThe filter is suitable for relatively simple, precise string replacements. If your replacement requirements involve a large number of different keyword pairs, or need to perform pattern matching replacements based on complex logic such as regular expressions, then using a singlereplaceThe filter may become cumbersome.In this case, you may need to consider processing some complex replacement logic through the "Document Keyword Replacement" feature before content publishing, or combine it with other custom development methods.
3.replaceThe filter can be used to modify the image linksrcor the image descriptionalt?English. As long as the image link or image description is output as a string variable in the template, you can apply itreplacefilters. For example, if you useitem.LogoEnter the image URL, you can replace it like this:.
<img src="{{ item.Logo | replace:"http://旧域名,https://新域名" }}" alt="{{ item.Title | replace:"旧描述词,新描述词" }}">
Make sure the format of the old and new URLs is correct when replacing the URL.