How to use the `replace` filter to dynamically adjust the keyword density in page titles or `meta` descriptions?

Calendar 👁️ 86

In website content operation, page title (Title) and Meta description (Description) are key elements of search engine optimization (SEO).They not only directly affect the visibility and click-through rate of a website in the search engine results page (SERP), but are also the first window through which users convey the core content of the page.AnQiCMS provides a powerful template engine and rich built-in tags, among whichreplaceThe filter helps us flexibly dynamically adjust the keyword density of these key meta information to meet different optimization needs.

Understand the TDK management mechanism of Anqi CMS.

Before delving deeperreplaceBefore applying the filter, we first need to understand how Anqi CMS manages page titles, keywords (Keywords), and Meta descriptions (Description) (usually abbreviated as TDK). The system determines the final TDK content according to a certain priority when generating pages:

  1. Global TDK Settings:In the background "Home TDK Settings", you can set the default home title, keywords, and description for the entire website. This provides the entire site with basic SEO information.
  2. Content (document, category, single page) level TDK settings:When publishing documents, creating categories, or editing single pages, you can set SEO titles, keywords, and descriptions separately for each specific content.These settings have a higher priority than the global TDK and will override the global default values for the corresponding page.
  3. Automatically extract:If you do not manually fill in the Meta description for a piece of content, AnQi CMS will intelligently extract some text from the main content introduction as the description.

In the template, we can usetdkTag to get the TDK information of the current page. For example, get the page title:{% tdk with name="Title" %}Get Meta description:{% tdk with name="Description" %}These tags output the final TDK content after the system priority processing.

MasterreplaceThe core function of the filter

replaceThe filter is a very practical text processing tool in the Anqi CMS template engine, which allows us to replace a specific word or phrase in the output content with another.This provides us with infinite possibilities for dynamically adjusting the keyword density in TDK.

Its basic syntax is:{{ 目标字符串 | replace:"旧词,新词" }}.

UnderstandingreplaceSeveral key points of the filter are crucial:

  • Parameter separation: 旧词and新词must be separated by English commas.,It separates.
  • The old word is empty:If旧词Empty (i.e.,replace:",新词")replaceThe filter will insert at the beginning of the target string and after each UTF-8 character新词. This can be used to insert separators between each character.
  • The new word is empty:If新词Empty (i.e.,replace:"旧词,")replaceThe filter will remove all matches from the target string旧词. This is often used to remove unnecessary characters or phrases.
  • Case Sensitive:By default,replaceThe filter is case sensitive.

Let's look at a few simple examples to deepen our understanding:

