How to print the complete type and value of a variable during the development and debugging of AnQiCMS templates for problem troubleshooting?

Calendar 👁️ 64

During the development and debugging of AnQiCMS templates, we often encounter such situations: the page displays incorrectly, or some data is not displayed as expected.At this time, we hope to "penetrate" the variables in the template, understand their types, and what specific values they contain.After all, only by understanding the true state of the variable can you accurately locate the problem.

AnQiCMS's template system adopts syntax similar to the Django template engine, making the output of variables and logical control intuitive. In the template, we usually use double curly braces{{ 变量名 }}Use to output the value of a variable,{% 标签名 %}To perform some logical operations. When we need to delve into the internal structure of a variable, AnQiCMS provides a very useful tool:dumpfilter.

“Perspective” variable: usedumpFilter

dumpThe filter acts like an X-ray machine, able to thoroughly reveal the complete structure, data type, and specific values contained in template variables. This is particularly effective for troubleshooting the following issues:

  • Does the variable is empty (nil)?: Confirm that the variable is assigned correctly and not empty.
  • Does the data structure meet the expectations?Check if an object contains all the expected fields and whether the names of these fields are correct.
  • Is the data type correct?Understanding whether a number is a string or a number, whether a list is an array or another collection.
  • Nested data hierarchy: For complex nested objects,dumpyou can expand all levels to see clearly.

To usedumpThe filter method is very simple. You just need to pass the variables you want to debug through the pipe character|Connected todumpthe filter.

Basic syntax:

{{ 变量名 | dump }}

In order todumpThe output content is easier to read in the browser, and we usually wrap it in HTML.<pre>In the tag, this can preserve the original format and line breaks.

Example: Print the complete information of a document object in the page.

Suppose we are debugging a document list, want to view eachitemWhat fields and values it contains:

