The wisdom of dynamically building jump links in the Anqi CMS front-end template according to the content model ID
As an experienced website operations expert, I know that flexibility and automation are the keys to improving efficiency in daily content management.AnQi CMS provides us with great convenience with its powerful content model customization capabilities and a friendly template engine.Today, let's delve into a very practical technique in front-end development: how to dynamically construct a link to the home page 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 structural adjustments of the website.
Understanding the link building mechanism of Anqi CMS
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:moduleDetailThe clever application of
To get the homepage link of a content model,moduleDetailthe tag is our powerful assistant. This tag is specifically used to obtain detailed data of a specified content model.
When we want to get the home 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"is crucial, it tellsmoduleDetailThe label we want to get is the jump link of the model.id="1"orid="2"It specifies the ID of the target content model. After execution,{{ articleModel }}and{{ productModel }}The output will be the homepage URL generated by the model according to the current pseudo-static rules, for example/article.htmlor/product.html.
The cleverness of this method lies in the fact that, regardless of how the pseudo-static rules on the backend are adjusted (for example from/article.htmlChanges to/modules/article-index.html)moduleDetailTags always return the correct, updated links without us having to manually modify the template code.
Further: Link construction for category pages under dynamic model building
Many times, we not only need to jump to the overall homepage of the content model, but may also need to jump to the list page of a specific category under the model.The AnQi CMS also provides an elegant solution.
We can utilizecategoryDetailUse tags to get links to specific categories. This tag can retrieve detailed data for a specified category, including the link to that category.
Assuming we are looping through multiple documents and we want to generate a list page link for the category of each document:
{% 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, thencategoryDetailLabels were retrieved for the corresponding category based on this ID.LinkThis way, 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 certain model directly on the page and provide links for them, we can combine them in this way:
{# 假设文章模型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 %}
categoryListTagged throughmoduleIdThe parameter specifies the model it belongs to and throughparentId="0"Filter out the top-level categories. In the loop,{{ cat.Link }}the list page links of each category will be automatically output, and these links will be dynamically generated according to the pseudo-static rules of the backend.
Integrated application and dynamic considerations
In actual development, we rarely hard-code model IDs or category IDs. More often, these IDs come from the context of the current page, items in the loop, or throughsystemThe global configuration dynamically obtained from the tag retrieval.
For example, in a universal sidebar, we may want to list the home links of all content models:
This is a hypothetical backend tag used to get the list of all content models. If the Anqi CMS does not provide it directly, it can be simulated by looping through the category list and obtaining its moduleId. {% comment %} It is assumed that all model basic information can be obtained in some way {% endcomment %} Here we use known model IDs for demonstration.