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

AnQiCMS's template system uses a syntax similar to Django template engine, which makes variable output and logical control intuitive. In the template, we usually use double curly braces{{ 变量名 }}Print the value of a variable, use{% 标签名 %}Execute some logical operations. When we need to understand the internal structure of a variable in detail, AnQiCMS provides a very useful tool:dumpFilter.

“Perspective” variable: usingdumpFilter

dumpThe filter is like an X-ray machine, which can thoroughly reveal the complete structure, data type, and specific values of template variables. This is particularly effective for troubleshooting the following issues:

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

to usedumpFilter, the method is very simple. You just need to pass the variable to be debugged through the pipeline|connected todumpthe filter.

Basic syntax:

{{ 变量名 | dump }}

In order todumpThe output content is more readable in the browser, and we usually wrap it in HTML's<pre>Label within, so as to retain the original format and line breaks.

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

Assuming we are debugging a document list, we want to view eachitemwhich 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>

UnderstandingdumpFilter Output

When you view the output of the above code in the browser, you may see something similar to the following (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 struct representation of the variable in the Go language. Let's break it down:

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

In this way, you can quickly confirm:

  • LogoField has an image address?
  • IdField is the expected number?
  • LinkWhy is the field an empty string, is there a configuration error?

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 asincludeLabeling the variables) in the variable, you can first use{% set 变量名 = 表达式/变量 %}or{% with 变量名 = 表达式/变量 %}Assign it to a temporary variable, and then operate on this temporary variabledump.

    {% set myCalculatedValue = item.Price * item.Quantity %}
    <pre>计算结果:{{ myCalculatedValue | dump }}</pre>
    
  2. Remove it after debugging:dumpThe debugging information generated by the filter is only useful during development and testing stages. It must be ensured that all of it is removed before the website is officially launched (production environment).dumpThe 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.


Common Questions (FAQ)

Q1: Why do I usedumpIs the output content of the filter a bunch of garbled text or difficult to read strings?

A1:dumpThe output of the filter is the internal struct representation of the variable in the 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.{{ 变量 | 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: Idumpdefined a variable, but it did not display any content, or only displayednilWhat is the reason for this?

A2: Ifdumpthe filter outputsnilor it contains no content, which usually means that the variable is indeed empty in the scope of the current template, or the data it points to does not exist. You need to check the following points:

  1. Variable name spelling: Confirm whether the variable name is completely correct, including the case.
  2. Data passed to the template: Check whether the variable has been correctly passed to the template in the backend logic.
  3. Variable assignment logicTrace the source of the variable, check in the earlier logic (such asarchiveListIn the tag, controller code, etc., has the variable been 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 struct information coming out (such as&models.Archive{Id:1, Title:"文章标题", ...}) I'm not very familiar with, how should I understand it?

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