{% archiveList archives with type="list" limit="1 %}
    {% for item in archives %}
        <h3>当前文档项的数据详情:</h3>
        <pre>{{ item | dump }}</pre>
    {% endfor %}
{% endarchiveList %}

<h3>某个独立变量的详情:</h3>
{% set systemInfo = system.SiteName %} {# 假设我们想看系统站点名称的变量详情,虽然这里只是一个字符串,但演示set用法 #}
<pre>{{ systemInfo | dump }}</pre>

UnderstandingdumpOutput of the filter

When you view the output of the above code in a browser, you may see something like this (withBannerItemfor example):

&config.BannerItem{Logo:"https://en.anqicms.com/uploads/202309/14/eba5aa9dc4c45d18.webp", Id:1, Link:"", Alt:"Hello", Description:"", Type:"default"}

This string looks a bit 'professional', but it is actually the structure representation of the variable in the Go language. Let's break it down:

  • &: indicates that the variable is a pointer type.
  • config.BannerItemThis is the Go language type name of the variable, which usually indicates the specific data structure it corresponds to within the AnQiCMS system. For example,archiveThe variable may be displayed asmodels.Archive.
  • {...}: The curly brackets list all the field names and their corresponding values, for exampleLogo:"...",Id:1,Link:"",Alt:"Hello"wait. Through this information, you can clearly know what properties this variable has, as well as the current value of each property.

In this way, you can quickly confirm:

  • LogoDoes the field have an image address?
  • IdIs the field the expected number?
  • LinkWhy is the field an empty string, is there a misconfiguration somewhere?

Combine other debugging techniques

exceptdumpFilter, the following debugging techniques can also improve efficiency:

  1. Temporary variable assignmentsetorwith: When you need to debug the result of a complex expression, or a specific context (such asincludeWhen passing a variable to a label{% set 变量名 = 表达式/变量 %}or{% with 变量名 = 表达式/变量 %}Assign it to a temporary variable first, then perform operations on this temporary variabledump.

    {% set myCalculatedValue = item.Price * item.Quantity %}
    <pre>计算结果:{{ myCalculatedValue | dump }}</pre>
    
  2. Remove it after debugging is complete:dumpThe debugging information generated by the filter is only useful during the development and testing phases. Be sure to turn off alldumpThe related code is removed from the template to avoid unnecessary performance overhead and information leakage.

Summary

dumpThe filter is an indispensable tool in the development and debugging process of AnQiCMS templates.It can help us intuitively understand the internal world of variables, quickly locate and solve template display problems, thereby greatly improving development efficiency.Proficiently using this tool will make your AnQiCMS template development experience smoother.


Frequently Asked Questions (FAQ)

Q1: Why do I usedumpIs the content output by the filter a pile of garbled or difficult-to-read strings?

A1:dumpThe filter outputs the internal struct representation of a variable in Go language, which is usually plain text.If you see garbled text, first make sure that your template file is saved in UTF-8 encoding.Secondly, to ensure that the browser displays its format correctly, it is always recommended to{{ 变量 | dump }}Wrapped in<pre>tags, such as<pre>{{ item | dump }}</pre>. This can prevent the browser from misinterpreting it as HTML and destroying the original format.

Q2: Idumpcreated a variable, but it did not display any content, or only displayednilWhat is the reason for this?

A2: IfdumpThe filter outputtednilOr there is nothing, which usually means that the variable is indeed empty in the scope of the current template, or it points to data that does not exist. You need to check the following points:

  1. Variable name spelling: Confirm whether the variable name is completely correct, including uppercase and lowercase.
  2. Whether the data is passed into the template: Check whether the variable has been passed correctly to the template in the backend logic.
  3. Variable assignment logic: Trace the source of the variable, check the earlier logic (such asarchiveListIn the tag, controller code, etc., is the variable correctly retrieved and assigned.It may have a value after some condition judgment, or it may not be able to obtain data due to a mismatch in filtering conditions.

Q3:dumpThe Go structure information that comes out (such as&models.Archive{Id:1, Title:"文章标题", ...}) I'm not familiar with, how should I understand it?

A3: This is actually the string representation of a struct in Go language.&It indicates that this is a pointer type.models.ArchiveThis is the specific data structure type name corresponding to this variable in the AnQiCMS backend code. Curly braces{}It lists each "field name" and its corresponding "value". For example,Id:1indicates that there is a namedIdwith a value of1;Title:"文章标题"indicates that there is a namedTitlewith a value of"文章标题". By these field names, you can understand what data the variable contains (such as article ID, title, content, etc.), and use these values to judge whether the data is correct.These field names are typically passed through the template you are using{{ item.Id }}or{{ item.Title }}property accessed is consistent.

Related articles

How to automatically add line numbers to plain text content entered in the background of AnQiCMS template?

When operating a website, we often encounter such a need: we hope that the pure text content entered in the background can automatically display line numbers on the front end, which is very helpful for displaying code, poetry, configuration scripts, or any text content that needs to be read line by line.Although AnQiCMS back-end content entry is very convenient, how can these plain text contents be automatically numbered with beautiful line numbers when displayed on the front end?The flexible template engine of Anqi CMS can help us easily achieve this.### Understanding the Source and Processing Basics Firstly, we need to clarify the source of this plain text content

2025-11-07

How to automatically render the multi-line text (including newline characters) of AnQiCMS custom fields into HTML tags `<p>` and `<br/>`?

When using AnQiCMS to manage website content, we often take advantage of its powerful custom field features to enrich page information.Especially for text that needs to include multiline descriptions, notes, or detailed explanations, the 'Multiline Text' type in custom fields is undoubtedly the ideal choice.However, when we eagerly display these text contents with line breaks on the front-end page, we may find that the original line breaks are not automatically recognized by the browser, resulting in all the text being cramped together, greatly reducing the reading experience.This is actually a feature of HTML rendering mechanism

2025-11-07

How to implement the `replace` filter of AnQiCMS for batch replacement of sensitive words in article content?

In the field of content management, the flexibility and maintainability of website content are crucial.Batch replacement of article content is an efficient and practical operation, whether it is for brand unification, information update, or sensitive word filtering.AnQiCMS as a rich-featured enterprise-level content management system, provides a variety of content processing mechanisms, among which the `replace` filter and the background content batch replacement feature play a key role in different scenarios.

2025-11-07

How to concatenate or add two strings or numbers in AnQiCMS template?

In AnQi CMS template design, dynamically combining text information or performing calculations on numerical values is a common requirement.It is crucial to be able to handle string concatenation and numeric addition operations flexibly, whether it is to build personalized product descriptions or to display dynamic data on the page.The Anqi CMS template system, drawing on the syntax of the Django template engine, provides a variety of intuitive and powerful methods to complete these tasks.

2025-11-07

What are the practical scenarios for the AnQiCMS `phone2numeric` filter? How to convert letters on a phone keyboard to numbers?

In the operation of daily websites, we often encounter situations where we need to process various types of data and display them in a user-friendly manner.A phone number is a typical example. Sometimes, for marketing or brand promotion, we may see some "vanity numbers" composed of letters and numbers, such as "1-800-FLOWERS".These numbers are easy to remember, but users still need to manually convert them to pure digits when dialing.AnQi CMS as a powerful enterprise-level content management system

2025-11-07

How to generate a specified number of random text placeholders when there is no actual content in the AnQiCMS template?

During the development of website templates, we often encounter such situations: the interface layout has been completed, but the actual content data is not ready.At this time, in order to ensure that the layout and visual effects of the front-end page can be accurately presented, we need some placeholder text to fill the page, simulating the existence of real content.AnQi CMS fully considers this requirement and provides a very practical template tag - `lorem`, which can help us easily generate a specified number of random texts.

2025-11-07

How to repeat a specific string (such as a separator) multiple times in the AnQiCMS template?

In web template design, we often encounter the need to repeatedly output a specific string or character pattern to achieve visual separation, emphasis, or layout effects.For example, you may want to display a line composed of multiple dashes between content blocks, or repeat asterisks to decorate an area.In the AnQiCMS template system, thanks to its syntax similar to the Django template engine, achieving such repeated output is very flexible and efficient.

2025-11-07

How to efficiently check if a long string or array contains a certain keyword in the AnQiCMS template?

In the daily operation of AnQiCMS, we often need to quickly and accurately judge whether a specific keyword or phrase exists in the dynamic content of the website, such as the main body of articles, product descriptions, custom fields, and even tag lists and category names.This requirement is particularly common in content management, SEO optimization, or personalized display.How can you efficiently complete this task in the flexible and powerful template system of AnQiCMS?

2025-11-07