As an experienced website operations expert, I deeply understand the powerful capabilities brought by the flexible content model in AnQiCMS.很多时候,我们不仅需要展示内容本身,还需要根据内容的“类型”来驱动更深层次的页面逻辑或样式。TableNameThus, achieve more fine-grained custom control.


How to obtain the database table name (TableName) of the content model in the Anqi CMS template: In-depth analysis and practical application

AnQiCMS is renowned for its highly customizable content model.We all know that we can create various content models such as "articlesEnglish 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, as it may not obtain the correspondingTableNameit is particularly critical.

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

core tools:moduleDetailThe magic use of tags

To obtain the database table name of the content model, we mainly rely on the AnQiCMS providedmoduleDetailTemplate tag. This tag is specifically used to retrieve detailed information about the content model, which includes the information we needTableName.

The basic usage 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 not define a variable name and output directly.
  • with name="TableName": This is the key point, we explicitly tell the system which fields need to be obtained.TableName.

How to get the current page's content model table name

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 for the currentTableNameAt this point,moduleDetail标签会自动识别当前页面的上下文:English

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

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

在上述例子中,如果当前页面是“文章”模型,其数据库表名通常会是EnglisharticleThis can be viewed and configured in the background "Content Management" -> "Content Model".{{ currentModelTableName }}it will 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 (for example, the "product" model). At this time, we can go through the model'sIDorURL别名(token)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 model.TableName.

TableNameThe practical application scenarios of

Get toTableNameAfter that, we can implement a variety of flexible applications in the template:

  1. Dynamic style and script loading:According to differentTableNameTo load different CSS classes or JavaScript files for personalized page performance. For example, loading the article model pagearticle-specific.cssTo load 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. Condition rendering content block:Display or hide specific content areas or feature modules based on the model type in a shared template layout.
    
    {% 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 scenarios that require highly customized URLs or form submission addresses,TableNameit can be used as part of building dynamic paths.
  4. Identifiers for data integration with other systems:If AnQiCMS needs to interact with external systems,TableNameit can serve as a unique identifier passed to the external system to recognize data types.

Summary

Get the content modelTableNameIs a powerful technique in AnQiCMS template development, which enables us to go beyond simple content display, delve into the model level for more intelligent and dynamic page construction.moduleDetailTag 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 AnQiCMS content operation and development.


Common Questions (FAQ)

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

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

Q3:TableNameWhat is the naming convention? Where can I view and modify it?A3: According to the AnQiCMS convention,TableNamemust bein English lowercase letters. You can view and modify each model in the "Content Management" -> "Content Model" section of the AnQiCMS backend.TableNameBut please note, modifyingTableNamewill affect the database table structure, may cause data access exceptions, so please operate with caution and make a data backup before modifying.