During AnQiCMS template development, we often need to understand what data and structure a variable contains internally, especially when dealing with complex data objects or debugging template issues.Sometimes a direct output of a variable can only yield a simple value or error message, unable to delve into its detailed composition.At this point, it is particularly important to master some effective methods to view the complete structure and value of variables.

The challenge of AnQiCMS template debugging

AnQiCMS's template syntax is versatile, whether it is built into the system or notarchive/categoryAn object, or a custom model field, or even various list loops initemTheir data structures may be more complex than we imagine.When a template rendering occurs an exception, or when accessing an unfamiliar variable attribute, it is difficult to accurately locate the problem or write the correct calling code if we cannot clearly see the internal structure of the variable.

Core tool:dumpFilter

To solve this problem, AnQiCMS provides a very practical built-in filter—dumpIt can help us quickly understand the internal structure of variables, printing the type of any variable's Go language struct, the names of all its fields, and its current values in detailed text form.

UsedumpThe method of the filter is very simple, just add it after the variable you want to check|dumpand it is done:

{# 假设您要查看名为 `myVariable` 的变量的详细信息 #}
{{ myVariable|dump }}

For example, if you want to view a certainitemspecific structure in a loop, you can write it like this:

{% for item in archives %}
    <p>当前文章项的详细信息:{{ item|dump }}</p>
    {# 正常展示文章标题 #}
    <h3>{{ item.Title }}</h3>
{% endfor %}

You will see output similar to this (withBannerItemfor 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 variable's Go language struct type (config.BannerItem),as well as the names and current values of all fields within it, for exampleLogo/Id/LinkWait. Through this information, you can accurately know which accessible properties the variable contains, and what their current values are, so that you can easily write{{ item.Logo }}or{{ item.Id }}Call code.

Advanced Techniques: Combine with other tags and filters

dumpAlthough powerful, filters can make the debugging process more efficient when used in conjunction with other features of the AnQiCMS template.

  1. Utilize{% set %}Label assignment temporary variableWhen you are faced with a very complex nested variable, and you want to analyze one of its sub-items separately,{% set %}Tags come in handy. You can assign a part of a complex variable to a temporary variable and then use that temporary variable fordump.

    {# 假设有一个复杂的变量 `archive.Category.Parent` #}
    {% set parentCategory = archive.Category.Parent %}
    <p>父分类详细信息:{{ parentCategory|dump }}</p>
    
  2. InforViewing list items in batches in a loopWhen processing a list (such asarchiveListreturnedarchivesWhenitemwe might want to view eachdumpThe filter is placedforitem in the list. You can check

    {% 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 %}
    
  3. Combineifeach item conditionally by using tags.In order to debug more accurately, you candumpthe filter meetsifLogical judgment labels are used together. For example, debug information is only output under specific conditions or only when the current user is an administrator, which is especially useful in multi-site or multi-user scenarios.

    {# 假设您有一个布尔变量 `debugMode` 用于控制调试信息输出 #}
    {% if debugMode %}
        <p>页面关键变量调试: {{ pageInfo|dump }}</p>
    {% endif %}
    

    Or it will only be output under a specific document ID:

    {% if archive.Id == 10 %}
        <p>ID 为 10 的文档详细信息: {{ archive|dump }}</p>
    {% endif %}
    
  4. Explore custom fields:archiveParamsandcategoryDetailofExtrafieldAnQiCMS allows you to define custom fields for content models and categories. In templates, you can output custom parameters of documents by{% archiveParams params %}tags, or by{% categoryDetail extras with name="Extra" %}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 combinedump.

    {% 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:Remove after debuggingdumpLabel. It is used during the development phase and is not suitable for display in user-accessible production environments to avoid leaking sensitive information or affecting user experience.
  • Local troubleshooting:Avoid doing everything at once at the top of the pagedumpToo many variables. This will cause the output information to be too large and difficult to locate. It is recommended to start from the local page and gradually narrow down the troubleshooting scope.
  • Combine browser developers