In the daily operation of AnQiCMS, we often need to make flexible adjustments to the website content, especially on core elements such as the website name.Whether it is to meet the ever-changing marketing needs, or to carry out more precise search engine optimization (SEO), sometimes we hope to dynamically replace specific keywords in the website name on the front-end page without directly modifying the background configuration.replaceThe filter becomes a very powerful tool.

UnderstandingreplaceFilter

AnQiCMS's template engine syntax is similar to Django templates, providing a rich set of filters to process variables.replaceThe filter, as the name implies, is used to find and replace the specified old keyword with a new keyword in a string.

Its basic usage is very intuitive:

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

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

Let's take a simple example, if we have a string“欢迎使用安企CMS”and we want to“安企”Replace it with“AnQi”You can write it like this:

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

After the page is rendered, the output result will be“欢迎使用AnQiCMS”It is worth noting 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 website namereplaceFilter

In AnQiCMS, the website name is usuallysystemTag to get, such as in the page<title>Tag, we may use{% tdk with name="Title" siteName=true %}to display the page title, wheresiteName=trueThe website name will automatically be appended to the title. But if we need to replace keywords directly in the website name itself, we usually first obtain the variable of the website name and then apply it.replaceFilter.

We can through{% system with name="SiteName" %}Label to get the website name defined in the "Global Settings" backend. We usually assign it to a variable for easy management and reading:

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

Suppose our website name is“安企CMS内容管理系统”。Now, we hope to do this during certain specific marketing campaigns,“内容管理系统”with“企业级解决方案”。We can operate like this:

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

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

In this way, all the websites that usedisplayedSiteNameEnglish

**Practice and Application Scenarios

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

  1. Promotional events and limited-time offersOn holidays or promotional events, we often need to add time-sensitive prefixes or suffixes to the website name to attract users. For example, the“您的品牌官网”is temporarily displayed 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 based on different pages or target keywords can help improve the SEO performance of specific pages. For example, the“AnQiCMS”with“AnQiCMS - 企业级内容管理平台”Please note that such operations should be carried out with caution to avoid keyword stacking excessively, which may result in penalties from search engine algorithms.

  3. Personalized display under multi-site managementIf your AnQiCMS has deployed multiple sites, each site may have a focus on content and audience.Although each site has an independent website name, you may sometimes want to dynamically adjust the keywords of a site name uniformly in a specific public template.systemTagssiteIdParameters can be used to obtain the website name of a specific site and then replace it, which can achieve more refined control over multi-site content.

  4. CombineifConditional replacement can be achieved with statements.In many cases, the replacement operation is not global but only takes effect under specific conditions.For example, replace keywords only on the homepage, or on a category page.replaceFilter is related toifUse logical judgment tags in combination.

    {% 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 replaceFilter 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 demonstrates the powerful capabilities of the AnQiCMS template engine, which can seamlessly connect multiple text processing steps.

###