How to retrieve the database table name (`TableName`) of the content model in a template for a custom query?

Calendar 👁️ 68

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.

Related articles

What is the difference and usage of the `Title` and `Name` fields in the content model in practice?

In the daily operation of AnQiCMS, we often encounter content models and their fields.Among the field names "Title" and "Name", which seem similar, there are completely different responsibilities in practical application.As an experienced website operations expert, I fully understand the importance of understanding these subtle differences for efficient content management, optimizing website structure, and improving user experience.

2025-11-07

If my AnQi CMS is a multi-site deployment, how to use the `moduleDetail` tag to retrieve the content model data of other sites?

AnQi CMS is an enterprise-level content management system designed specifically for small and medium-sized enterprises, self-media, and multi-site management, and its multi-site management capabilities are undoubtedly one of the core highlights.In daily operations, we often encounter such scenarios: we hope to display certain information of the child site on the main site, or to cross-reference content or data models between different sites.Today, let's delve into a specific and practical scenario - how to use the `moduleDetail` tag to obtain content model data from other sites in a multi-site deployed Aiqi CMS.###

2025-11-07

How to retrieve information about a specific model through the content model's URL alias (`token`)?

## How to skillfully use AnQiCMS content model URL alias (`token`): The wisdom of accurately obtaining information As an experienced website operations expert, I am well aware of the importance of an efficient and customizable content management system for enterprise websites and content operation teams.AnQiCMS with its efficient architecture in Go language and versatile functions is gradually becoming a powerful assistant for many operators.In daily content management, we not only strive for the quality presentation of content, but also need efficient data access and management. Today

2025-11-07

I want to display the Chinese title of the current content model in the template, which field of the `moduleDetail` tag should be used??

As an experienced website operations expert, I know that the flexible use of templates in AnQiCMS (AnQiCMS) is the key to improving website operation efficiency and user experience.When faced with the need to dynamically display content model information, such as the Chinese title of the current content model, choosing the correct label field is particularly important.Today, let's delve deeply into how to accurately display the Chinese title of the current content model in the Anqi CMS template.

2025-11-07

Does the `moduleDetail` tag support direct retrieval of custom field values defined in the content model, and if so, how do you call it?

As an experienced website operations expert, I know the importance of a flexible content model and convenient template calls for a modern CMS.AnQiCMS (AnQiCMS) performs well in this aspect, its powerful template tag system makes content presentation flexible and efficient.Today, let's delve deeply into a question that concerns everyone: Does the `moduleDetail` tag support direct retrieval of custom field values defined in the content model?How can it be called if it can? Firstly, let's clarify the division of responsibilities of different tags in the Anqi CMS

2025-11-07

How to determine if the current page belongs to a specific content model (such as 'article model'), and display different content accordingly?

As an experienced website operations expert, I know that flexible content display is crucial for the operational efficiency and user experience of a website.In a powerful content management system like AnQiCMS, understanding how to display differentiated content based on different content models is a key step in achieving refined operation.Today, let's delve deeply into this topic and reveal how AnQiCMS can help us easily achieve this goal.

2025-11-07

When creating a new content model, what are the naming restrictions and practices for the `model table name`?

## Build an Efficient Content Model: The Art and Practice of Naming `Model Table Name` in Anqi CMS As an experienced website operations expert, I fully understand that every detail in a content management system can affect the performance, stability, and future scalability of a website.AnQiCMS (AnQiCMS) provides strong support for building a diverse content structure with its flexible content model features.However, when customizing content models, a seemingly trivial aspect - the naming of the `model table name`, entails many limitations and**practical considerations, worthy of in-depth exploration

2025-11-07

How should the `URL alias` of the content model be set to better support the `{module}` variable in the SEO static rules?

## Unlock AnQiCMS SEO Potential: The Clever Combination of Content Model URL Aliases and Permalinks In today's highly competitive online environment, whether a website can stand out in search engines largely depends on the implementation of its SEO optimization strategy.As an experienced website operations expert, I am well aware of the importance of URL structure for SEO.A clean, meaningful, and SEO-friendly URL is the foundation for improving website ranking.

2025-11-07