During the template development process of AnQiCMS, we often need to understand which data and structure a variable contains internally, especially when dealing with complex data objects or debugging template issues.Sometimes direct output of a variable can only yield a simple value or an error message, and it is not possible to delve into its detailed composition.It is particularly important to have some effective methods to view the complete structure and value of variables at this time.
Challenges of AnQiCMS template debugging
AnQiCMS's template syntax is flexible and variable, whether it is the built-in systemarchive/categoryobject, or custom model fields, even various list loopsitemThey may be more complex in structure than we imagine.When the template rendering occurs an exception, or when accessing an unfamiliar variable property, it is difficult to accurately locate the problem or write correct calling code if we cannot clearly see the internal structure of the variable.
core tools:dumpFilter
To solve this problem, AnQiCMS provides a very practical built-in filter —dump.It can help us quickly delve into the internal workings of variables, printing out the structure type of any variable in the Go language, the names of all its fields, and their current values in detailed text form.
UsedumpThe method of the filter is very simple, just add it after the variable you want to check|dumpas follows:
{# 假设您要查看名为 `myVariable` 的变量的详细信息 #}
{{ myVariable|dump }}
For example, if you want to view a certain element in a loop list,itemyou can write it like this:
{% for item in archives %}
<p>当前文章项的详细信息:{{ item|dump }}</p>
{# 正常展示文章标题 #}
<h3>{{ item.Title }}</h3>
{% endfor %}
You will see output content similar to this (BannerItemfor example):
&config.BannerItem{Logo:"http://127.0.0.1:8001/uploads/202309/14/eba5aa9dc4c45d18.webp", Id:1, Link:"", Alt:"Hello", Description:"", Type:"default"}
This line clearly shows the Go language struct type of the variableconfig.BannerItem), as well as the names and current values of all its fields, for exampleLogo/Id/Linketc. Through this information, you can accurately know which accessible properties the variable contains, as well as their current values, thus making it easy to write{{ item.Logo }}or{{ item.Id }}etc. call code.
Advanced Techniques: Combine with other tags and filters
dumpWhile filters are powerful, combining them with other features of AnQiCMS templates can make the debugging process more efficient.
Utilize
{% set %}Label assignment temporary variableWhen you are faced with a very complex nested variable and want to analyze one sub-item separately,{% set %}Tags come into play. You can assign a part of a complex variable to a temporary variable, and then use this temporary variable.dumpFilter for checking.{# 假设有一个复杂的变量 `archive.Category.Parent` #} {% set parentCategory = archive.Category.Parent %} <p>父分类详细信息:{{ parentCategory|dump }}</p>In
forTo batch view list items in a loop.In processing a list (such asarchiveListreturnedarchives) we may want to view the detailed structure of eachitemobject. You can place adumpfilter insideforCheck each list item individually inside the loop.{% archiveList archives with type="list" limit="3" %} {% for item in archives %} <div style="border: 1px solid #ccc; margin-bottom: 10px; padding: 5px;"> <h4>文章标题: {{ item.Title }}</h4> <p>调试信息: {{ item|dump }}</p> </div> {% endfor %} {% endarchiveList %}Combine
ifDebug conditionally using labels.To debug more precisely, you can putdumpFilter is related toifLogical judgment tags combined use. For example, only output debug information under specific conditions or when the current user is an administrator, which is particularly useful in multi-site or multi-user scenarios.{# 假设您有一个布尔变量 `debugMode` 用于控制调试信息输出 #} {% if debugMode %} <p>页面关键变量调试: {{ pageInfo|dump }}</p> {% endif %}Or output under a specific document ID:
{% if archive.Id == 10 %} <p>ID 为 10 的文档详细信息: {{ archive|dump }}</p> {% endif %}Explore custom fields:
archiveParamsandcategoryDetailofExtraFieldAnQiCMS allows you to define custom fields for content models and categories. In the template, you can output custom parameters of documents in a loop through{% archiveParams params %}tags or by{% categoryDetail extras with name="Extra" %}To get the custom fields of the category. If you are not sure about the specific values or structure of these custom fields, you can also combinedumpFilter for checking.{% archiveParams params %} {% for item in params %} <p>自定义字段 {{ item.Name }} 的值: {{ item.Value }}</p> {# 如果 item.Value 也是一个复杂对象,可以进一步 dump #} {# <p>自定义字段 {{ item.Name }} 的详细结构: {{ item.Value|dump }}</p> #} {% endfor %} {% endarchiveParams %}
Debugging Tips
- Clean up in time:Ensure that the template is removed after debugging is completed
dumpLabel. It is used during the development phase and is not suitable for display in the user-accessible production environment to avoid leaking sensitive information or affecting user experience. - Local inspection:Avoid putting everything at the top of the page
dumpToo many variables. This will lead to an excessive amount of output information, making it difficult to locate. It is recommended to start from the local part of the page and gradually narrow down the scope of investigation. - Combine browser developer