How does AnQiCMS display the table of contents or outline of the article content on the article detail page?

Calendar 78

For articles that are rich in content and long in length, a clear table of contents or outline is like a navigation map, which can greatly enhance the reading experience of the reader.It not only helps readers quickly grasp the main outline of the article, but also allows them to accurately jump to the parts of interest, thereby improving the readability and practicality of the content.In AnQiCMS, implementing the content directory display on the article detail page is not difficult, the built-in functions of the system provide us with a simple and efficient solution.

How does AnQiCMS understand the structure of an article?

AnQiCMS was designed from the beginning with flexibility in content management and search engine friendliness. When you create articles in the editor, whether you use a rich text editor to set H1, H2, H3, etc., titles, or write with a Markdown editor with#,##,###The marked level title, AnQiCMS will intelligently parse these structures.

The system will not simply view the content as a large block of text, but can recognize the hierarchy and logic within it.This intelligent recognition capability is the basis for generating the table of contents on the article detail page.AnQiCMS will handle the article content internally, extract all title information, and organize them into a structured data list for template calls.

ByContentTitlesTag to get directory data

We need to display the table of contents on the article detail page, usually in the corresponding template file (usuallyarchive/detail.htmlOr customize the article detail template) Use the template tags provided by AnQiCMS to obtain these structured data. The key point lies inarchiveDetailThe tag and its special field:ContentTitles.

ContentTitlesThe field is used to return a list of all identified titles in the article content. It is not a direct HTML structure, but an array of objects that represent each title and provide the following information:

  • Title: The text content of the title.
  • Tag: The HTML tag of the title (e.g.,h1,h2,h3), helps us determine the level of the title.
  • Level: The numeric level of the title (e.g., H1 corresponds1, H2 corresponds to2),This is very useful for the indentation and style control of the catalog.
  • Prefix: Numbers or symbols that may exist before the title (if generated by the editor).

With these data, we can flexibly construct the article directory.

Build the directory display step by step

Next, let's take a look at how to useContentTitlesGenerate an interactive directory in practice.

1. Locate the template file.

Firstly, you need to log in to the AnQiCMS backend, find the currently used template in the "Template Design" section, and locate the template file responsible for displaying the article detail page. According to the template design specifications, it is usuallytemplate/您的模板目录/{模型table}/detail.htmlOr in the specific file specified in the custom template settings.

2. Get directory data

In the appropriate position in the template file (for example, the article content sidebar or above the article content), usearchiveDetailtag to obtainContentTitlesdata. It is recommended to useifThe tag is used to judge, ensuring that the catalog is only displayed when the article content contains a title.

