During the development of AnQi CMS templates and content operations, 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.
AutoCMS provides a flexible and powerful template engine, which borrows the syntax of Django templates and also includes some very practical debugging tools, allowing you to directly check variables in the template file.Below, we will discuss how to use these tools in the Anqi CMS template to view the internal structure and values of variables.
Core debugging techniques: usingdumpFilter
The template engine of Anqi CMS provides a name calleddumpThe built-in filter, its function is to print out 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 a certain object (such as fromarchiveListthe tags you getitem) has available, just simply need todumpThe filter is applied to this 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 detailed information of the variable will be directly output on the page, including its type (for example*models.ArchiveRepresents a document object) and all its public fields with their current values.<pre>Label wrapping output content, ensuring that the formatted text maintains its original layout in the browser, enhancing readability.
Further view: CombinedstringformatFilter
AlthoughdumpThe filter is 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 and specific formatting parameters%#v.This parameter will output the variable in the form of a Go language struct source code snippet, including its field name and value. This provides more detailed information for developers familiar with the Go language or users who need to understand the underlying data model precisely.
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 output content will be 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
In the actual template debugging process, you can add these withdumporstringformatTemporary filter code snippets to any position in the template file, for example,<body>Label inside, or near the specific code where you suspect the variable may be causing issues.
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 issue is resolved, it must be completely removed from the template in the production environment.Exposing debug information on publicly accessible websites may pose security risks, 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>It is a good habit to wrap output content with labels, which can keep the format of the output content clear and not affect the normal display of other elements on the page. - Debugging complex objects:For particularly complex nested objects, the output may be very large. At this point, you can try step-by-step debugging, first
dumpthe parent object, and then according to the output informationdumpAn object or specific field, or using it in a template.{% set %}Tags 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.
Common Questions (FAQ)
Q:
dumpfilters andstringformat:"%#v"What are the main differences in usage of the filter?Answer:dumpThe filter provides a more readable output, it tries to format the type and value of variables to make them more user-friendly and convenient for a quick overview.stringformat:"%#v"Then 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 have a background in Go language. In simple terms,dumpTending towards “human-readable”.stringformat:"%#v"Tending towards “Go code structure”.Q: Can I use these filters to view the internal structure of a list (array) or a dictionary (map)?答:完全可以。无论是列表(在模板中通常称为 English),
sliceorarray)还是字典(map)dumpandstringformat:"%#v"Filters can effectively display their internal structure.They will list each element of the list or each key-value pair of the dictionary, along with their respective types and values.This is particularly useful for debugging data of collection types.问:调试完成后,我应该怎么处理这些在模板中添加的调试代码?答:After debugging is completed, you must completely remove these debug codes from the template.Keeping them in the production environment may cause various issues, including: exposing sensitive internal system data, reducing page loading speed, destroying page layout, and even possibly bringing security risks.Therefore, always follow the principle of 'Debugging is deleting' to ensure that your production template is clean and efficient.