In the content management of Anqi CMS, the document detail pagearchiveDetailtags are very powerful, they can help us obtain 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, usually containing the following key information:Title(Title text),Tag(HTML tags such as h1, h2),Level(Title level, such as 1, 2), as well asPrefix(May be used as a prefix for numbering, such as 1.1).

When we get this title array, how can we further process it to make it play a greater role? The template engine of Anqi CMS provides rich logic tags and filters, allowing us to process it as if it were a normal array,ContentTitlesPerform various operations.

1. Basic traversal and conditional filtering: Build dynamic article contents.

The most direct usage is to traverse this array and display all titles. This is usually done throughforLoop tag implementation. For example, we can easily generate a dynamic table of contents for articles, allowing readers to quickly jump to the parts of interest.

{% 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, which can convert title text into URL-safe strings, very suitable as an in-page anchor point,id)The value, to achieve smooth jumping after clicking on the catalog item.

If we only want to display titles of a specific level, such as only displaying the second-level (H2) titles as main chapters, we can useifConditional 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 Total Number of Titles:lengthThe filter can return the number of elements in the array. This is very useful for displaying how many titles an article contains, or for not displaying the table of contents when the number of titles is very small.

    {% archiveDetail titles with name="ContentTitles" %}
        {% if titles %}
            <p>本文共包含 <strong>{{ titles|length }}</strong> 个标题,为您提供了结构化的阅读体验。</p>
        {% endif %}
    {% endarchiveDetail %}
    
  • Check if a specific title exists: Although there is no direct filter to judge whetherObjectsome attribute matches a specific value, but we can combineforloop andifImplement the statement and use a boolean variable to record the result.

    auto

    {% set has_conclusion = false %}
    {% for item