Deep analysis of AnQiCMS: How tomoduleDetailDisplay different fields according to the model type to create a personalized content experience
As an experienced website operations expert, I know that a truly excellent content management system is not only able to publish content, but also needs to be able to flexibly adapt to various content forms.AnQiCMS (AnQiCMS) performs exceptionally well in this regard, its powerful "content model" function is the secret weapon for us to achieve personalized content display.moduleDetailUnder the help of tags, according to different content models (such as article models, product models, etc.), dynamically display matching fields on the front-end page, thereby bringing readers a more accurate and richer browsing experience.
Understand the content model mechanism of Anqi CMS: the 'blueprint' of content
In AnQi CMS, the content model is like the 'blueprint' or 'skeleton' of various contents.It defines which information fields a certain type of content should include.For example, an 'article model' usually has fields such as title, content, author, and publication date; while a 'product model' may require product name, price, inventory, specifications, and multiple image displays.
The strength of AnQi CMS lies in the fact that it not only provides built-in models such as 'Article Model' and 'Product Model', but also allows us to customize more content models according to business needsCustomize more content modelsand add for each modelexclusive custom fieldsThese custom fields are the key to our implementation of content differentiation display.
You can access the Anqi CMS backend内容管理 -> 内容模型In Chinese, find and manage these models and their custom fields.After configuring unique fields for different content models, we can intelligently retrieve and display these fields in the frontend template based on the current content model type.
Second,moduleDetailLabel: Identify the 'identity' of the content
In the template, to display different fields according to the model type, the first thing to do isIdentify which model the content on the current page belongs to. At this point,moduleDetailThe tag comes in handy.
moduleDetailThe tag is used to obtain the details of the model belonging to the current content. Through it, we can easily obtain the ID of the current model (Id) or the model table name (TableName),These are the basis of our conditional judgment.
Generally, in the content detail page (for example{模型table}/detail.html),we can use it like thismoduleDetailto get the information of the current model:
{# 获取当前模型的详细信息,并赋值给变量 currentModule #}
{% moduleDetail currentModule %}
Now,currentModuleVariables include the current content model data, for examplecurrentModule.IdIt will return the model's numeric ID, whereascurrentModule.TableNameIt will return the English table name of the model (such asarticle/productetc.). In actual development, useTableNameJudging is usually more readable.
Third,archiveDetailCombined with custom fields in a clever way: refined display
Once we pass throughmoduleDetailIdentified the type of the content model, the next task is to use this type toarchiveDetailorarchiveParamslabels to display the corresponding fields.
1. Accurate judgment, pinpoint display of specific fields
This is the most commonly used and most intuitive method. We go throughifConditional judgmentcurrentModule.TableNamevalue, and then use the corresponding code block, usingarchiveDetailtag to call the special fields of the model
Assuming we have two models:
- Article model (TableName:)
article)In addition to the default title and content, there is also a custom field "author". - Product Model (TableName:
product)In addition to the default title and content, there are custom fields "product price (price)" and "product specifications (specs)."}]
Before yourdetail.htmlIn the template, you can write logic like this:
`twig {# Firstly, get the complete data of the current content, which is convenient to call general fields, such as title, content #} {% archiveDetail archive %}
{# Get current model information #} {% moduleDetail currentModule %}
<h1 class="title">{{ archive.Title }}</h1>
<div class="meta-info">
<span>发布日期:{{ stampToDate(archive.CreatedTime, "2006-01-02") }}</span>
<span>浏览量:{{ archive.Views }}</span>
{# 您可以在此处添加其他通用字段,如分类、标签等 #}
</div>
{# 根据模型类型显示不同的字段 #}
{% if currentModule.TableName == "article" %}
<div class="article-meta">