In AnQiCMS template development, we often encounter the need to process certain data and then use the processed results 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 the Django 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.
Understand the template variables of AnQiCMS.
In the AnQiCMS template, we mainly use two methods to process and display data:
- double curly braces
{{ 变量 }}: This is the most common way to output variable content. For example,{{ archive.Title }}It will directly display the title of the current document. - Percent sign curly braces
{% 标签 %}: This is used to perform logical operations, such as conditional judgments ({% if %}), Loop traversal ({% for %}), and we will focus on variable assignment today.
When we want to create a temporary variable in the template to store a value for subsequent reuse,{% set %}Labels 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 can be accessed and used within the subsequent template range. AnQiCMS also provides{% with %}Tags, they are usually used to define multiple variables or passincludeTags pass a group of variables, their 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 usage of filters
String replacement is a very common requirement in content processing in AnQiCMS.replaceThe filter can help us easily achieve this goal. Its basic syntax is:
{{ obj|replace:"旧关键词,新关键词" }}
This filter will handleobjReplace all instances of the 'old keyword' with the 'new keyword' then output the result directly.
For example:
{% set content = "安企CMS是一个功能强大的CMS,安企CMS致力于企业网站。" %}
<p>替换后的内容:{{ content|replace:"安企CMS,AnQiCMS" }}</p>
This code will directly output: ”Replaced content: 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 temporary variables and how to use themreplaceFilter. Combining them is the key to solving our problem. We can use{% set %}tags to receivereplaceThe string processed by the filter is stored in a new temporary variable.
Thus, the intermediate result of the replacement operation is "captured", and we can refer to this new variable like any other variable at any position in the template.
Here 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:
- We first defined a variable named
original_sloganto store the original text. - Next, we use
{% set modified_slogan = original_slogan|replace:"安企CMS,AnQiCMS" %}This line of code, willoriginal_sloganpass the variable toreplaceThe filter is processed. After the filter performs the replacement operation, the result is not output directly, but is assigned to a new temporary variable.modified_sloganThis new temporary variable. - From this point on, whether it is in any position on the page, we can go through
{{ modified_slogan }}Refer to this text that has already been replaced once, which avoids repeated replacement calculations and makes the template code more readable and maintainable. - We can even base on
modified_sloganVariables, replace twice and store.final_sloganIt shows the reusability of temporary variables.
In this way, we not only make the template logic clearer, but also provide a flexible foundation for subsequent more complex string processing or content operation strategies.For example, you may need to display different brand names based on the user's region, or show optimized content with specific keywords on different pages. Storing the replacement results can make these operations simple and efficient.
Summary
In the AnQiCMS template,巧妙地利用{% set %}with the tag andreplaceCombine filters, which can effectively manage and reuse data.This pattern makes template code more modular, easier to read and maintain, and provides strong support for implementing more refined content operation strategies.Master this skill, and it will make your AnQiCMS template development more proficient.
Frequently Asked Questions (FAQ)
Question:{% set %}and{% with %}What are the differences between the tags? Which one should I choose?
Answer:{% set %}It is mainly used to define and assign an independent variable in a template, whose scope is usually the current template and any that contain (include) or inherit (extendsIts sub-template. It is suitable for simple, single variable assignment scenarios. And{% with %}The tag is 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 within the block), thenwithblock) then{% set %}It is the more suitable choice. If you just want to conveniently use a group of variables within a local scope, then{% with %}it would be more suitable.
Question: Canreplacemultiple replacement operations be applied in a chain to the filter?
Answer: Absolutely. The AnQiCMS template engine supports the chaining of filters. This means that you can apply multiplereplaceFilter them together, replace the string sequentially. For example:{% set final_text = original_text|replace:"旧词1,新词1"|replace:"旧词2,新词2" %}This method is very concise and suitable for completing multiple replacements in one go.If intermediate results need to be referenced multiple times, you can also consider storing the results of each step in different temporary variables.
Question: IfreplaceWhat happens if the 'old keyword' in the filter does not exist in the original string?
Answer: IfreplaceThe specified "old keyword" was 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 makesreplaceThe filter is very robust and secure when handling dynamic content.