{{ "欢迎使用安企CMS"|replace:"安企,anqi" }}
{# 输出结果:欢迎使用anqiCMS #}

{{ "欢迎使用安企CMS"|replace:",-" }}
{# 输出结果:-欢-迎-使-用-安-企-C-M-S- #}

{{ "安企CMS,专业的CMS"|replace:"专业的CMS," }}
{# 输出结果:安企CMS, #}

Through these examples, we can seereplaceThe flexibility of filters in string processing.

Dynamically adjusting the keyword density of page titles and Meta descriptions.

Now, we willreplaceThe filter is applied to the optimization practice of page titles and Meta descriptions.Our goal is to dynamically adjust the template level without modifying the original TDK settings on the backend.

1. Dynamically optimize the page title (Title)

Assuming we have a product page, the original SEO title is "AnQi CMS Corporate Website Solution".But we hope to emphasize the keyword "Free Website" in specific promotional activities, or adjust the order and frequency of keyword occurrence.

{# 1. 首先,获取当前页面经过系统处理后的最终标题 #}
{% tdk originalTitle with name="Title" %}

{# 2. 使用 replace 过滤器进行关键词替换或调整 #}
{# 示例1:将“解决方案”替换为“免费建站方案”,增加“免费建站”关键词密度 #}
{% set modifiedTitle = originalTitle|replace:"解决方案,免费建站方案" %}

{# 示例2:在标题末尾动态添加一个关键词,例如“【限时优惠】” #}
{% set modifiedTitle2 = originalTitle|add:"【限时优惠】" %} {# add过滤器用于拼接字符串 #}

{# 示例3:组合多个替换操作,例如先替换再添加 #}
{% set tempTitle = originalTitle|replace:"企业建站,网站建设" %}
{% set finalTitle = tempTitle|add:" - 安企CMS官网" %}

{# 3. 最后,将修改后的标题输出到 <title> 标签中 #}
<title>{{ finalTitle }}</title>

{# 也可以直接在 <title> 标签内进行操作,但通过 set 变量会更清晰和方便多步操作 #}
{# <title>{{ {% tdk with name="Title" %} | replace:"解决方案,免费建站方案" }}</title> #}

We used heresettag to define a temporary variablemodifiedTitleFirst obtaintdkThe original title output by the tag, and then performreplaceThe operation, and finally output to<title>In the tag. This ensures the clarity and maintainability of the template code.

2. Dynamically adjust the Meta description (Description)

Meta description can also be enabledreplaceThe filter can be dynamically adjusted to ensure it includes core keywords and attracts user clicks.

{# 1. 获取当前页面的Meta描述 #}
{% tdk originalDescription with name="Description" %}

{# 2. 使用 replace 过滤器调整描述内容 #}
{# 示例1:确保描述中包含某个重要的关键词,如果不存在则尝试插入,如果存在则可能替换其周边文字 #}
{% set modifiedDescription = originalDescription|replace:"高效建站,利用安企CMS高效免费建站" %}

{# 示例2:移除描述中不必要的短语,例如“本站提供” #}
{% set modifiedDescription2 = originalDescription|replace:"本站提供," %}

{# 3. 输出修改后的Meta描述 #}
<meta name="description" content="{{ modifiedDescription }}">

When adjusting Meta descriptions, be sure to pay attention to the fluency and readability of the description. Even to increase keyword density, user experience should not be compromised.

3. Dynamic adjustment for specific content models

replaceFilters can also be combined with specific content models (such as documentsarchiveDetail, ClassificationcategoryDetail, Single pagepageDetailThe data is adjusted dynamically in more detail. For example, you may want to adjust according to

Related articles

What to note when processing URL parameters with the AnQiCMS `replace` filter?

In AnQi CMS template development, the `replace` filter is a very flexible string processing tool that can help users quickly replace specific strings in text content.However, when its application scenarios involve handling URL (Uniform Resource Locator) parameters, we need to invest more thought and caution, as URL parameters have their own unique structure and encoding standards. ## Learn the basic functions of `replace` filter First, let's review the basic usage of the `replace` filter.

2025-11-08

Can the `replace` filter be used for the `set` variable defined in the AnQiCMS template?

In AnQiCMS template development, flexibly using various tags and filters is the key to achieving content customization display.When you need to define a variable in a template and process its content further, for example, string replacement, a common question is: can the `replace` filter be applied to variables defined through the `set` tag?The answer is affirmative.

2025-11-08

How to combine AnQiCMS `if` logical judgment tags to implement conditional keyword replacement?

In the flexible and powerful template system of AnQiCMS, achieving high content customization is the key to improving user experience and meeting operational needs.Among them, combining the `if` logical judgment tag for conditional keyword replacement can make our website content more intelligent and accurate. Whether it is to optimize SEO effects or to provide more attractive information in specific scenarios, this is a very practical skill.Let's explore how to use this feature in AnQiCMS to bring new vitality to the website content.

2025-11-08

Use the `replace` filter to replace keywords in the website name, what are some practices?

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 cope with the ever-changing 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.At this time, the `replace` filter provided by the AnQiCMS template engine has become a very powerful tool.

2025-11-08

How to avoid errors when using the AnQiCMS `replace` filter to replace strings containing special characters?

AnQiCMS as an efficient and flexible content management system, provides a wealth of features to help us manage and display content.In daily operations, we often use template tags and filters to process data, where the `replace` filter is a very practical tool that can help us easily replace specific content in strings.

2025-11-08

Does the filtered content take effect immediately after the AnQiCMS static cache is updated?

When using Anqi CMS to manage website content, we often encounter situations where the website front-end does not immediately display the latest changes after content updates, especially when it involves batch content operations such as "filter replacement", this problem becomes more prominent.About the filtered content, will it take effect immediately after the AnQiCMS static cache is updated?This question can be deeply understood from the operation mechanism of AnQi CMS.

2025-11-08

Can the `replace` filter be used to format the basic style of output such as phone numbers or email addresses?

In the daily operation of website content, we often need to ensure that the information presented on the page is both beautiful and standardized.Especially for critical contact information such as phone numbers and email addresses, maintaining a uniform display style can not only enhance the user experience but can also be to meet specific design or security requirements.When using Anqi CMS for template development and content display, many friends may wonder: Can the `replace` filter provided by Anqi CMS be flexibly used to format the output of these basic styles?Today, let's delve deeply into this issue. ###

2025-11-08

In the AnQiCMS `navList` navigation list, what are the application scenarios of the `replace` filter?

In the daily operation of AnQiCMS, we often encounter scenarios where we need to make minor adjustments to the front-end display of the website, especially in areas such as navigation where users frequently interact.AnQi CMS provides flexible template tags and filters, making these detailed adjustments easy.Today, let's talk about a practical application of the `replace` filter in the `navList` navigation list, which can help us control the presentation of navigation content more finely.### Getting to know the `replace` filter First, let's briefly review one

2025-11-08