AnQiCMS (AnQiCMS) provides great convenience to website operators with its flexible content model and powerful template tag system. However, in practice, we often encounter a question: likemoduleDetailThis tag is usually used to retrieve information about a specific content model, is it still available on pages like the home page that are not related to models?How can it be safely and effectively used to obtain the required model information?As an experienced website operations expert, I will take you deep into this topic.

ExploremoduleDetailThe core role of tags

Firstly, we need to clarifymoduleDetailLabel positioning.In AnQiCMS, the content model is the cornerstone of organizing and managing website content, for example, the 'article model' is used to publish news and information, and the 'product model' is used to display products.moduleDetailThe original purpose of the tag design is to facilitate the acquisition of these in the templateThe metadata of the content model itselfIt refers to the general content rather than the specific content under the model (such as the title of an article or product price). It helps us obtain the ID, name, link, description, and other basic information of the model.

For example, if you are on a page that displays a list of all articles under a specific "article model".moduleDetailThe tag can implicitly obtain the model context information of the current page. But when the page does not have this explicit model context, the situation is different.

moduleDetailExploration of availability on non-model-related pages

Now, let's directly answer the core question:moduleDetailThe tag is available on the homepage or single page of the website, etc., which is non-model-relatedis completely available.

The key is that in these pages lacking implicit model contexts, you needto explicitly tellmoduleDetailwhich model information you want to retrieveThis means you cannot rely on it to automatically recognize the "current model", but must implement it by providing clear parameters. AnQiCMS provides two main ways to do this:

  1. Through model ID (idparameters)Each content model has a unique numeric ID in the AnQiCMS backend.
  2. Through model URL alias (tokenparameters)In the content model settings, you can define a URL alias for the model using an English lowercase letter, such asarticleorproductwhich can also be used as a model identifier.

For example, assuming your 'Article Model' ID is 1, the URL alias isarticle;“Product Model” ID is 2, the URL alias isproduct. Then, on the website homepage, you can obtain their detailed information in the following way:

  • Get the article model title:{% moduleDetail with name="Title" id="1" %}
  • Get the product model link:{% moduleDetail with name="Link" token="product" %}

Through these explicit parameters,moduleDetailLabels can accurately locate the specified content model regardless of the page environment and return its relevant information.

Safely obtain and use model information

In templates, when retrieving data, especially data from backend configurations, it is always necessary to consider 'safety' and 'robustness'. 'Safe' here refers more toAvoid page rendering failure or display anomalies due to missing data or parameter errorsThe following are several key security practices:

1. Use variable assignment to obtain the complete model object

AnQiCMS template tags support assigning the retrieved data to a variable. This is more efficient and safer than calling once for each field.moduleDetailTags are more efficient and safer.

{# 假设我们需要获取ID为1的文章模型信息 #}
{% moduleDetail articleModelInfo with id="1" %}

{# 此时,articleModelInfo 变量将包含文章模型的所有字段(如 Id, Title, Link 等) #}
{# 如果模型不存在,articleModelInfo 将是一个空对象或nil #}

2. Always use conditional judgment (ifstatements)

After obtaining model information, be sure to use{% if ... %}Condition tags to check if the variable has successfully retrieved the data. This can effectively prevent page errors due to situations such as the model not existing, or an incorrect ID input.

{% moduleDetail articleModelInfo with id="1" %}
{% if articleModelInfo %}
    <p>文章模型名称:{{ articleModelInfo.Title }}</p>
    <p>文章模型链接:<a href="{{ articleModelInfo.Link }}">{{ articleModelInfo.Link }}</a></p>
{% else %}
    <p>抱歉,未能加载文章模型信息,请稍后再试或联系管理员。</p>
{% endif %}

This approach is known as 'defensive programming,' which ensures that your website maintains a good user experience even if some data is missing, rather than directly displaying an error or blank area.

3. UnderstandnameThe difference between parameter and direct object access

When you use{% moduleDetail with name="字段名称" %}When, the tag will try to directly output the value of the field. If the field does not exist or the model is not found, it may directly output an empty string.

When you use{% moduleDetail myVar with ... %}Assign the entire model object tomyVarAfter that, you can access its properties through{{ myVar.Title }}/{{ myVar.Link }}and other methods. This method, when combined withif myVarcan be used to control the display logic more finely.

4. Under multi-site environmentsiteIdParameter

If you have used the AnQiCMS multi-site management function and want to retrieve model information from sites other than the current site, don't forget to usesiteIdSpecify the target site. For example:{% moduleDetail productModel with id="2" siteId="3" %}(Get product model information for site ID 3).

Practical example: Display the entry for a specific model on the homepage.

Let's integrate the above knowledge points through a common scenario: Display a "Product Center" link on the website homepage and also show a brief description of the product model.

`twig