In Anqi CMS, when managing content, the document detail pagearchiveDetailtags are very powerful, they can help us get rich information about the current document. Among them,ContentTitlesThe field is a particularly useful data structure that returns an array containing the hierarchical information of all titles (such as H1, H2, H3, etc.) in the document content.This provides us with great flexibility, which can be used to build article catalogs, intelligent navigation, and even content analysis.
ContentTitlesEach element returned by the field is an object, typically containing the following key information:Title(Title text),Tag(HTML tags such as h1, h2),Level(title levels such as 1, 2), andPrefix(May be used as a prefix for numbering, such as 1.1).
How can we further process this title array after we get it, so that it can play a greater role? The Anqi CMS template engine provides rich logic tags and filters, allowing us to process it like a regular array, and toContentTitlesPerform various operations.
1. Basic traversal and conditional filtering: Build a dynamic article catalog
The most direct way is to traverse the array and display all the titles. This is usually done throughforImplementing loop tags. For example, we can easily generate a dynamic table of contents for articles, allowing readers to quickly jump to the parts they are interested in.
{% archiveDetail titles with name="ContentTitles" %}
{% if titles %} {# 检查标题数组是否存在且不为空 #}
<nav class="article-toc">
<h3>文章目录</h3>
<ol>
{% for item in titles %}
{# 根据标题层级添加不同的样式或前缀,或者生成页面内跳转链接 #}
<li class="level-{{ item.Level }}">
<a href="#section-{{ item.Title | urlencode }}">{{ item.Prefix }} {{ item.Title }}</a>
</li>
{% endfor %}
</ol>
</nav>
{% endif %}
{% endarchiveDetail %}
Here, item.LevelHelp us identify the level of headings,item.Title | urlencodeIt is a very practical filter that can convert title text into a URL-safe string, very suitable as a page anchor (idThe value to implement smooth jump when clicking on the catalog item.
If we only want to display specific levels of titles, such as only displaying secondary (H2) titles as main chapters, we can use.ifConditional judgment:
{% archiveDetail titles with name="ContentTitles" %}
{% if titles %}
<nav class="article-main-sections">
<h4>主要章节</h4>
<ul>
{% for item in titles %}
{% if item.Level == 2 %} {# 只显示二级标题 #}
<li><a href="#section-{{ item.Title | urlencode }}">{{ item.Prefix }} {{ item.Title }}</a></li>
{% endif %}
{% endfor %}
</ul>
</nav>
{% endif %}
{% endarchiveDetail %}
2. Array Length and Content Check: Quick Overview of Article Structure
In addition to iteration, we can also use some array filters to obtain overall information about the array.
Get the Total Number of Titles:
lengthThe filter can return the number of elements in an array. This is very useful for displaying how many titles an article contains, or not displaying the table of contents when the number of titles is very few.{% archiveDetail titles with name="ContentTitles" %} {% if titles %} <p>本文共包含 <strong>{{ titles|length }}</strong> 个标题,为您提供了结构化的阅读体验。</p> {% endif %} {% endarchiveDetail %}Check if a specific title exists: There is no direct filter to determine whether the array containsobjectWhether a certain attribute matches a specific value, but we can combine
forloop andifImplement the statement and use a boolean variable to record the result.”`twig {% archiveDetail titles with name=“ContentTitles” %}
{% set has_conclusion = false %} {% for item