In AnQiCMS template development,moduleDetailTags are a very practical tool that allow us to obtain detailed information about website content models (such as articles, products, etc.). Imagine that you are designing a unique display page for a content model and need to retrieve the name, overview, even the table name in the database for the model, at this timemoduleDetailIt can be put to use. However, for developers who are new to it or encounter specific scenarios, how to accurately debugmoduleDetailThe label returns the data structure and field values, which may be a process that requires in-depth understanding.

As an experienced website operations expert, I fully understand the importance of clearly understanding the data flow and structure in the process of template development.AnQiCMS is based on Django template engine syntax, providing an intuitive way to use tags, but to truly "see through" the data returned by the tags, it is essential to master some debugging skills.

UnderstandingmoduleDetailThe role and returned data of the label

First, let's clarifymoduleDetailThe core role of the label. It aims to obtainThe content model itselfThe details, not the details of a specific content (such as an article or a product).A content model in AnQiCMS can have ID, title, unique name (URL alias), keywords, introduction, and corresponding table name in the database, etc., as basic attributes.These are metadata about the "model" rather than the data under the "model".

moduleDetailThe basic usage of tags is usually like this:

{% moduleDetail 变量名称 with name="字段名称" id="1" %}

Or, if you want to get the entire model object instead of a single field:

{% moduleDetail 变量名称 with id="1" %}

Several key parameters need to be paid attention to here:

  • idortoken: Used to specify the specific content model you want to get details of.idIs the model ID,tokenIs the model's URL alias. If the current page context is already a model page, these two parameters can be omitted, and it will automatically obtain the details of the current model.
  • name: If you are only interested in a specific field of the model (such asTitle/Description), you can usename parameter to directly output the value of the field.
  • 变量名称: This is an optional parameter, but it is strongly recommended. It allows you tomoduleDetailThe entire model object returned by the tag (or the value of the specified field) is assigned to a variable so that it can be reused elsewhere in the template or for more complex operations.

moduleDetailThe label can return fields, mainly including according to the internal design of AnQiCMS:

  • Id: The unique identifier ID of the model.
  • Title: The Chinese name displayed in the background of the model (such as "article", "product").
  • Name: The English name or alias of the model, often used in URL paths.
  • Keywords: The keywords of the model.
  • Description: Introduction of the model.
  • Link: Link address of the model.
  • TableName: The corresponding data table name in the database of the model.

The 'secret weapon' for debugging data structure and field values:dumpFilter

In template development, one of the most common needs is to ask "What exactly did I get?" When you are unsuremoduleDetailWhen you need to know what fields the label returns, what the field names are, or what the value types are, AnQiCMS provides a very powerful and convenient debugging tool——dumpfilter.

This filter can print the detailed data structure, type, and current value of any variable in the form of a Go language struct (struct) to the page.This is extremely helpful for us to understand how backend data is organized and passed to the frontend template.

Suppose you want to debug the complete data structure returned by the content model with ID 1. You can do it like this:

  1. tomoduleDetailAssign the returned value to a variable:

    {% moduleDetail myModuleData with id="1" %}
    

    Here, we assign all the data of the model with ID 1 tomyModuleDatathis variable.

  2. UsedumpThe filter prints this variable:

    <pre>{{ myModuleData|dump }}</pre>
    

    To make the output more readable on the page, I suggest wrapping it in<pre>Tags within, this way it can keep the format, avoiding the browser rendering it into a mess.

When you visit the page, the browser will display content similar to this (the specific content will vary depending on the actual model data):

&models.Module{Id:1, Title:"文章", Name:"article", Keywords:"文章,新闻,资讯", Description:"安企CMS文章内容模型", Link:"/article", TableName:"anqi_archive"}

From this output, you can not only seemyModuleDataA variable is amodels.Modulestructured type, and you can clearly see all the field names inside it (Id/Title/NameWait) as well as their current values. This is like taking an X-ray of the data, with all the internal details fully visible.

exceptdumpFilter, you can also usestringformat:"%#v"A filter that can also output the structure and value of variables in the Go language syntax, similar to:

<pre>{{ myModuleData|stringformat:"%#v" }}</pre>

Once you pass throughdumporstringformatThe filter clearly understoodmyModuleDataThe data structure, accessing any field becomes easy. For example, if you want to get the title and table name of the model, you can directly access it using the dot (.) syntax:

{% moduleDetail myModuleData with id="1" %}
<p>模型名称: {{ myModuleData.Title }}</p>
<p>模型在数据库中的表名: {{ myModuleData.TableName }}</p>
<p>模型的URL别名: {{ myModuleData.Name }}</p>

Thus, you can accurately render model data to any position in the template.

Debugging注意事项与实践

  • Remove debugging code in a timely manner: dumporstringformatThe filter outputs a large amount of raw data, which should not appear in a production environment, as it may expose sensitive information and affect page performance and user experience.Therefore, after the development debugging is completed, please be sure to remove the debugging code from your template.
  • Step-by-step debugging:If you encounter a problem in a complex template, you can try to narrow down the debugging scope first. UsedumpView the entire object, then target a specific field, and check the value of that field.
  • Combined with the backend management interface:AnQiCMS backend "Content Model" management interface (help-content-module.mdMentioned) is another important reference for you to understand the model definition and its available fields. Combined with the background configuration information and the front enddumpactual data, it can help you locate the problem faster.

by masteringmoduleDetailThe usage of tags anddumpFilter debugging tips, you will be able to develop AnQiCMS templates with more confidence and efficiency, whether it is to build personalized content display pages or optimize the website's SEO structure, you will be able to handle it with ease.


Frequently Asked Questions (FAQ)

Q1:moduleDetailTags andarchiveDetailWhat are the differences between tags?

A1:This is a common question asked by beginners and also an important concept distinction.moduleDetailTags are used to obtainContent Model (Module)The metadata itself, such as the name, description, and other global information of the "Article" model or "Product" model.archiveDetailTags are used to retrievespecific content items (Archive)detailed data, such as all fields of a specific article or a specific product, including its custom fields. Simply put,moduleDetailFocuses on the definition of 'content type' whilearchiveDetailFocuses on an instance of 'some content'.

Q2:moduleDetailCan the tag return the custom fields I have defined for the content model? For example, I have defined 'color', 'size', and other fields under the 'product' model.

A2: moduleDetailThe tag itselfIt will not return directly.Those "custom fields" you define for the content model (such as the one you mentioned "color", "size"). These custom fields are specifically designed forspecific content items (Archive)Properties other than the content model itself. To obtain the custom fields of a specific content item, you should usearchiveDetailtags (if the specific content item has already been obtained) orarchiveParamstags to obtain.moduleDetailReturns the fixed metadata of the model itself, such as the model name, description, and corresponding database table name, etc.

Q3: Can I fetch all at once?moduleDetailAll fields returned by the tag, not throughnameAre the parameters specified one by one?

A3:Absolutely! In fact, this is the recommended practice when debugging. When you omitnameParameters, and you willmoduleDetailWhen returning an assignment to a variable, the variable will hold the entire content model object, you can access all its public fields through the dot (.) syntax. For example:{% moduleDetail myModule with id="1" %}After that, you can access it like this:{{ myModule.Title }}/{{ myModule.TableName }}This method is more flexible, and it is also convenient to reuse model information in templates.