In AnQiCMS template development, we often encounter situations where we need to process some data and then use the processed result for subsequent display or logical judgment.Especially when it comes to operations such as string replacement, if the result of the replacement can be stored in a temporary variable, it will undoubtedly greatly enhance the clarity and reusability of the template code.

AnQiCMS uses a syntax similar to Django's template engine, which provides a powerful and flexible mechanism for us to operate data in templates. Understanding how to define and use temporary variables to storereplaceThe result of the filter is a key step to improve template development efficiency.

Understanding the template variables of AnQiCMS

In AnQiCMS templates, we mainly handle and display data in two ways:

  1. Double curly braces{{ 变量 }}This is the most common way to output variable content. For example,{{ archive.Title }}This will directly display the title of the current document.
  2. Percent sign curly braces{% 标签 %}: This is used to perform logical operations, such as conditional judgments ({% if %}), Loop through ({% for %}),as well as the variable assignment we will focus on today.

When we want to create a temporary variable in the template to store a value for subsequent reuse,{% set %}Tags make it very convenient. Its basic usage is:

{% set my_variable = "Hello AnQiCMS" %}
<p>{{ my_variable }}</p>

Here,my_variableIt is a temporary variable that stores the string “Hello AnQiCMS”, and it can be accessed and used within the subsequent template scope. AnQiCMS also provides{% with %}Labels, which are typically used to define multiple variables or pass a group of variables at once.includeLabels pass a group of variables, whose scope is limited to.{% with %}and{% endwith %}. But for our current need to store filter results,{% set %}Tags are usually more direct and concise.

replaceBasic application of filters

String replacement is a very common requirement in content processing in AnQiCMS.replaceFilter helps us easily achieve this goal. Its basic syntax is:.

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

This filter will handleobjReplace all instances of the 'old keyword' in a string with 'new keyword' and then output the result directly.

For example:

{% set content = "安企CMS是一个功能强大的CMS,安企CMS致力于企业网站。" %}
<p>替换后的内容:{{ content|replace:"安企CMS,AnQiCMS" }}</p>

这段代码会直接输出:”替换后的内容:AnQiCMS is a powerful CMS, AnQiCMS is committed to corporate websites.

As you can see,replaceThe filter directly outputs the processed result to the page.But if we need to further process this replacement result, or use it in multiple places in the template, this direct output method is not flexible enough.

Define a temporary variable to storereplaceThe result of the filter

Now, we have learned how to define a temporary variable and how to use itreplaceFilter. Combining them is the key to solving our problem. We can use{% set %}a label to receivereplacethe string processed by the filter and store it in a new temporary variable.

So, the intermediate result of the replacement operation is "captured", and we can refer to this new variable at any location in the template as if it were any other variable.

Below is a specific example:

{% set original_slogan = "安企CMS,高效的内容管理系统,是您企业建站的理想选择。" %}

{# 使用 replace 过滤器将“安企CMS”替换为“AnQiCMS”,并将结果存储在 modified_slogan 变量中 #}
{% set modified_slogan = original_slogan|replace:"安企CMS,AnQiCMS" %}

<p>原始宣传语:{{ original_slogan }}</p>
<p>品牌更新后的宣传语:{{ modified_slogan }}</p>

{# 现在,我们可以复用 modified_slogan 进行其他操作,比如再次替换或者与其他文本拼接 #}
{% set final_slogan = modified_slogan|replace:"理想选择,**伙伴" %}
<p>最终宣传语:{{ final_slogan }}</p>

In this example:

  1. We first defined a variable namedoriginal_sloganto store the original text.
  2. Next, we use{% set modified_slogan = original_slogan|replace:"安企CMS,AnQiCMS" %}This line of code,original_sloganVariable passed toreplaceThe filter performs processing. After the filter executes the replacement operation, the result is not output directly but is assigned tomodified_sloganthis new temporary variable.
  3. Here, whether it is at any position on the page, we can{{ modified_slogan }}Come to refer to this text that has already been replaced once, avoiding repeated replacement calculations, and making the template code more readable and maintainable.
  4. We can even base onmodified_sloganVariable, perform secondary replacement and store.final_sloganIn this context, the reusability of temporary variables is demonstrated.

Through this method, we not only make the template logic clearer, but also provide a flexible foundation for more complex string processing or content operation strategies in the future.For example, you may need to display different brand names based on the user's region, or show content optimized with specific keywords on different pages. Storing the replacement results can make these operations simple and efficient.

Summary

In AnQiCMS templates, it cleverly utilizes{% set %}Tags andreplaceCombined with filters, it can effectively manage and reuse data.This pattern makes template code more modular, easier to read and maintain, and also provides strong support for implementing more refined content operation strategies.Master this skill, and it will make your AnQiCMS template development more intuitive.


Common Questions (FAQ)

Q:{% set %}and{% with %}What are the differences between labels? Which one should I choose?

Answer:{% set %}It is mainly used to define and assign an independent variable in the template, whose scope is usually the current template and any included (include) Or inherit (extends) Its child template. It is suitable for simple, single variable assignment scenarios.{% with %}Labels are used to define one or more variables within a specific code block, its scope is limited to{% with %}and{% endwith %}between. If you need to define a temporary variable to store the filter results and reuse it elsewhere in the template (not inside a block), thenwithblock){% set %}It is a more suitable choice. If you just want to use a group of variables conveniently within a local scope, then{% with %}would be more suitable.

Question: Canreplacemultiple replacement operations be applied to the filter chain?

答:完全可以。AnQiCMS 模板引擎支持过滤器的链式应用。这意味着你可以将多个 EnglishreplaceFilter them together, replace the strings in order. For example:{% set final_text = original_text|replace:"旧词1,新词1"|replace:"旧词2,新词2" %}This method is very concise and suitable for scenarios where multiple replacements need to be completed at once.If the intermediate results need to be referenced multiple times, consider storing the results of each step in different temporary variables as well.

Question: IfreplaceWhat happens if the 'old keyword' in the filter does not exist in the original string?

Answer: IfreplaceFilter specified "old keyword" not found in the original string.replaceThe filter will not perform any replacement operations and will return the original string directly. It will not raise an error, so you do not need to worry that the template will crash when the keyword does not exist. This feature makesreplaceFilter is very robust and secure when handling dynamic content.