{%- archiveDetail contentTitles with name="ContentTitles" %}
{% if contentTitles %}
    <div class="article-outline">
        <h3>文章目录</h3>
        <ul>
        {% for item in contentTitles %}
            {# 生成唯一的锚点ID,通常基于标题内容进行简化,这里仅作简单示例 #}
            {% set anchorId = 'heading-' ~ forloop.Counter ~ '-' ~ item.Title|replace(" ", "-")|lower %}
            <li style="margin-left: {{ (item.Level - 1) * 20 }}px;">
                <a href="#{{ anchorId }}">{{ item.Prefix }}{{ item.Title }}</a>
            </li>
        {% endfor %}
        </ul>
    </div>
{% endif %}

In this code block:

  • We first pass through{%- archiveDetail contentTitles with name="ContentTitles" %}The catalog data has been obtained and assigned tocontentTitlesVariable.
  • {% if contentTitles %}Ensure that the directory structure is rendered only when the article has a title.
  • {% for item in contentTitles %}Loop through each title.
  • {% set anchorId = 'heading-' ~ forloop.Counter ~ '-' ~ item.Title|replace(" ", "-")|lower %}This line of code attempts to generate a unique ID for each title. Because the AnQiCMS filter does not have a directslugifyfunction, here we useforloop.CounterEnsure uniqueness and usereplaceEasily replace spaces with hyphens and convert to lowercase as a basic anchor ID generation example.In practice, you may need more robust JavaScript code to generate the HTML title ID and maintain consistency with the directory links.
  • margin-left: {{ (item.Level - 1) * 20 }}px;Smartly utilizeitem.LevelTo achieve hierarchical indentation of the catalog, for example, H2 titles will be indented 20 pixels more than H1 titles.
  • <a href="#{{ anchorId }}">{{ item.Prefix }}{{ item.Title }}</a>Created a catalog item, linking to the anchor point corresponding to the title in the article content.

3. Ensure that the article content title has the corresponding anchor ID.

To ensure that the links in the above directory can be correctly navigated, the actual HTML in the article content

Related articles

How to generate random text in AnQiCMS template as placeholder content for development testing display?

During the development of AnQiCMS templates, we often need to fill in placeholder text on the page before the real content is ready, in order to better observe the layout, test the style and function.Manually entering "test content" or placeholder text is time-consuming and lacks authenticity.Fortunately, AnQiCMS provides a very convenient built-in tag that can elegantly solve this problem - that is the `lorem` tag.### AnQiCMS is a random text generator tool: `lorem` tag `lorem`

2025-11-08

How to automatically parse URL strings in HTML code to display clickable links in AnQiCMS?

In the daily operation of websites, we often need to insert various URLs in the article content, whether it is to refer to external resources or to guide users to visit other pages within the site.If adding the `<a>` tag manually to these URLs each time is time-consuming and prone to errors.It is fortunate that AnQiCMS provides users with a very intelligent and convenient solution in this regard, which can automatically identify and parse URL strings in HTML code and convert them into clickable links.### Core Mechanism: Make the URLs in the content "alive" When you edit articles in the AnQiCMS backend

2025-11-08

How to use AnQiCMS filter to perform string truncation, case conversion, and other processing on displayed content?

When using AnQiCMS for website content management, we often encounter situations where we need to make some fine adjustments to the displayed content, such as when a title of an article is too long and we want it to be displayed partially with an ellipsis at the end;Or some text should be displayed in uppercase or lowercase. The powerful template filter function of AnQiCMS can easily help us meet these needs without modifying any backend code, and it can be completed directly at the template level.The template filter is a practical tool in AnQiCMS template language, which allows us to transform the values of template variables

2025-11-08

How does AnQiCMS ensure that mathematical formulas and flowcharts inserted in the Markdown editor are displayed normally on the front end?

In modern website content creation, Markdown is favored by a large number of content creators for its concise and efficient features.However, for websites that need to present complex information, such as mathematical formulas and flowcharts, how to seamlessly insert them into Markdown editors and ensure they are displayed normally on the front end is often a challenge for content operators.AnQiCMS (AnQiCMS) fully understands this need, through flexible configuration and standardized introduction methods, allowing these complex elements to be elegantly presented on your website

2025-11-08

How to dynamically retrieve and display the contact information of a website, such as phone number, email, and social media links?

The contact information of the website, such as phone number, email, and social media links, is an important bridge for users to connect with us.It is crucial that the accuracy and accessibility of this information is maintained in the operation of the website.AnQiCMS as an efficient content management system provides a very convenient way to manage and dynamically display this contact information, allowing us to easily maintain the consistency of website content and quickly update it when needed.This article will delve into how to set contact information in AnQiCMS, as well as how to dynamically retrieve it in templates and display it in a user-friendly manner.### One

2025-11-08

How does AnQiCMS set the 'default thumbnail' to unify the display effect when images are missing?

In website content operation, images play a vital role. They can attract users' attention, enhance the visual appeal of the content, and help users quickly understand the theme of the content.However, in the process of daily content publishing, we sometimes encounter situations where some articles fail to upload exclusive thumbnails for various reasons, or the content itself does not have suitable images to extract.If the front-end page directly displays blank, broken image icons, or disordered layout, it will undoubtedly severely affect user experience and the professionalism of the website.To solve this problem, AnQiCMS

2025-11-08

How AnQiCMS affects the display effect of the website under different template modes (adaptive, code adaptation, PC + mobile)?

When building and operating a website, we often consider how to make the website display beautifully on different devices.AnQiCMS (AnQiCMS) fully understands this point and therefore provides three flexible template modes to meet different needs: adaptive, code adaptation, and PC + mobile independent site mode.Each pattern has its unique implementation method and impact on the display effect of the website, understanding them can help us better choose the solution suitable for our project.### One, Adaptive Mode: One template, multiple screens Adaptive mode, as the name suggests

2025-11-08

How to determine if a variable exists or is empty in a template and display different default content based on the result?

During the development and maintenance of website templates, we often encounter situations where variables may not exist, have empty values, or do not meet expectations.If these variables are not properly handled, it may lead to page display anomalies, or even cause errors, affecting user experience.AnQiCMS (AnQiCMS) provides a powerful and flexible template engine based on Django syntax, allowing us to easily determine the state of variables and display different content accordingly.

2025-11-08