During the template development process of AnQiCMS, we often encounter a frustrating scenario: the page display does not meet expectations, and the data of a certain variable cannot be output correctly.At this moment, we urgently need a 'sharp eyes' to透视 the real data type and internal structure of template variables.AnQiCMS powerful Django-like template engine is flexible, but if it cannot clearly understand the 'true face' of variables during debugging, the efficiency will be greatly reduced.
幸运的是,AnQiCMS为我们提供了一系列实用工具,帮助我们深入探查模板变量。This is like adding a 'debug mode' to your template, allowing you to clearly see the data behind each variable.
Discover the 'secret weapon' of variables: dumpFilter
In many debugging tools,dumpThe filter is undoubtedly our preferred 'secret weapon' for checking variable data types and structures.It can print the full structure, data type, and current value of any variable in the form of a Go language struct.archiveDetailorcategoryListdata obtained from tags such as these is particularly useful.
Imagine, you get a document from the background and assign it to thearchivevariable. You may only know that it hasTitle/ContentThis field, but what other fields does it have internally?ImagesIs it a string or an array?CreatedTimeIs it a timestamp or a formatted date string? These questions,dumpcan all give you an answer.
You only need to use it like this in the template:
{# 假设我们有一个名为 'archive' 的文档变量 #}
<pre>
{{ archive|dump }}
</pre>
{# 或者你可能在循环中,想查看每个 'item' 的详细信息 #}
{% archiveList archives with type="list" limit="1 %}
{% for item in archives %}
<pre>
{{ item|dump }}
</pre>
{% endfor %}
{% endarchiveList %}
When you visit the page, the browser will display something like this output (the specific content depends on your data):
&models.Archive{Id:1, Title:"AnQiCMS模板调试指南", SeoTitle:"", Link:"/article/1.html", Keywords:"AnQiCMS,模板,调试", Description:"本文将详细介绍如何在AnQiCMS模板中调试...", Content:"<p>正文内容...</p>", ContentTitles:[], ModuleId:1, CategoryId:10, CanonicalUrl:"", UserId:1, ParentId:0, Price:0.00, Stock:0, ReadLevel:0, OriginUrl:"", Views:123, Flag:"c", Images:["https://en.anqicms.com/uploads/image1.webp", "https://en.anqicms.com/uploads/image2.webp"], Logo:"https://en.anqicms.com/uploads/thumb1.webp", Thumb:"https://en.anqicms.com/uploads/thumb1_s.webp", CommentCount:5, CreatedTime:1678886400, UpdatedTime:1678972800, Category:..., Tags:[], Params:{...}}
From this output, we can clearly seearchiveis amodels.ArchiveA structure of types, which includesId(integer),Title(string),Images(string array),CreatedTimeThe timestamp and corresponding values. This is much more efficient than blind guessing.
In-depth analysis:stringformatand type detection
dumpThe filter is powerful, but sometimes we may not need such detailed information, we just want to quickly understand the type of variables, or view the structure in the form of Go language source code snippets.stringformatThe filter can be used by pairing it with specific formatting symbols.
To view the variable type: use
%TIf you are only concerned about the data type of a variable and do not want to see its value,
stringformat:"%T"It is a perfect tool.{# 查看文档标题的类型 #} <p>文档标题类型:{{ archive.Title|stringformat:"%T" }}</p> {# 查看图片列表的类型 #} <p>图片列表类型:{{ archive.Images|stringformat:"%T" }}</p> {# 查看创建时间的类型 #} <p>创建时间类型:{{ archive.CreatedTime|stringformat:"%T" }}</p>The output might be:
文档标题类型:string 图片列表类型:[]string 创建时间类型:int64View the structure and values in Go source code form: using
%#vIf you want the output to be closer to the Go struct initialization code,
stringformat:"%#v"It will be very helpful. It will output Go source code snippets with field names and values, usually more concise thandumpthe output, and still retains the structural information.<pre> {{ archive|stringformat:"%#v" }} </pre>The output will be:
main.Archive{Id:1, Title:"AnQiCMS模板调试指南", ..., Images:[]string{"https://en.anqicms.com/uploads/image1.webp", "https://en.anqicms.com/uploads/image2.webp"}, ...}this is
dumpSimilar, but formatted differently, sometimes it is easier to read.
Auxiliary tool: makes debugging more convenient
In addition to the powerful filters mentioned above, there are some other tools and techniques that can provide convenience during debugging:
lengthFilter: Check the size of the collectionWhen you want to know if an array (slice) or a string is empty, or how many elements it contains,
lengthfilters are very useful.{# 检查文档标题长度 #} <p>标题长度:{{ archive.Title|length }}</p> {# 检查图片列表包含的图片数量 #} <p>图片数量:{{ archive.Images|length }}</p> {# 结合 if 语句判断是否有图片 #} {% if archive.Images|length > 0 %} <p>文档包含图片。</p> {% else %} <p>文档不包含图片。</p> {% endif %}joinandsplitFilters: visualize array contentIf
dumpThe array content is a bit difficult to read, you can try usingjoinConnecting array elements into a string, or usingsplitSplitting the string into an array for inspection.{# 将图片数组用逗号连接起来显示 #} <p>所有图片路径:{{ archive.Images|join:", " }}</p>stampToDate标签:时间戳的“English”翻译官在AnQiCMS中,时间字段(如English)
CreatedTime/UpdatedTime)通常以10位Unix时间戳的形式存在。如果你直接输出English{{ archive.CreatedTime }}English translation: A series of numbers are seen, not the date format we are accustomed to.stampToDateThe tag is designed for this, it can convert the timestamp to a readable date format.{# 将时间戳转换为“年-月-日 时:分:秒”格式 #} <p>发布时间:{{ stampToDate(archive.CreatedTime, "2006-01-02 15:04:05") }}</p>This can help you confirm whether the value of the time field is correct.
ifLogical judgment label: quick condition checkbasic
ifThe statement is also very useful during debugging, which can be used to check if a variable exists, whether it is a specific value, or whether it is empty.{% if archive %} <p>archive 变量存在且非空。</p> {% endif %} {% if archive.AuthorName == "AnQiCMS团队" %} <p>作者是 AnQiCMS团队。</p> {% endif %}setorwithTag: Isolation and Naming VariablesIn complex templates, variables may be deeply nested or have long names. You can use
setorwithCreate a temporary variable using a label to simplify the debugging process, or to save the result of an expression for later inspection.{# 创建一个临时变量来存储文档的第一张图片 #} {% set firstImage = archive.Images|first %} <p>第一张图片路径:{{ firstImage|dump }}</p> {# 或者在局部作用域内定义变量 #} {% with tempTitle = archive.Title %} <p>临时标题:{{ tempTitle|dump }}</p> {% endwith %}
Recommended workflow for actual debugging.
When you encounter problems in the AnQiCMS template, you can try the following debugging steps:
- Locate the problem area:Determine which variable may have caused the incorrect output.
- Insert
dumpFilter:Beside the variable you suspect, temporarily insert{{ 变量名|dump }}and use<pre>wrapped in tags to clearly display on the page. - Analysis output:View
dumpoutput to understand the exact type and internal