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:"http://127.0.0.1:8001/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:
Temporary variable assignment
setorwith: 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>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:
- Variable name spelling: Confirm whether the variable name is completely correct, including uppercase and lowercase.
- Whether the data is passed into the template: Check whether the variable has been passed correctly to the template in the backend logic.
- Variable assignment logic: Trace the source of the variable, check the earlier logic (such as
archiveListIn 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.