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:
- 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.
- 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.
- 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