In website content operation, we sometimes need to perform more refined processing on article titles, such as highlighting a keyword, or organizing content based on the position of the keyword.For AnQiCMS, the system provides a powerful template engine, combined with its built-in filters, we can easily achieve these requirements.Today, let's discuss how to accurately obtain the position of the first occurrence of a keyword in the article title of AnQiCMS.
Reveal the core features:indexFilter
To find the position of a specific keyword within a string in AnQiCMS templates, we need to use a very useful built-in feature——indexFilter. This filter is used to find the first occurrence of a substring in the main string.
Its basic usage is very intuitive:
{{ 主字符串 | index: "要查找的关键词" }}
When you pass a string through a pipe character|Pass toindexFilter and, when a keyword is provided as a parameter, it returns the starting position of the first occurrence of that keyword in the main string. This position is from0Starting the count, that is, if the keyword is at the beginning of the string, it will return0. If the keyword is not found in the string,indexthe filter will return-1.
Here is a place that needs special attention, especially when dealing with titles containing Chinese characters:indexThe filter will treat a Chinese character as occupying 3 bytes of space when calculating the position.This means that if your title contains Chinese and you try to search for an English keyword, the returned position may be larger than what you intuitively think."你好世界AnQiCMS"Search for"AnQiCMS",indexThe returned position will be12because the four Chinese characters in “hello world” occupy4 * 3 = 12a position)。“
Practical exercise: Find keywords in the article title
Now, let's takeindex
First, assume that we are on a detailed article page and need to retrieve the title of the current article and search for keywords in it.
{# 假设这是在文章详情页,archive 是当前文章对象 #}
{% set articleTitle = archive.Title %}
{% set searchKeyword = "AnQiCMS" %} {# 我们要查找的关键词 #}
{% set keywordPosition = articleTitle|index:searchKeyword %}
{% if keywordPosition != -1 %}
<p>关键词 "{{ searchKeyword }}" 在标题 "{{ articleTitle }}" 中的首次出现位置是:{{ keywordPosition }} (从0开始计数)</p>
{# 我们可以进一步利用这个位置信息 #}
{% set keywordLength = searchKeyword|length %} {# 获取关键词的字节长度,用于后续截取 #}
{% set textBefore = articleTitle|slice:":keywordPosition" %} {# 截取关键词前面的部分 #}
{% set textAfter = articleTitle|slice:"(keywordPosition + keywordLength):" %} {# 截取关键词后面的部分 #}
<p>标题中关键词前面的内容:<span>{{ textBefore }}</span></p>
<p>标题中关键词后面的内容:<span>{{ textAfter }}</span></p>
{# 甚至可以在模板中将关键词高亮显示,但这需要更复杂的字符串操作或在后端处理 #}
{# 简单示例:替换关键词 #}
{% set highlightedTitle = articleTitle|replace:"(searchKeyword, <mark>" ~ searchKeyword ~ "</mark>)" %}
<p>高亮后的标题(简单替换):{{ highlightedTitle|safe }}</p>
{% else %}
<p>在标题 "{{ articleTitle }}" 中未找到关键词 "{{ searchKeyword }}"。</p>
{% endif %}
This code first retrieves the latest 5 articles by{% set ... %}Label defines the article title and the keywords we want to search for. Next, it usesindexThe filter retrieves the position of the keywords. If the position is not-1It indicates that the keyword exists and will display its position.
你可以将searchKeywordSet it as a variable to dynamically obtain the keyword from URL parameters or other configurations, making the search more flexible.
Further: Combine with other filters to perform advanced operations
indexThe filter is powerful, but when combined with other AnQiCMS template filters, it can achieve more rich title processing functions.
Determine if the keyword exists:
containFilterIf you only need to know whether the title contains a certain keyword without knowing the specific location,containthe filter will be more concise. It returns directly,trueorfalse.{% if articleTitle|contain:"重要" %} <p>标题中包含“重要”这个词。</p> {% endif %}to implement case-insensitive search:
lowerorupperFilterindexThe filter is case sensitive. If you want to perform a case-insensitive search, you can convert the title and keywords to lowercase (or uppercase) before comparison.{% set lowerTitle = articleTitle|lower %} {% set lowerKeyword = searchKeyword|lower %} {% set caseInsensitivePosition = lowerTitle|index:lowerKeyword %} {% if caseInsensitivePosition != -1 %} <p>不区分大小写查找,关键词 "{{ searchKeyword }}" 在标题中存在。</p> {% endif %}Extract the content before and after the keywords:
sliceFilterAs shown in the previous example, once we have obtained the starting position of the keywords (keywordPosition) and the length of the keywords (searchKeyword|length),we can usesliceThe filter to extract the content before and after the keywords in the title, which is very useful for displaying summaries or highlighting context.{# 结合上面的例子 #} {% set textBefore = articleTitle|slice:":keywordPosition" %} {% set textAfter = articleTitle|slice:"(keywordPosition + keywordLength):" %}
Through these flexible filter combinations, you can perform various customized keyword analysis and display on article titles in the AnQiCMS template.
Summary
The template engine of AnQiCMS providesindexFilter that allows us to accurately find the first occurrence of a specific keyword in the article title. Although attention must be paid to the byte count characteristics of Chinese characters during processing, but by usingindexCombineset/contain/lower/slice等其他过滤器,我们可以实现对标题关键词的查找、判断、提取甚至简单的内容重组,从而为网站内容的动态展示和用户体验带来更多可能性。