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.