As an experienced website operations expert, I deeply understand the powerful capabilities brought by the flexible content model in AnQiCMS through my practice.At times, we not only need to display the content itself, but also need to drive deeper page logic or styling based on the content's 'type'.Today, let's delve deeply into a seemingly minor but crucial feature: how to obtain the database table name of the content model in the AnQiCMS template (TableNameTo achieve more refined customization.


How to get the database table name (TableName) of the content model in the AnQi CMS template: Deep Analysis and Practical Application

AnQiCMS is known for its highly customizable content model.We all know that we can create various content models such as 'articles', 'products', 'cases', etc. Each model may carry unique field information according to business needs.But in the actual template development process, sometimes we encounter such a scenario: we need to base it on the current page's content modelUnderlying database table nameTo dynamically adjust the page layout, load specific JavaScript scripts, or even perform some advanced conditional judgments. At this point, relying solely on model ID or name may not meet our needs, and obtaining the correspondingTableNameIt is particularly important.

In the internal mechanism of AnQiCMS, each content model has a dedicated table name in the database to store all data of the model. For example, as mentioned in the document, the template path convention is directly used.{模型table}Such placeholders suggestTableNamePlaying a core role in the operation logic of AnQiCMS.The Anqi CMS provides simple and powerful template tags, allowing us to easily obtain this information in the front-end template.

Core tool:moduleDetailThe clever use of tags

The database table names of content models are mainly dependent on the provided by AnQiCMSmoduleDetailTemplate tag. This tag is specifically used to obtain the detailed information of the content model, which includes the information we need.TableName.

The basic way to use it is like this:

{% moduleDetail modelInfo with name="TableName" %}
{{ modelInfo }}

In this code block:

  • {% moduleDetail modelInfo ... %}We define a variable namedmodelInfoThe variable is used to store the obtained model details. You can also output it directly without defining a variable name.
  • with name="TableName": This is the key point, where we explicitly tell the system which fields we need to retrieve.TableName.

How to get the name of the content model table of the current page

The most common scenario is that you are browsing a page of a specific content model (such as an article or a product detail page) and you want to get the corresponding content model of the currentTableName. At this point,moduleDetailThe tag will automatically recognize the context of the current page:

{# 假设当前页面是一个“文章”模型的详情页 #}
<div>当前内容模型的数据库表名是:{% moduleDetail with name="TableName" %}</div>

{# 将表名存储在变量中以供后续使用 #}
{% moduleDetail currentModelTableName with name="TableName" %}
<div class="{{ currentModelTableName }}-wrapper">
    <!-- 针对文章模型(假设表名为article)的特定内容和样式 -->
</div>

In the above examples, if the current page is the "article" model, the database table name is usuallyarticle(This can be viewed and configured in the background "Content Management" -> "Content Model"). Then{{ currentModelTableName }}it will be outputarticle.

How to get the table name of a specified content model

Sometimes, we may not be on a specific model page, but still need to obtain the database table name of a known model (such as the "product" model). At this time, we can use the model'sIDorURL别名(token)Please specify:

{# 获取ID为2的内容模型(假设是产品模型)的数据库表名 #}
<div>产品模型的数据库表名是:{% moduleDetail productModelTable with name="TableName" id="2" %}{{ productModelTable }}</div>

{# 也可以通过模型的URL别名来获取,假设URL别名是'product' #}
<div>产品模型的数据库表名(通过别名获取):{% moduleDetail productModelTableByToken with name="TableName" token="product" %}{{ productModelTableByToken }}</div>

In this way, even if you are on the homepage or any other page, you can accurately obtain the specified content modelTableName.

TableNameApplication scenarios

obtaining toTableNameAfter that, we can implement various flexible applications in the template:

  1. Dynamic styles and script loading:According to differentTableNameFor loading different CSS classes or JavaScript files to personalize the page display. For example, the article model page loadsarticle-specific.cssFor loading the product model pageproduct-gallery.js.
    
    {% moduleDetail currentModelTableName with name="TableName" %}
    <body class="{{ currentModelTableName }}-page">
        {# ... 页面内容 ... #}
        {% if currentModelTableName == "product" %}
        <script src="/static/js/product-gallery.js"></script>
        {% endif %}
    </body>
    
  2. Conditional rendering content block:Display or hide specific content areas or functional modules in a shared template layout based on the model type.
    
    {% moduleDetail currentModelTableName with name="TableName" %}
    {% if currentModelTableName == "article" %}
        <div class="article-sidebar">
            <!-- 显示文章相关的热门推荐 -->
        </div>
    {% elif currentModelTableName == "product" %}
        <div class="product-specs">
            <!-- 显示产品参数表格 -->
        </div>
    {% endif %}
    
  3. Build dynamic links or forms:Although AnQiCMS usually handles links automatically, in some cases where highly customized URLs or form submission addresses are required,TableNamethey can be part of building dynamic paths.
  4. The data identifier for integration with other systems:If AnQiCMS needs to interact with external systems,TableNameit can be used as a unique identifier to identify the data type passed to the external system.

Summary

Get the content modelTableNameIs a powerful technique in AnQiCMS template development, it enables us to go beyond simple content display and delve into the model level for more intelligent and dynamic page construction. BymoduleDetailLabel combinationname="TableName"Parameters, we can easily capture this key information and use it in various custom queries and logical judgments, thus building a more flexible and adaptable website.Mastering this skill will undoubtedly greatly enhance your efficiency and creativity in content operation and development on AnQiCMS.


Frequently Asked Questions (FAQ)

Q1: Why do I needTableName, instead of using the model ID or URL alias for conditional judgment?A1: Although the model ID and URL alias can also be used for conditional judgments, butTableNameHas stronger underlying uniqueness and stability. The model ID may change with data migration or import, and the URL alias may also be modified. AndTableNameDirectly corresponds to the database structure, usually does not change easily after the model is created, and is the most stable and lowest-level identifier for identifying the model. When custom logic that is closely related to the database structure is needed, or when precise distinction of model types is required, TableNameIs a more reliable choice.

Q2: Can I useTableNameAre you querying the database directly in the template?A2: The template design philosophy of AnQiCMS abstracts complex database operations into concise and easy-to-use template tags, such asarchiveList/categoryListThis means that you should not attempt to write SQL statements directly in the template or execute database queries.TableNameThe retrieval is mainly used for conditional judgments in template logic, dynamic style loading, and other advanced customizations, or as an identifier to provide model type information to external scripts.All data acquisition and business logic should be completed through the tags or backend controllers provided by AnQiCMS.

Q3:TableNameWhat are the naming rules? Where can I view and modify them?A3: According to the AnQiCMS convention,TableNameMust beEnglish lowercase letters. You can view and modify each model's content management in the AnQiCMS background -> "Content Model".TableNamePlease note that the modificationTableNamewill affect the database table structure, may cause data access exceptions, so please be cautious when operating, and make a backup before modification.