Use AnQiCMS content model URL alias (token):The wisdom of accurately obtaining information

As an experienced website operations expert, I am well aware of the importance of an efficient and customizable content management system for corporate websites and content operations teams.AnQiCMS with its efficient architecture in Go language and its versatile functions, is gradually becoming a powerful assistant for many operators.In daily content management, we not only strive for the quality presentation of content, but also for the efficient call and management of data.Today, let's delve deeply into a powerful feature in AnQiCMS that can optimize URL structure and achieve precise content acquisition - Content Model URL Alias (token)

In AnQiCMS, the content model is the foundation we build various content types on, whether it's articles, products, events, or single pages, they all rely on the flexible definition of the content model. The closely related URL alias, which is what we usually calltokenIt plays the role of an 'ID card' for the content on the web page.It not only makes our URL address more readable and friendly to search engines, but more importantly, it provides a convenient way to directly locate and obtain specific model information without using an ID.

URL alias (token) Importance: More than just beauty

We all know that a clear and meaningful URL is crucial for SEO and user experience.AnQiCMS was well thought out from the very beginning.When you create articles, categories, single pages, or even the content model itself, the system will automatically generate a pinyin-based URL alias based on the title of the content you enter.Of course, if you are not satisfied with the automatically generated alias, you can always manually modify it to ensure that it reflects the content theme and includes core keywords, thereby better supporting SEO optimization.It is worth noting that in order to avoid link conflicts and unnecessary disturbances, the manually set URL alias must ensure the uniqueness of the entire site.If a duplicate occurs, AnQiCMS will automatically add a random number at the end to ensure its uniqueness.

This custom URL alias mechanism, combined with the pseudo-static rule management function of AnQiCMS, can make your website URL structure more standardized, for example, you can set the document detail page to/{module}-{filename}.htmlor/{catname}/{filename}.html.{filename}It usually corresponds to what we call a URL alias ortoken.

However,tokenIt is far more than just its value. It is not only an aesthetically pleasing part of the URL, but also a powerful parameter in AnQiCMS template tags, allowing us to obtain specific content information in a more intuitive and business-oriented way.

Core mechanism: URL alias (token)How to accurately retrieve information

In AnQiCMS template development, when we want to obtain detailed information about a specific document, category, single page, or model itself, we usually think of using its numeric ID. But when the content ID changes or we want to use a more semantic identifier, tokenParameters came into play. AnQiCMS provides a series of detail tags, all of which support specifying the content to be obtained throughtokenparameters.

These tags include:

  • archiveDetailUsed to obtain detailed information about a specific document (article, product, etc.).
  • categoryDetailUsed to obtain detailed information about a specific category.
  • pageDetailUsed to obtain detailed information about a specific single page.
  • moduleDetailUsed to obtain detailed information about a specific content model.
  • tagDetailUsed to obtain detailed information about a specific tag.

The general calling format of them is roughly as follows:{% XXXDetail 变量名称 with name="字段名称" token="别名字符串" %}. Here,变量名称Optional, if you want to output a specific field directly, you can omit it;name="字段名称"Then it specifies the specific content field you want to get, such asTitle/DescriptionorContent; whereastoken="别名字符串"It is the main character of our day, allowing you to directly lock and extract the corresponding content through that unique URL alias set in the background.

Practical exercise: MastertokenCalling技巧

In order to understand bettertokenActual application, we explore several common scenarios to find out.

Scenario one: Obtain detailed information of a specific article (document)

Imagine that you might need to display a fixed article's title and link in a specific area of a website, such as the 'Recommended Reading' module on the homepage, and the ID of this article may change due to data migration. At this time, if you set a stable URL alias for this article, such asabout-our-companySo it can be passed throughtokencan be easily obtained.

{# 假设“关于我们公司”这篇文章的URL别名是 "about-our-company" #}
<div>
    <h3>关于我们</h3>
    {% archiveDetail companyIntro with name="Description" token="about-our-company" %}
    <p>{{ companyIntro|truncatechars:100 }}</p> {# 截取前100个字符显示简介 #}
    {% archiveDetail companyLink with name="Link" token="about-our-company" %}
    <a href="{{ companyLink }}">了解更多</a>
</div>

Through this code, no matter how the article ID changes, as long as the URL aliasabout-our-companyKeep it unchanged, we can always accurately obtain its introduction and link. This greatly improves the robustness and maintainability of the template code.

Scene two: Get detailed information of a specific category

Sometimes, we may need to display an introduction to a certain product category fixed in the website sidebar or link to a specific solution category in the footer. ThroughtokenWe can easily achieve this goal.

{# 假设有一个名为“热门产品”的分类,其URL别名是 "hot-products" #}
<div class="sidebar-category">
    <h3>{% categoryDetail with name="Title" token="hot-products" %}</h3>
    <p>{% categoryDetail with name="Description" token="hot-products" %}</p>
    {% categoryDetail hotProductLink with name="Link" token="hot-products" %}
    <a href="{{ hotProductLink }}">查看热门产品</a>
</div>

Even if the category ID changes, as long astokenunchanged, the content of the sidebar will also be updated consistently.

Scenario three: Get the content of a specific single page.

The single page is very common in corporate websites, such as 'Contact Us', 'Terms of Service', etc. These pages are often the evergreen trees of the website, with relatively fixed content and links. ThroughtokenIt is the most natural choice to call them.

This is the terms of service page, the URL alias is "terms-of-service"

<nav>
    <ul>
        <li><a href="/">首页</a></li>
        {% pageDetail termsLink with name="Link" token="terms-of-