In AnQiCMS template development, flexibly using various tags and filters is the key to achieving content customization display. When we need to define a variable in the template and further process its content, such as string replacement, a common problem is: replaceCan the filter act on throughsetWhat about the variables defined by the tag?
The answer is affirmative. AnQiCMS template is based on the Go language's Iris framework, its template engine syntax is similar to Django, and it provides powerful variable definition and data processing capabilities.setTags andreplaceThe filter is one of the two important components, which can work well together.
Variable definition and content processing in AnQiCMS template
We often use in AnQiCMS template{% set variable_name = "变量值" %}This 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 needs to be uniformly processed.
And Filters are tools that process the values of these variables. They pass through|symbols connected to variables, for example{{ variable | filter_name }}.replaceThe filter is one of them, its core function is as the name suggests, it is 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" and return the new string.
replacethe filter meetssetCombining variables in use
SincesetTags can define string variables whilereplaceFilters are specifically used for strings, so combining them makes it quite natural.
we can use firstsetLabel defines a variable to store the original string we want to process. Then, when we need to perform a replacement operation on this string, we directlyreplaceApply the filter 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 the keywords in it according to 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 thatsetVariables provide 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 operation of actual websites, for example:
- Dynamic content adjustmentWhen some fixed text or prompts on the website need to be adjusted according to different holidays, promotional activities, or user statuses, it is not necessary to modify a large amount of static text, just through the template level by
setDefine basic text, thenreplacePerform local replacement. - Unified brand informationIf the company name, brand term, or the name of a product needs to be unified or variant in a specific context, this method can be implemented efficiently.
- Sensitive word filtering or substitution: Although the AnQiCMS backend has a sensitive word filtering function, it can also be replaced lightly at the template level under some special display needs to avoid the direct exposure of content.
- SEO Optimization AssistanceIn certain scenarios, in order to cater to the different keyword matching strategies of search engines, keyword variant replacements can be made for non-core content on the page.
Points to note
- Data type matches:
replaceThe filter is only applicable to string type data. If you try to apply it to non-string types such as numbers, booleans, or arrays, it may cause the filter to not work as expected or return unexpected results. When you attempt toreplaceBefore applying to the variable, make sure the variable content is indeed a string or can be implicitly converted to a string. - chaining call: Filters can be chained together, as shown in the example.
variable|filter1|filter2The output of the previous filter will be used as the input for the next filter. - The original string remains unchanged:
replaceFilters (like most filters) do not change the original variable.page_taglineThe value 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,setwith the tag andreplaceThe combination of filters is a manifestation of this flexibility. It allows developers and operations personnel to control the display of page content more finely and efficiently, thus enhancing user experience and website maintenance efficiency.
Frequently Asked Questions (FAQ)
Question:
replaceThe filter can also be applied tosetVariables, but can it affect other types of data? Answer:Of course you can.replaceThe filter can be applied to any template expression that produces a string result. This includes direct string literals (such as{{ "hello world"|replace:"world,AnQiCMS" }}), and field values obtained from the database (such as{{ archive.Title|replace:"文章,内容" }}Even the string results after other filter processing. As long as the final input is a string type,replacethe filter can work normally.**Question: If
setThe variable stores a number, can I still use it toreplace