During the development and debugging of Anqi CMS templates, we often need to deeply understand the data structure returned by template tags in order to control the display of page content more accurately. Especially likecategoryListSuch a list label, it will loop and output multipleitemobjects, eachitemcarrying rich data. So, how can we quickly and comprehensively viewcategoryListreturneditemWhat fields and their corresponding values are included in the object?Don't worry, as an experienced website operations expert, I will reveal several efficient methods to help you navigate the template world of Anqi CMS with ease.
UnderstandcategoryListThe data structure returned is crucial
categoryListTags in the AnQi CMS are mainly used to obtain category list data, it supports based onmoduleId(Model ID),parentIdParameters such as (parent category ID) filter out the set of categories that meet the conditions. When we use{% for item in categories %}such a loop structure to traverse,itemThis represents the detailed data object for each category. To effectively utilize this data, we first need to know.itemWhat 'treasure' fields are in this object.
Method one: usedumpFilter, clear at a glance (highly recommended)
In the template engine of AnQi CMS, there is a very practicaldumpFilter, its function is to completely print out the structure type and value of a variable. It is a real weapon for debugging dynamic data.
Operation Steps:
- in your template file (for example)
category/list.htmlorindex.htmlcallcategoryListat position(的位置)find{% for item in categories %}inside the loop body. - Add a line of code inside the loop body, to
itemthe object throughdumpfilter output:{% categoryList categories with moduleId="1" parentId="0" %} {% for item in categories %} <pre>{{ item|dump|safe }}</pre> {# 正常展示分类信息的代码,例如: #} <h2><a href="{{ item.Link }}">{{ item.Title }}</a></h2> <p>{{ item.Description }}</p> {# ... 其他分类字段 ... #} {% endfor %} {% endcategoryList %} - Refresh your page. You will see each
itemthe detailed structure of the object and its current value,<pre>wrapped in tags for better readability.
Why recommenddumpFilter?
- Comprehensiveness:It will print out
itemall fields contained in the object, including those internal fields or custom fields that may not be explicitly listed in the documentation. - Real-time:directly reflects the current
itemThe actual data of the object when it is rendered on the page, convenient for troubleshooting data issues. - Intuitiveness:The output format is clear, making it easy to understand the levels of the object and the values of each field.
This needs to be specially explained.|safeThe usage of. IfdumpThe output content of the filter may contain HTML tags (such as some field values being HTML fragments), without|safeMay be displayed as plain text by the browser. AlthoughdumpThe output is typically a variable structure, without malicious HTML, but for safety's sake, or when you see certain characters (such as</>) in the output being escaped, add|safeThe filter ensures that the output content is parsed and displayed as is.
Method two: refer to the official documentation and look for clues as shown in the pictures.
【en】AnQi CMS provides a comprehensive template tag document, which is the most fundamental and authoritative reference material for our template development.categoryListTag returnsitemObject, the document explicitly lists the commonly used available fields.
Path to check:
- In the Anqi CMS help document, find the 'Category Page Tag' section under 'Template Call Tags'.
- Click to enter the "Category List Tag"
categoryListdetailed description page (i.e.,)tag-categoryList.md). - In this page, you can find the "
itemThis section is for the variables within the for loop, the available fields are: "
The document usually lists the following key fields:
Id: The unique identifier ID of the category.Title: The title name of the category.Link: The access link of the category.Description: The description information of the category.ContentCategory details (if filled in by the backend).ParentId: The ID of the parent category, used to build the hierarchy relationship.Logo: The thumbnail large image address of the category.Thumb: The thumbnail address of the category.Spacer:Used to display the prefix of the subcategories of the category level (for example, indentation symbols).HasChildren:A boolean value indicating whether the category has subcategories.IsCurrentAn boolean value indicating whether this category is the current category of the visited page.ArchiveCountThe number of documents contained under this category.
The advantage of this method is:
- Authoritativeness:The document is the most accurate field definition.
- Purposefulness:Fields usually have clear meanings and usage descriptions.
However, the document may not cover all internal fields or additional fields generated under specific versions or custom models. Therefore, combiningdumpfilter usage, can provide a more comprehensive view.
Tips in practical applications
- Conditional judgment:After obtaining the field, you can use
{% if item.HasChildren %}This method to determine whether there is a subcategory, thereby deciding whether to display the submenu or perform other operations. - Data formatting:For
ContentField, if it contains Markdown content, you can refer totag-categoryDetail.mdmentionedrender=trueparameter to control rendering. For image links, you can work with|thumbfilter to generate thumbnails. - Dynamic display:Use flexibly
item.Linkanditem.Titlefields, to build dynamic category navigation and content list.
By combining the use of the above two methods, you will be able to obtain quickly and accurately.categoryListreturneditemAll available fields and values of the object, thus more efficiently carrying out the development and debugging of security CMS templates.
Common Questions and Answers (FAQ)
Q1: Why do I usedumpAfter the filter, is the output result garbled or incomplete?
A1: If the output results in garbled text, please first check if your template file is saved in UTF-8 encoding format.The default requirement of AnQi CMS is to use UTF-8 encoding for template files.Secondly, if the output result is incomplete, it may be due to the output content being too long or containing certain special characters that cause the browser to parse an exception.dumpPaste the output content into a code editor (such as VS Code), as they usually handle long text and special characters better. Additionally, make sure you are using|safeFilter to prevent potential HTML escaping issues.
Q2:categoryListreturneditemthe object ofContentField, how to control Markdown rendering?
A2:categoryListreturneditem.ContentThe field itself only contains the original text content. If you want the content rendered in Markdown to be displayed in the category list, you need to likecategoryDetailthe tag, in the way of obtainingContentAdd at 【en】timerender=trueParameters. For example, you can first go through 【en】categoryDetailto get the Markdown rendering content of a specific category, or in 【en】categoryListloop, for each 【en】item, and then use 【en】categoryDetailLabel and pass initem.Idto get one with rendering optionsContent.
Q3: BesidescategoryList, other list labels (such asarchiveList/pageList) ofitemIs there a similar method to check for object fields?
A3: Yes, it's completely applicable!dumpThe filter is a general feature of the Anqi CMS template engine, applicable to any variable whose internal structure and values you want to view. Whether it isarchiveListreturnedarchivean object, orpageListreturnedpageObject, you can use it inside the loop body.{{ item|dump|safe }}To view all its available fields. You can also likecategoryListcheck the official documentation of the corresponding tag (such astag-archiveList.md/tag-pageList.md) to get itsitemThe list of object fields. This will greatly accelerate your debugging efficiency in the AnQi CMS template development.