When operating website content on AnQiCMS, we often encounter situations where we need to make minor adjustments to the website text content.Sometimes it is the replacement of brand terms, sometimes it is the correction of misspellings, or it may be 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.

Understanding the need for keyword replacement in templates

AnQiCMS background usually provides the "Full Site Content Replacement" feature, this feature is powerful and convenient, and can batch modify the 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 and dynamic keyword replacement for a specific string in the template, or for text output by a certain tag.

  • Local fine-tuning:A general template snippet needs to display different keywords for specific pages without modifying the original data.
  • Formatted output:The content of the label output 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 for keyword replacement becomes an ideal choice that does not affect the original data and can respond quickly to the needs.

AnQiCMS solution:replaceFilter

AnQiCMS's template engine supports rich filters (Filter), among which there is a special tool for string replacement -replaceFilter. Its core function is to replace the specified old keywords in the text string with new keywords and return the new string after replacement.

replaceThe basic usage of the filter is as follows:

{{ obj|replace:"旧关键词,新关键词" }}

Here are theobjRepresents the text string or variable that you want to perform the replacement operation on.replaceThe filter accepts a string parameter that consists of two parts, separated by an English comma,分隔:第一部分是需要被替换的旧关键词,第二部分是用于替换的新关键词.

It is worth noting that if旧关键词为空,它将在字符串的开头和每个UTF-8序列之后进行匹配;如果新关键词Empty, it is equivalent to removal旧关键词.

Actual operation example

Let us understand through several specific examplesreplaceThe application of filter in AnQiCMS template.

1. Basic text replacement

Assuming there is a piece of hard-coded text in the template, you need to replace one of the words:

<p>欢迎使用安企CMS,您的内容管理专家。</p>

Now we want to replace “AnQi” with “AnQi”, we can do it like this:

<p>{{ "欢迎使用安企CMS,您的内容管理专家。"|replace:"安企,AnQi" }}</p>

The output will be:

欢迎使用AnQiCMS,您的内容管理专家。

2. Replace keywords in dynamic content

More common scenarios are to replace keywords in dynamic content output by AnQiCMS tags. For example, we have an article titlearchive.Title,it's current value is “AnQiCMS template creation guide”,but we hope to replace “AnQiCMS” with “安企CMS”:

<h1>{{ archive.Title|replace:"AnQiCMS,安企CMS" }}</h1>

Ifarchive.TitleIs "AnQiCMS Template Creation Guide", then the page will display:

<h1>安企CMS模板制作指南</h1>

3. Delete a specific keyword

When you need to remove a specific keyword from a piece of text, you can新关键词Leave blank. For example, from the article descriptionarchive.DescriptionRemove the word "exclusive" from within:

<p>这篇文章提供了独家的AnQiCMS模板开发技巧。</p>

UsereplaceFilter deletion:

<p>{{ archive.Description|replace:"独家," }}</p>

Output result:

<p>这篇文章提供了的AnQiCMS模板开发技巧。</p>

4. Replace multiple keywords consecutively

If you need to replace the same text 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 'Copyright' with 'All rights reserved'.Please note that the order of replacement may affect the final result because the result of the previous replacement is used as the input for the next replacement.

Use cases and precautions.

Applicable scenarios:

  • Text fine-tuning:Quickly correct minor text errors displayed on the page.
  • Brand/Terminology Consistency:Use new brand names or terms in the old template.
  • Content Purification:Remove unnecessary or redundant words from specific template outputs.
  • SEO Optimization Assistant:For specific pages, dynamically adjust keyword density (use with caution).

Caution:

  • Performance considerations:AlthoughreplaceThe filter efficiency is high, 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 excessively.
  • Maintainability:Too many template replacement logic will reduce the readability and maintenance cost of the template.If keyword replacement is a long-term and widespread need, it is recommended 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 match based on the string.It does not use regular expressions and cannot understand the context of words.Therefore, for need of fuzzy matching or context-aware replacement, it may be necessary to combine other AnQiCMS features or carry out secondary development.

In summary, what is the difference between the filter and the "Full Site Content Replacement" function provided by AnQiCMS?replaceFilter is a powerful and convenient tool for string operations at the template level, allowing website content operators to control content display more flexibly and achieve precise adjustments without touching the database.Master this skill, and it will greatly enhance the efficiency of content updating and maintenance.


Common Questions (FAQ)

Q1:replaceWhat is the difference between the filter and the "Full Site Content Replacement" function of AnQiCMS backend? A1: replaceThe filter is a feature provided by AnQiCMS template engine, which is used to replace the text string output by a certain tag locally during the rendering of the front-end page.It will not change the original content data in the database.The "Full Site Content Replacement" feature 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. It is designed for large-scale content adjustments.The choice of method depends on whether your need is for temporary, local display adjustments or for permanent, global content data modifications.

Q2: Can IreplaceDo you use regular expressions for more complex replacements in the filter? A2:Cannot. AnQi