In AnQiCMS template development, flexibly using various tags and filters is the key to achieving content customization. When we need to define a variable in the template and further process its content, such as string replacement, a common problem is:replaceFilter whether it can act on the ones passing throughsetWhat about the variables defined by the tag?

The answer is affirmative.AnQiCMS template is based on the Iris framework written in Go language, its template engine syntax is similar to Django, and it provides powerful variable definition and data processing capabilities.setTags andreplaceFilters are two of the important components, which can work well together.

AnQiCMS template variable definition and content processing

We often use in AnQiCMS template{% set variable_name = "变量值" %}Such syntax is used to define a local variable.This variable can store various data types, but the most common is still a string.For example, we may need to define a temporary page title, a dynamic prompt, or a piece of text that requires uniform processing.

The filter (Filters) is a tool for processing specific values of these variables. They connect|symbols with variables, for example{{ variable | filter_name }}.replaceFilter is one of them, its core function is as the name suggests, used to find and replace specific substrings in strings. Its basic usage is{{ 字符串 | replace:"旧内容,新内容" }}It will replace all occurrences of 'old content' with 'new content' in the string and return the new string.

replaceFilter is related tosetCombination usage of variables

SincesetLabels can define string variables, andreplaceFilters are specifically used for strings, so combining them is a piece of cake.

we can use them first,setDefine a variable, store the original string we want to process. Then, at the place where we need to replace this string,replaceFilter applied to this defined variable.

Let's understand the practicality of this combination through some specific examples:

Assuming we want to display a company slogan on the page, but sometimes we need to fine-tune keywords within it based on different contexts.

{# 1. 使用 set 标签定义一个包含原始文本的变量 #}
{% set page_tagline = "我们致力于提供高效、可定制、易扩展的内容管理解决方案,AnQiCMS是您的**选择。" %}

{# 2. 直接对 set 变量使用 replace 过滤器 #}
<p>原始标语:{{ page_tagline }}</p>
<p>替换“高效”为“卓越”:{{ page_tagline|replace:"高效,卓越" }}</p>

{# 3. 链式调用多个 replace 过滤器,进行多重替换 #}
<p>替换“可定制”为“灵活”,“易扩展”为“可伸缩”:{{ page_tagline|replace:"可定制,灵活"|replace:"易扩展,可伸缩" }}</p>

{# 4. 结合其他 set 变量的值进行替换 #}
{% set email_template = "您好,欢迎使用我们的服务,请访问AnQiCMS官网获取更多信息。" %}
{% set official_site = "www.anqicms.com" %}

<p>更新邮件模板中的网站信息:{{ email_template|replace:"AnQiCMS官网",official_site }}</p>

It can be seen from the above examples,setVariables provide us with a temporary storage space, andreplaceThe filter provides the ability to modify the string content in this storage space. This combination makes the text processing in the template very flexible and dynamic.

Consideration of practical application scenarios

This combination has many applications in the actual website operation, such as:

  • Dynamic content adjustmentWhen some fixed copy or prompts on the website need to be slightly adjusted according to different festivals, promotional activities, or user statuses, it is not necessary to modify a large amount of static text, just by adjusting through the template level.setDefine basic text, thenreplaceperform local replacement.
  • Brand information unifiedIf the company name, brand term, or product name needs to be unified or variant in a specific context, this method can achieve it efficiently.
  • Sensitive word filtering or substitutionAlthough AnQiCMS has a sensitive word filtering feature, it can also be replaced with lightweight replacements at the template level under some special display requirements, to avoid exposing the content directly.
  • SEO Optimization Assistance:In certain scenarios, in order to cater to the different keyword matching strategies of search engines, it is possible to replace keyword variants in non-core content on the page.

Precautions

  • Data type match:replaceThe filter applies only to string data.If you try to apply it to non-string types such as numbers, booleans, or arrays, it may cause the filter not to work as expected or return unexpected results.replaceUsed before a variable, make sure the variable content is indeed a string or can be implicitly converted to a string.
  • to chain calls.: Filters can be chained together, as shown in the example.variable|filter1|filter2The output of the previous filter will be the input of the next filter.
  • The original string remains unchanged.:replaceThe filter (like most filters) does not change the original variable.page_taglineThe value of itself. It always returns a new, processed string. If you need to retain the replaced result, you should reassign it to another variable (such as{% set processed_text = dynamic_text|replace:"最棒的,顶级的" %}).

In summary, the AnQiCMS template system provides powerful flexibility,setTags andreplaceThe combination of filters is a manifestation of this flexibility.It allows developers and operations personnel to better and efficiently control the display of page content, thereby enhancing user experience and website maintenance efficiency.


Common Questions (FAQ)

  1. Q:replaceThe filter can not only act onsetvariables, but can also act on other types of data? Answer:Of course you can.replaceFilter can be applied to any template expression that produces a string result. This includes direct string literals (such as{{ "hello world"|replace:"world,AnQiCMS" }})、values fetched from the database (such as{{ archive.Title|replace:"文章,内容" }})、even other filter processing results. As long as the final input is a string type,replaceThe filter will work normally.

  2. **Question:** Ifsetthe variable stores a number, can I still use itreplace