During the development and maintenance of Anqi CMS templates, we often encounter situations where data cannot be displayed correctly or variable structures do not match expectations.How can one quickly and effectively find the root cause of the problem, which is a challenge facing every template developer.fortunately, AnQi CMS provides an extremely useful tool -dumpA filter that can help us see the true nature of variables in the template, like an X-ray.

dumpWhat is a filter?

As the name implies,dumpThe filter acts like a data probe, which can fully print the internal structure, data type, and current value of any variable in the template.This is like giving us a透视镜, allowing us to directly see the real state of the data during template rendering, rather than relying solely on guesses or line-by-line code inference.

dumpThe actual use of filters in template debugging.

  1. Quickly diagnose variable content and type: When the rendered page of the template is different from what we expect, the first question that often comes to mind is: does this variable have a value?What is the type of its value? Simply by outputting the variable name directly in the template, we may only see a blank space, or a vague error message. AnddumpThe filter can directly display the current state of the variable, whether it is an empty string, zero value,nilIt is also clear for a complex object. For example, you expect a variable to be a string, butdumpShow that it is an integer, then you know that you need to perform type conversion or correct the data source in the template.

  2. Reveal complex data structures and available fields:The template tags (such asarchiveList/categoryDetailetc.) will return structured data, which often contains multiple fields. For example, we usearchiveListThe tag retrieved a set of document data, each document may contain a title (Title), link (Link), description (DescriptionMultiple fields. If we are not sure about the specific fields available within a document object or whether the field names are consistent with our expected camel case naming (such asitem.Title)}dumpIt can help us confirm quickly. It will display the complete definition of the Go language struct, including the names and types 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.

  3. Debugging nested data and collections:When processing lists (slice/array), maps (map), or custom struct types:dumpIts value is particularly prominent. It can help us distinguish between a single object and an iterable collection, as well as the structure of each element in the collection.This is for writing loops (forTags) or conditional judgments (ifThe logic at the time of the tag is crucial. For example, you expect to get a list of images, but the template does not loop out multiple images,dumpThe following image variable will immediately indicate that the variable is a string rather than an array, prompting 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|dumpJust do it.

Syntax: {{ obj|dump }}

Example:For example, if you are debugging in a 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.BannerItemPointer to a structure of type, it includes:Logo/Id/Link/