During the development and maintenance of templates in AnQi CMS, we often encounter situations where some data cannot be displayed correctly or the structure of variables does not match expectations.How can one quickly and effectively find the problem at hand, which is a challenge facing every template developer?dumpThe filter, which can help us see the true nature of variables in the template, like an X-ray.
dumpWhat is a filter?
As the name suggests,dumpThe filter acts like a data probe, which can completely print out the internal structure, data type, and current value of any variable in the template.This is like giving us a magnifying glass, allowing us to directly see the true state of data during template rendering, rather than relying solely on guesses or line-by-line code inference.
dumpThe actual use of the filter in template debugging
Quickly diagnose the content and type of variables:When the rendered page of the template is different from what we imagine, the first question we often think of is: does this variable have a value?What is the type of its value?We may only see a blank space or a vague error message by directly outputting the variable name in the template.
dumpThe filter can directly display the current state of variables, whether empty strings, zero values,nilIt can be clearly seen that even a complex object. For example, you expect a variable to be a string, butdumpDisplaying it is an integer, then you know that you need to perform type conversion or correct the data source in the template.Revealing complex data structures and available fields:The template tags (such as
archiveList/categoryDetail) will return structured data, which often contains multiple fields. For example, we usearchiveListLabel retrieved a set of document data, each document may contain a title (Title), link (Link), description (Description)等多个字段。如果我们不确定某个文档对象内部具体有哪些可用的字段,或者字段名是否与我们预期的驼峰命名法(如item.Title)一致,dumpcan help us quickly confirm.It will display the complete definition of Go language structs, including the name and type of each field, which greatly reduces the time spent looking up documents and guessing.This is particularly helpful for beginners or when developing on unfamiliar data models.Debug nested data and collections:When handling lists (slice/array), maps, or custom structs,
dumpThe value is particularly prominent.It can help us distinguish whether a variable is a single object or a traversable collection, as well as the structure of each element in the collection.fortags (or conditional judgments):ifThe logic when labeling is crucial. For example, you expect to get a list of images, but the template does not output multiple images in a loop,dumpThis image variable will immediately indicate that the variable is a string rather than an array, guiding you to correct the template logic or backend data interface.
How to usedumpFilter?
UsedumpThe filter is very simple, just add it after the variable you need to view in the template.|dump.
Syntax:)
{{ obj|dump }}
Example:For example, if you are debugging in a banner (Banner) listitemVariable, it can be written like this in the template:{{ item|dump }}
The output may be similar to:&config.BannerItem{Logo:"http://127.0.0.1:8001/uploads/202309/14/eba5aa9dc4c45d18.webp", Id:1, Link:"", Alt:"Hello", Description:"", Type:"default"}
From this output, we can clearly see:itemis aconfig.BannerItemA pointer to a structure of type, which includes:Logo/Id/Link/