In the daily operation of AnQiCMS, we often need to make flexible adjustments to the website content, especially to core elements such as the website name.In order to meet the dynamic marketing needs or to optimize search engine optimization (SEO) more accurately, sometimes we hope to replace specific keywords in the website name dynamically on the front-end page without directly modifying the background configuration.This is provided by the AnQiCMS template engine at this timereplaceThe filter has become a very powerful tool.

UnderstandingreplaceFilter

The AnQiCMS template engine syntax is similar to Django templates and provides a rich set of filters to process variables.replaceA filter, as the name implies, is used to search for a specified old keyword in a string and replace it with a new keyword.

Its basic usage method is very intuitive:

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

HereobjIt is the string variable you need to operate on,"旧词,新词"Then defined the replacement rule, the old keyword and the new keyword are separated by English comma,Separated.

Let's take a simple example, if we have a string“欢迎使用安企CMS”, want to“安企”with“AnQi”This can be written as:

{{ "欢迎使用安企CMS"|replace:"安企,AnQi" }}

After the page is rendered, the output result will be“欢迎使用AnQiCMS”It is noteworthy that if旧词is empty, it will match at the beginning of the string and after each UTF-8 character sequence; if新词is empty, it will be removed directly旧词.

Apply in website namereplaceFilter

In AnQiCMS, the website name is usually obtained throughsystemTag retrieval, for example in the page<title>Tag, we may use{% tdk with name="Title" siteName=true %}To display the page title, wheresiteName=trueIt will automatically append the website name as a suffix to the title. But if we need to replace keywords in the website name itself, we usually get the variable of the website name first, and then applyreplacefilter.

We can use{% system with name="SiteName" %}Label to retrieve the website name defined in the background "Global Settings". For easy management and readability, we usually assign it to a variable:

{% set originalSiteName = {% system with name="SiteName" %} %}

Assuming our website name is“安企CMS内容管理系统”. Now, we hope to implement during certain specific marketing activities, to“内容管理系统”Replace“企业级解决方案”. We can do it like this:

{% set originalSiteName = {% system with name="SiteName" %} %}
{% set displayedSiteName = originalSiteName|replace:"内容管理系统,企业级解决方案" %}

<title>{{ displayedSiteName }}</title>
<h1>{{ displayedSiteName }}</h1>

In this way, all the uses of the website,displayedSiteNameThe place will display the replaced name, and the configuration of the website name in the background will remain unchanged.

**Practice and Application Scenarios

replaceThe filter has a high degree of flexibility in keyword replacement for website names, and the following are some common practices and application scenarios:

  1. Promotional activities and festival limited editionDuring holidays or promotional activities, we often need to add some time-sensitive prefixes or suffixes to the website name to attract users. For example, will“您的品牌官网”temporarily display as“双十一 | 您的品牌官网”or“您的品牌官网 - 狂欢不止”.

    {% set originalSiteName = {% system with name="SiteName" %} %}
    {% set campaignSiteName = originalSiteName|replace:"官网","官网 - 双十一大促" %}
    <title>{{ campaignSiteName }}</title>
    
  2. SEO OptimizationWithout modifying the core website name, dynamically inserting or replacing long-tail keywords in the website name according to different pages or target keywords can help improve the SEO performance of specific pages. For example, to“AnQiCMS”Replace“AnQiCMS - 企业级内容管理平台”Please note that such operations should be carried out cautiously to avoid overloading keywords and being penalized by search engine algorithms.

  3. Personalized display under multi-site managementIf your AnQiCMS has deployed multiple sites, each site may focus on different content and audience.Although each site has an independent website name, but sometimes you may want to make a unified dynamic adjustment to the keywords of a site name in a specific public template.Passing cooperationsystemlabel'ssiteIdParameters to obtain the website name of a specific site and replace it can achieve more refined multi-site content control.

  4. CombineifConditional replacement through statements.Most of the time, the replacement operation is not global but needs to take effect under specific conditions.For example, only replace keywords on the homepage, or on a category page.At this point, we canreplacethe filter meetsifUsing logical judgment tags together.

    {% set originalSiteName = {% system with name="SiteName" %} %}
    {% if currentPage == "home" %} {# 假设 currentPage 是一个表示当前页面的变量 #}
        {% set finalSiteName = originalSiteName|replace:"核心词,新的核心词" %}
    {% else %}
        {% set finalSiteName = originalSiteName %}
    {% endif %}
    <title>{{ finalSiteName }}</title>
    
  5. Chain Filter Application replaceThe filter can be chained with other filters to achieve more complex string processing.For example, first convert the website name to lowercase, then perform keyword replacement, and finally capitalize the first letter.

    {% set originalSiteName = {% system with name="SiteName" %} %}
    {% set processedSiteName = originalSiteName|lower|replace:"cms","content management system"|capfirst %}
    <title>{{ processedSiteName }}</title>
    

    This showcases the power of the AnQiCMS template engine, which can smoothly connect multiple text processing steps.

###