How to quickly view the detailed structure and value of complex variables during debugging in AnQiCMS template?

Calendar 👁️ 64

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:"https://en.anqicms.com/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

Related articles

In AnQiCMS template, how to determine if one number can be evenly divided by another to achieve conditional display?

In Anqi CMS template development, we often need to display content based on specific conditions, such as adding a special style to every few elements in a list or inserting separators at specific positions.When this condition is to determine whether a number can be divided by another number, Anqi CMS provides a concise and efficient solution with its powerful template engine.The Anqi CMS template system uses a syntax similar to the Django template engine, which makes it very intuitive in handling such logical judgments.To determine if a number can be evenly divided by another number

2025-11-08

What are the differences and applicable scenarios between the `default` and `default_if_none` filters when the template variable is empty?

In AnQi CMS template design, reasonably handling variables that may be empty is the key to ensuring the integrity and smooth user experience of website content display.When a template variable has no value or is in an 'empty' state, we usually do not want blank or error messages to appear on the page, but rather we would like to display a preset default content.At this time, the `default` and `default_if_none` filters provided by Anqicms come into play.They can all provide default values for variables

2025-11-08

How to format Unix timestamp into a readable date and time format in AnQiCMS template?

In website content management, time information plays a crucial role, whether it is the publication date of articles, update time, or the submission time of comments, the record of time is indispensable.Databases usually store these time data in a concise and efficient format - Unix timestamps.However, for the end user, a string of numbers in a timestamp is not as intuitive and easy to understand as “October 27, 2023 14:30”.

2025-11-08

How to remove specific characters or extra spaces from AnQiCMS template variables?

In website content operations, we often encounter such situations: the template variable content retrieved from the database may contain some unnecessary characters, extra spaces, or formats that are not satisfactory.These subtle details, if not handled properly, may affect the aesthetics of the page, user experience, and even cause interference with search engine optimization (SEO).AnQiCMS provides powerful template filtering functionality, helping us easily clean and format these variables.The template syntax of AnQiCMS is similar to Django engine

2025-11-08

The `escape` and `escapejs` filters in AnQiCMS are applicable to which HTML/JS escaping scenarios?

In AnQiCMS template development, it is very important to understand and properly use escape filters to ensure website security and correct content display.The system uses a template engine syntax similar to Django, which means it takes some security measures by default when handling variable output.Today, let's talk about the `escape` and `escapejs` filters to see in which scenarios they can be used.

2025-11-08

How to split a line of text content (such as a tag string) into an array of individual words for processing in AnQiCMS?

In the practice of AnQiCMS content management, we often encounter scenarios where it is necessary to split a seemingly simple line of text into smaller, more independent 'words' for fine-grained processing.For example, the document tag (Tag), keyword list, or multiple values separated by a specific symbol in custom fields.The core of this requirement lies in converting a string into an array that can be traversed and manipulated individually.

2025-11-08

How to precisely control the display of floating-point numbers in the AnQiCMS template, such as retaining two decimal places?

In website content operation, the way numbers are presented often affects user experience and the accuracy of information.Especially for floating-point numbers, such as product prices, statistics, and ratings, it is common to require accuracy to several decimal places or rounding according to business needs.AnQiCMS is a powerful template system that provides us with a flexible way to handle these data.Today, let's delve into how to efficiently and accurately control the display of floating-point numbers in the AnQiCMS template.

2025-11-08

How to extract specific number information from a long numeric string in AnQiCMS template?

In website operations, we often encounter scenarios where we need to handle some structured long numeric string.For example, a product code may contain the production date and batch information; an order number may imply regional codes and serial numbers; or may be a unique identifier generated by specific business logic.These long digital strings often carry rich metadata, and we may only need the numerical information at specific positions for display, filtering, or further processing.

2025-11-08