The wisdom of dynamically building jump links in the Anqi CMS front-end template based on content model ID

As an experienced website operations expert, I know that flexibility and automation are the key to improving efficiency in daily content management.AnQi CMS with its powerful content model customization capabilities and friendly template engine, provides us with great convenience.Today, let's delve deeply into a very practical skill in front-end development: how to dynamically build a link to the homepage or list page of a model based on the model's ID without hardcoding the path.This not only makes the template more versatile, but also leaves sufficient flexibility for the future adjustment of the website structure.

Understanding the link building mechanism of Anqi CMS

In AnQi CMS, the URL structure of the website content is not fixed, it depends heavily on the static rules configured in the background.Whether it is an article, product, or custom content model, the link form of its detail page, list page, and even the model homepage is defined by these rules.Therefore, we cannot simply concatenate strings to construct links, but should use the powerful tags provided by the Anqi CMS template engine to allow the system to automatically parse and generate the correct URL for us.

One of the core concepts of AnQi CMS is "modeling", which abstracts different types of content (such as articles, products) into different "content models" and allows us to define independent fields and display logic for each model.Therefore, to dynamically construct links, our approach naturally revolves around these content models.

Core tags:moduleDetailingenious application

To get the homepage link of a content model,moduleDetailThe tag is our powerful assistant. This tag is specifically used to retrieve detailed data of a specified content model.

When we want to get the home page link of a model (such as an article model or product model), we can do it like this:

{# 假设我们已知文章模型的ID是1 #}
{% moduleDetail articleModel with name="Link" id="1" %}
    <p>文章模型首页链接:<a href="{{ articleModel }}">{{ articleModel }}</a></p>
{% endmoduleDetail %}

{# 假设我们已知产品模型的ID是2 #}
{% moduleDetail productModel with name="Link" id="2" %}
    <p>产品模型首页链接:<a href="{{ productModel }}">{{ productModel }}</a></p>
{% endmoduleDetail %}

Here, name="Link"It is crucial, it tellsmoduleDetailThe tag we want to get is the jump link of the model.id="1"orid="2"It specified the ID of the target content model. After execution,{{ articleModel }}and{{ productModel }}the homepage URL generated by the corresponding model according to the current static rule will be output, for example,/article.htmlor/product.html.

The ingenuity of this method lies in the fact that no matter how the back-end static rule adjustment (for example, from/article.htmlchanges to/modules/article-index.html)moduleDetailLabels always return the correct, updated link without the need to manually modify the template code.

Further: Link list page under the dynamic model construction

At times, we not only need to jump to the overall home page of the content model, but may also need to jump to the list page of a specific category under the model.AnQi CMS also provides an elegant solution.

We can make use ofcategoryDetailLabel to get links to specific categories. This label can obtain detailed data for the specified category, including the link to the category.

Assuming we are looping through multiple documents and want to generate a list page link for each document's category:

{% archiveList recentArchives with type="list" limit="5" %}
    {% for archiveItem in recentArchives %}
        <div>
            <h4><a href="{{ archiveItem.Link }}">{{ archiveItem.Title }}</a></h4>
            {# 获取当前文档所属分类的列表页链接 #}
            {% categoryDetail categoryLink with name="Link" id=archiveItem.CategoryId %}
                <p>所属分类:<a href="{{ categoryLink }}">{{ categoryItem.Title }}</a></p>
            {% endcategoryDetail %}
        </div>
    {% endfor %}
{% endarchiveList %}

In this example,archiveItem.CategoryIdDynamically provides the category ID of the current looping document, thencategoryDetailThe label retrieves the corresponding category based on this IDLinkSo, each document's category link can accurately point to the list page of the category.

If we need to display all top-level categories under a model directly on the page and provide links for them, we can use this combination:

{# 假设文章模型ID为1,获取其所有顶级分类 #}
{% categoryList topCategories with moduleId="1" parentId="0" %}
    <p>文章模型顶级分类列表:</p>
    <ul>
        {% for cat in topCategories %}
            <li><a href="{{ cat.Link }}">{{ cat.Title }}</a></li>
        {% endfor %}
    </ul>
{% endcategoryList %}

categoryListTag throughmoduleIdThe parameter specifies the model and usesparentId="0"Filter out the top-level categories. In the loop.{{ cat.Link }}It will automatically output the list page links of each category, and these links will be dynamically generated according to the pseudo-static rules of the background.

Comprehensive application and consideration of dynamics

In actual development, we rarely hardcode model IDs or category IDs. More often, these IDs are obtained from the context of the current page, items in the loop, or throughsystemThe label obtained from the globally configurable dynamic acquisition.

For example, in a universal sidebar, we might want to list the home page links of all content models:

`twig {# This is a hypothetical backend tag used to retrieve the list of all content models. If the Safe CMS does not provide it directly, it can be simulated by looping through the category list and retrieving its moduleId. #} {% comment %} It is assumed that we can obtain the basic information of all models in some way {% endcomment %} {# Here we use a known model ID for demonstration #}