How can you view the internal structure and value of a variable in AnQiCMS templates for debugging?

Calendar 👁️ 70

During the development and content operation of AnQi CMS templates, a deep understanding of the internal structure and specific values of variables in the templates is the key to efficient debugging.When you are faced with abnormal data displayed on a page, or are unsure about the available properties of an object returned by a certain tag, being able to quickly view the details of variables will undoubtedly greatly enhance the efficiency of solving problems.

AnQiCMS provides a flexible and powerful template engine, which follows the syntax of Django templates, and also includes some very practical debugging tools that allow you to directly check variables in template files.Below, we will discuss how to use these tools in the Anqi CMS template to view the internal structure and value of variables.

Core debugging techniques: usingdumpFilter

The Anqi CMS template engine provides a nameddumpThe built-in filter, its function is to print the internal structure, data type, and current actual value of any variable.This is crucial for quickly understanding the content of an unknown variable or the accessible properties of a complex object.

When you encounter an unfamiliar variable in a template or want to know what fields an object (such as fromarchiveListTags retrieved from comments.item) has available, you just need todumpFilter applied to the variable. For example:

{# 假设您想查看一个名为 `someVariable` 的变量的详细信息 #}
<pre>{{ someVariable|dump }}</pre>

{# 或者,在一个循环中查看每个 `item` 的结构 #}
{% archiveList archives with type="list" limit="1 %}
    {% for item in archives %}
        <pre>{{ item|dump }}</pre>
    {% endfor %}
{% endarchiveList %}

todumpAfter the filter is applied, the variable's details will be output directly on the page, including its type (such as*models.ArchiveRepresents a document object) and all the public fields and their current values. Use<pre>Tag wrapping output content can ensure that the formatted text maintains its original formatting in the browser, improving readability.

Further explore: combinestringformatFilter

AlthoughdumpThe filter is already very practical, but in some cases, if you need to view variables in a way that is closer to the source code structure of the Go language (the development language of Anqi CMS), you can combinestringformatFilter with specific formatting parameters%#v. This parameter will output the variable in the form of a Go language struct source code snippet, including its field names and values, which provides more detailed information for developers familiar with Go language or those who need to understand the underlying data model in detail.

The usage is as follows:

{# 使用 stringformat 过滤器以 Go 结构体形式查看变量 #}
<pre>{{ someVariable|stringformat:"%#v" }}</pre>

{# 在循环中对 item 使用 stringformat #}
{% archiveList archives with type="list" limit="1 %}
    {% for item in archives %}
        <pre>{{ item|stringformat:"%#v" }}</pre>
    {% endfor %}
{% endarchiveList %}

stringformat:"%#v"The content will become more "technical", directly showing the Go struct representation of the variable in memory, including the precise types and values of the fields.This is very useful for debugging deep data structures or understanding how data models map to Go code.

Application scenarios and precautions

During the actual template debugging process, you can add these withdumporstringformatTemporary add the code snippet of the filter to any position in the template file, such as in<body>Inside the label, or near the specific code where you suspect the variable may be problematic.

However, there are a few points to pay special attention to when using these debugging tools:

  • Principle of temporalityThese debugging codes should only be used during development and debugging.Once the problem is resolved, be sure to remove it completely from the production environment template.Exposing debug information on an open website may pose a security risk, leak internal system details, and may affect page performance.
  • Visibility of output:dumpandstringformatThe complete information of the variable will be directly output to the HTML page, which may cause the page layout to be temporarily disordered. Use<pre>Tags wrapping the output content is a good habit, which can keep the output content formatted clearly, and not affect the normal display of other elements on the page.
  • Complex object debuggingFor particularly complex nested objects, the output may be very large. At this point, you can try debugging step by step firstdumpThen, according to the output informationdumpThe object or specific field, or using it in a template{% set %}Labels to create intermediate variables, so that we can delve deeper layer by layer.

MasterdumpandstringformatThese powerful filters will greatly enhance your efficiency in AnQiCMS template development and debugging, allowing you to build and maintain websites with greater confidence and composure.

Frequently Asked Questions (FAQ)

  1. Question:dumpFilters andstringformat:"%#v"What are the main differences in the use of filters?Answer:dumpThe filter provides a more readable output, it tries to format the type and value of variables to make them more user-friendly for general users, and to facilitate a quick overview. Whilestringformat:"%#v"It will output the variable in the form of a Go language struct source code snippet, including more precise field names and type information. This is more detailed and direct for developers who need to understand the underlying data model or Go language background in depth. Simply put,dumptending towards “human-readable”,stringformat:"%#v"tending towards “Go code structure”.

  2. Ask: Can I use these filters to view the internal structure of lists (arrays) or dictionaries (maps)?Certainly. Whether it is a list (usually referred to as in the template) or a dictionarysliceorarray) or a dictionarymap)dumpandstringformat:"%#v"Filters can effectively display their internal structure. They list each element of the list or each key-value pair in the dictionary, along with their respective types and values.This is particularly useful for debugging collection type data.

  3. Question: After debugging is completed, how should I deal with the debugging code added in the template?Answer: Be sure to completely remove this debug code from the template after debugging is complete.Leaving them in the production environment may lead to various issues, including: exposing sensitive system internal data, reducing page loading speed, destroying page layout, and even potentially bringing security risks.Therefore, always follow the 'debugging is deleting' principle to ensure that your production template is clean and efficient.

Related articles

What type of value does the `divisibleby` filter in AnQiCMS return in mathematical operations?

In AnQiCMS template creation, we often need to control the display of content based on some mathematical logic, such as determining whether a number can be evenly divided by another number.At this point, the `divisibleby` filter is particularly important.It helps us easily implement this judgment in the template without writing complex logic code.The `divisibleby` filter returns a very intuitive and easy-to-understand boolean value when performing mathematical integer division.This means that the result can only be two possibilities: `True` (true) or

2025-11-08

How to check if a number is divisible by another number in AnQiCMS template?

During the development of AnQiCMS templates, we often encounter situations where we need to adjust the display of content or apply different styles based on specific conditions.For example, when a number in the list meets a certain rule, such as being divisible by 3, we would like it to have a special performance.Lucky enough, AnQiCMS's powerful template engine provides a simple and efficient way to meet this requirement.This article will deeply explore how to flexibly judge whether a number can be evenly divided by another number in the AnQiCMS template, and provide practical code examples in real-world scenarios.### Core Function

2025-11-08

What is the difference between the `default` and `default_if_none` filters for handling null values in AnQiCMS?

In AnQiCMS template development, we often need to handle the case where data may be empty to ensure the stability of page display and user experience.AnQiCMS provides rich template filters to meet such needs, where `default` and `default_if_none` are very practical tools for handling null values.They are both intended to provide a fallback value for missing or empty data, but there is a subtle yet critical difference in the definition of 'empty,' understanding these differences can help us more precisely control the template rendering logic.###

2025-11-08

How to set a default display value for possibly empty variables in AnQiCMS templates?

When using AnQiCMS to build a website, we often encounter such situations: some content fields may not have values every time, such as articles may not have thumbnails, products may not have detailed descriptions, or some contact information may not have been filled in.If variables that may be empty are directly called in the template, ugly blank spaces may appear on the page, or even program errors, which will greatly affect the user experience and the professionalism of the website.Luckyly, the AnQiCMS template system is based on syntax similar to Django and Blade, providing a powerful and flexible mechanism to handle these situations

2025-11-08

The `dump` filter has what help for understanding complex data structures in AnQiCMS template development?

During the template development process of AnQi CMS, we often need to deal with various data passed from the backend.AnQiCMS is a powerful content model with flexible tag system, which allows us to easily obtain articles, categories, pages, and even custom fields of data.However, when the data structure becomes complex, or when we are unsure of what content a variable contains, the efficiency of development debugging will be greatly reduced.At this moment, the `dump` filter acts like a powerful 'data perspective mirror', which can help us clearly understand these complex data structures

2025-11-08

How does the AnQiCMS template escape special characters in HTML or JavaScript code to prevent XSS attacks?

When building a website with AnQiCMS, we often need to fill dynamic content into the page template, which includes text that may come from user input.However, if not handled properly, the content entered by these users may be exploited by malicious attackers to plant malicious scripts, thereby triggering cross-site scripting (XSS) attacks.XSS attacks can steal user data, tamper with page content, even hijack user sessions, causing serious harm to websites and users.AnQiCMS as a content management system that focuses on security

2025-11-08

escape` filter and `e` filter in AnQiCMS template are they functionally the same?

During the development of AnQiCMS templates, the security of data output is a focus we need to pay attention to.Frequently encounter issues about `escape` filter and `e` filter, many users are curious about whether there are differences in their functions.

2025-11-08

How to use the `autoescape` tag to control the automatic escaping of HTML content in AnQiCMS templates?

It is crucial to understand and effectively control the escaping behavior of HTML content during AnQiCMS template development.This not only concerns the correct display of page content, but also directly affects the security of the website, especially in preventing cross-site scripting (XSS) attacks.AnQiCMS's template system provides a flexible mechanism to manage this, with the `autoescape` tag playing a core role.

2025-11-08