During the development and debugging of AnQi CMS templates, we often need to gain a deep understanding of the data structure returned by template tags in order to control the display of page content more accurately. Especially likecategoryListsuch list tags, it will loop and output multiple timesitemobjects, eachitemthey carry 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 Anqi CMS template world with ease.
UnderstandcategoryListthe returned data structure is crucial
categoryListThe tag in AnQi CMS is mainly used to obtain the list of category data, it supports filtering out the category set that meets the conditions according to parameters such asmoduleId(Model ID),parentId(parent category ID) and others. When we use{% for item in categories %}such loop structure while iterating,itemrepresents the detailed data object of each category. To effectively utilize these data, we first need to knowitemWhat treasures are in this object.
Method one: utilizedumpFilter, see everything at a glance (highly recommended)
In the Anqi CMS template engine, a very practical one is built in.dumpA filter that prints the structure type and value of a variable in full. It is a real lifesaver for debugging dynamic data.
Operation steps:
- In your template file (for example
category/list.htmlorindex.htmlcall incategoryListat position) find{% for item in categories %}inside the loop body. - Add a line of code inside the loop to
itempass the object throughdumpa filter 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
itemobject's detailed structure and current value, they are<pre>Label wrapping for better readability.
Why recommenddumpFilter?
- Comprehensiveness:It will print out
itemAll fields included in the object, including those internal fields or custom fields that may not be explicitly listed in the document. - Real-time:Directly reflects the current
itemActual data of the object at page rendering, convenient for troubleshooting data issues. - Intuitiveness:Clear output format, easy to understand the level of the object and the values of each field.
Special attention is needed here.|safeThe usage. IfdumpThe filter output may contain HTML tags (for example, some field values are themselves HTML fragments), without adding|safeMay be displayed as plain text by the browser. AlthoughdumpThe output is typically a variable structure, free of malicious HTML, but for safety's sake, or when you see certain characters in the output (such as</>), add the|safeThe filter ensures that the output content is parsed and displayed as is.
Method two: refer to the official documentation and follow the instructions.
AnQi CMS provides a comprehensive template tag document, which is the most fundamental and authoritative reference for our template development. Specifically,categoryListthe tags returned byitemObject, the document clearly lists the commonly used available fields.
Check the path:
- In the Anqi CMS help document, find the "template call tags" under the "category page tags" section.
- Click to enter the 'Category List Tag'
categoryListDetailed description page (i.e.,tag-/anqiapi-category/151.html) - You can find in this page, such as “
itemAvailable fields for the variable in the for loop body
The document usually lists the following key fields:
Id: The unique identifier ID of the category.Title: The title name of the category.Link: Category access link.DescriptionCategory description.Content: Detailed content of the category (if filled in on the back end).ParentId: The ID of the parent category, used to build the hierarchical relationship.LogoThe thumbnail large image address of the category.ThumbCategory thumbnail image address.SpacerUsed to display the prefix of the sub-category at the classification level (e.g., indentation symbol).HasChildrenA boolean value indicating whether the category has a sub-category.IsCurrentA boolean value indicating whether the category is the category of the current visited page.ArchiveCountThe number of documents contained under the category.
The advantage of this method lies in:
- Authoritativeness:The document is the most accurate field definition.
- Purpose: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, combinedumpFilter usage can provide a broader perspective.
Tips in practical application
- Conditional judgment:After obtaining the field, it can be utilized
{% if item.HasChildren %}This way to judge whether there is a subcategory, thus deciding whether to display the submenu or perform other operations. - Data formatting:For
Contentfield, if it contains Markdown content, you can refer totag-/anqiapi-category/152.htmlmentionedrender=trueparameters to control rendering. For image links, you can use|thumbfilter to generate thumbnails. - dynamic display:Flexible application
item.Linkanditem.Titlefields, 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.
Frequently Asked Questions (FAQ)
Q1: Why do I usedumpAfter the filter, the output result is garbled or incomplete?
A1: If the output results in garbled text, first check if your template file is saved in UTF-8 encoding format.The AnQi CMS defaults to using 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 some special characters that cause the browser to parse errors.You can try todumpPaste the output content into a code editor (such as VS Code). They usually handle long text and special characters better. Also, make sure you are using|safeFilter to prevent potential HTML escaping issues.
Q2:categoryListreturneditemWithin the objectContentField, how to control Markdown rendering?
A2:categoryListreturneditem.ContentThe field itself only contains the original text content. If you want to display the content rendered with Markdown in a category list, you need to usecategoryDetailthe tag as follows to obtainContentwhen callingrender=trueThe parameter. For example, you can first go throughcategoryDetailto get the Markdown rendering content of a specific category, orcategoryListin the loop, for eachitemuse first, and then usecategoryDetailLabel and pass in.item.Idto get the rendering options withContent.
Q3: BesidescategoryListsuch as other list labelsarchiveList/pageList)的itemDo object fields also have similar methods to view?
A3: Yes, it is fully applicable!dumpThe filter is a general feature of Anqi CMS template engine, applicable to any variable whose internal structure and value you want to view. Whether it isarchiveListreturnedarchivean object orpageListreturnedpageObject, you can use it inside the loop{{ item|dump|safe }}to view all available fields. At the same time, you can also likecategoryListto consult the official documentation of the corresponding tag (such astag-/anqiapi-archive/141.html/tag-/anqiapi-category/154.html) to get itsitemThe field list of the object. This will greatly accelerate your debugging efficiency in the Anqi CMS template development.