How to debug and view the complete structure and value of variables in the template?

Calendar 👁️ 69

When developing website templates in AnQi CMS, we often need to understand the specific structure and data contained in a variable on the page in order to accurately call its properties.When dealing with complex business logic or unfamiliar template variables, being able to quickly view the complete structure and value of variables is undoubtedly the key to improving development efficiency.AnQi CMS based on Django template engine syntax features provide us with a simple and powerful variable debugging means.

Core tool:dumpFilter

AnQi CMS template provides a nameddumpThe powerful filter, which can directly output the complete structure, data type, and current value of variables in the template, helping us to clearly grasp the full picture of variables. When you encounter an uncertain variable in a template file, and want to know what fields it contains internally, or what the actual value of a specific field is, dumpThe filter can be used.

It is very simple to use, you just need to add it after the variable you want to view|dumpFor example, if you want to view the core document variable of the current pagearchiveThe details can be temporarily added to the template like this:

{# 临时添加,用于调试,查看 archive 变量的完整结构和值 #}
<pre>{{ archive | dump }}</pre>

Some notes:Here we usually use<pre>tags to wrap{{ ... | dump }}because the output isdumpThe filter will return a formatted text using<pre>Tags can preserve the original newlines and spaces for clearer and more readable output.

After you save the template file and refresh the page, you will see detailed output similar to the Go language struct, which includesarchiveThe names of all fields, their data types, and their current values. For example:

&models.Archive{Id:101, Title:"安企CMS模板调试指南", SeoTitle:"", Link:"/article/101.html", Keywords:"模板调试,变量查看", Description:"本文将详细介绍如何在安企CMS模板中调试查看变量的完整结构和值。", Content:"...", ModuleId:1, CategoryId:5, CanonicalUrl:"", UserId:1, ParentId:0, Price:0.00, Stock:1, ReadLevel:0, OriginUrl:"", Views:120, Flag:"h", CreatedTime:1678886400, UpdatedTime:1678886400, Status:1, Sort:10, Tags:[], Category:models.Category{Id:5, Title:"开发教程", ...}}

Through this output, you can clearly seearchiveA variable is amodels.ArchiveThe structure of the type, and understand itsId/Title/Link/Viewsand its associatedCategoryand all properties and their corresponding values.

a more precise view:stringformatthe filter meets%#v

AlthoughdumpThe filter is very useful, but in some cases, you may need to be closer to the original output of the backend Go language struct definition, or want to control the output format more accurately. At this point,stringformatFilter combined with Go language formatting verbs%#vIt would be another good choice.

%#vThe formatting verb can output the Go syntax representation of values in Go language, which includes type names and field names, and can provide a clearer structure view for developers familiar with Go.

You can use it like this:

{# 临时添加,用于调试,查看 item 变量的 Go 语言语法表示 #}
<pre>{{ item | stringformat:"%#v" }}</pre>

for example in aarchiveListofforloop, you want to view eachitemvariable's detailed structure:

{% archiveList archives with type="list" limit="3" %}
    {% for item in archives %}
        <h3>{{ item.Title }}</h3>
        {# 查看当前循环中的 item 变量的 Go 语法表示 #}
        <pre>{{ item | stringformat:"%#v" }}</pre>
    {% endfor %}
{% endarchiveList %}

After refreshing the page, you will see eachitemVariables are output in the form of Go struct code snippets, which is very helpful for understanding the backend data model and variable types.

Debugging practice: Step-by-step variable inspection.

Mastereddumpandstringformat:"%#v"After these two tools, the actual debugging process will become very intuitive:

  1. Determine the target variable:First, make it clear which variable you want to view. It may be a page-level variable (such asarchive/category),may also be an element in a loop (such asfor item in archivesofitem),or a nested property of a variable (such asitem.Category)
  2. Insert debugging code:In the template file (usually intemplateUnder the directory.htmlIn the file, find the location of the variable you wish to view and temporarily insert{{ 你的变量 | dump }}or{{ 你的变量 | stringformat:"%#v" }}. If you are debugging in a loop, make sure to place the code inside the loop to view the specific values of each element.
  3. Save and refresh the page:Save the modified template file and then refresh the corresponding page in the browser.
  4. Analysis output:Check the structure and value of the output variables on the page. Based on the output, you can confirm the existence of the variables, the fields they contain, the data types of the fields, and their current values.
  5. Adjust as needed:Adjust your template code based on the analysis results, correctly call the properties of the variables.
  6. Remove debug code: An extremely important step!After debugging is complete, be sure to delete all debugging code from the template.Debug code should not be retained in the final production environment, as it may expose sensitive information, affect page performance, or even lead to security issues.

If you edit the template directly in the background, this process will be more convenient, and the modification and viewing effects will be almost instantaneously synchronized.

Points to note

  • Temporary Operations:Temporary debug code, please remember to delete or comment it out immediately after debugging.
  • Page layout affects: Inserting debug output into the template may temporarily disrupt the original layout and style of the page, which is normal during the debugging process.
  • Sensitive information: dumpandstringformatThe entire content of the variable will be output, if the variable contains sensitive information such as user passwords, database connection strings, etc., please debug in a secure environment and ensure that the debugging code will not leak to the outside.

By flexible applicationdumpandstringformatFilter, debugging work for AnQi CMS templates will become more efficient and transparent than ever before.No matter how complex the data structure you face, these two tools can help you quickly locate the problem and build a stable and functional website.


###

Related articles

How to determine if a string or array contains specific content in AnqiCMS?

In the daily work of content operation, we often need to make judgments and displays based on specific information of the content.For example, if a blog post mentions a hot keyword, we may want to give it a special tag; or if a product description includes a certain feature, we want to adjust its display style accordingly.The template engine of AnQiCMS provides a flexible and intuitive way to determine whether a string or array contains specific content, making dynamic content display and personalized processing very convenient.

2025-11-08

How to convert an array to a string with a delimiter, or split a string into an array?

In AnQi CMS template development, we often encounter situations where we need to handle data format conversion, especially when combining multiple values into a string or splitting a string containing multiple values into independent data.This is especially common when dealing with tags, multi-select properties, or extracting information from custom fields. 幸运的是,AnQi CMS built-in very practical template filters, can help us easily achieve the conversion between arrays and string with delimiters.

2025-11-08

How to perform simple number or string concatenation in AnqiCMS template?

In Anqi CMS template design, flexibly handling and displaying data is the key to building a dynamic website.This is a common need in daily development to concatenate numbers or strings.AnQi CMS provides various ways to achieve this goal, from simple direct combinations to powerful filters, which can meet your different content assembly scenarios.Understanding the Basics of Concatenation: Direct Output Equivalent to "Addition" In Anqi CMS template syntax, the most intuitive way to concatenate strings is to place multiple variables or text directly side by side. For example

2025-11-08

How to remove leading and trailing spaces or specified characters from a string?

In website content management, string formatting is often a problem we need to solve when displaying content and handling data.Especially in user input, data import, or system-generated content, strings may have extraneous spaces at the beginning and end, or specific characters may need to be removed to meet expected display effects or data standards.AnQiCMS as an efficient and flexible content management system, through its powerful template engine and rich filters, provides us with concise and powerful string processing capabilities, allowing such tasks to be easily completed at the template level.

2025-11-08

How to display the comment form and submit comments in Anqi CMS?

It is crucial to establish an effective communication channel with visitors in website operation, and the message function is one of the key tools to achieve this goal.It can not only help websites collect user feedback and answer questions, but also promote user participation, enhance the interactivity and stickiness of the website.For users using AnQiCMS, the built-in message function provides a convenient and flexible solution, allowing you to easily display and submit message forms without writing complex code.### Overview of AnQi CMS Message Function The AnQi CMS message function is designed to be very user-friendly

2025-11-08

How to display the document comment list and implement pagination?

When using Anqi CMS to manage website content, the comment function on the document detail page is an important part of interacting with readers.Effectively display these comments and ensure that the page maintains good loading speed and user experience as the number of comments increases, it is necessary to properly handle the display and pagination of the comment list.The Anqi CMS provides very intuitive and powerful template tags, helping us easily achieve this goal.

2025-11-08

How to implement the like function for comments in AnQi CMS?

In website operation, enhancing user interaction is a key factor in maintaining the vitality of the website and increasing user stickiness.The comment section is an important battlefield for users to express their opinions and interact with each other. Adding a like feature would undoubtedly significantly enhance the interactivity and community atmosphere of the content.The Anqi CMS is an efficient, customizable, and feature-rich enterprise-level content management system that provides flexible mechanisms, allowing users to easily add the popular interactive feature of liking comments in the comment section.This article will give a detailed explanation of how to implement the like function for comments in Anqi CMS.

2025-11-08

How to directly output HTML code without being automatically escaped in AnQiCMS templates?

In AnQiCMS templates, we often need to flexibly control the way content is displayed.You may have encountered such a situation: you carefully formatted some content containing HTML tags in the rich text editor on the back end, but when the output was displayed on the front page, the tags were displayed as plain text, rather than rendered according to the HTML structure.This is actually a security mechanism that is in operation by default in AnQiCMS and many modern content management systems.

2025-11-08