How to display the detailed structure, type, and value of the `dump` filter in AnQiCMS template debugging?

Calendar 👁️ 64

During the development and maintenance of AnQiCMS templates, we often encounter situations where we need to confirm the content of variables.Improper use of template tags or the data structure passed from the backend may lead to abnormal display of the page.If you can see the internal structure, type, and current value of the variable at this time, it will undoubtedly greatly improve our debugging efficiency.In the AnQiCMS template system,dumpThe filter is exactly designed to solve this pain point, it acts like a periscope, helping us gain a deep understanding of the 'inner nature' of template variables.

The pain points of template debugging anddumpThe role of the filter

AnQiCMS uses a syntax similar to the Django template engine, which is a concise and efficient template language that allows us to quickly build pages.However, when the page displays incorrectly, or when certain data does not appear as expected, traditional debugging methods are often not intuitive.We may need to guess whether the variable name is spelled correctly or what type of data is passed in, and what fields it contains.

dumpThe filter is designed to solve these puzzles. Its core function is to clearly output the detailed structure, type, and current value of any variable to the page.This includes the names of the underlying data structures, field names, and their corresponding values in the Go language, making it easy to understand the content of variables.

How to usedumpFilter?

UsedumpThe filter is very simple and intuitive. You just need to attach it to the variable you want to check, just like using other filters. Its basic syntax format is:

{{ 变量名|dump }}

For example, suppose you have one in a templateitemTo know the specific content of a variable, you can directly write it like this:

<pre>{{ item|dump }}</pre>

We usually place it in<pre>Label within, so that the output content will retain the original format, which is more convenient for reading, especially when the variable content is a complex structure.

Practical application scenarios and output interpretation

Let's look at some specific examples to see.dumpThe filter in the actual application of AnQiCMS template debugging.

Scenario one: Check the individual item in the loop.

In the list page or data loop, we often need to traverse.archiveList/categoryListThe data collection returned by the label. If you want to view the structure of a certain loop elementitemWe can do this:

{% archiveList archives with type="list" limit="10" %}
    {% for item in archives %}
        <p>文章标题: {{ item.Title }}</p>
        <pre>{{ item|dump }}</pre> {# 在这里使用 dump 过滤器 #}
    {% endfor %}
{% endarchiveList %}

When you refresh the page, below each article title, you will see output similar to this (this is just an example, the actual output will be longer and more detailed):

&models.Archive{Id:1, CreatedTime:1678886400, UpdatedTime:1678886400, DeletedTime:0, SiteId:1, ModuleId:1, CategoryId:5, UserId:1, ParentId:0, Title:"AnQiCMS模板调试:如何显示变量的详细结构", Link:"/article/anqicms-template-debug.html", SeoTitle:"", Keywords:"AnQiCMS,模板调试", Description:"本文将详细介绍AnQiCMS模板调试时,如何利用dump过滤器显示变量的详细结构、类型和值...", Content:"...", Logo:"https://en.anqicms.com/uploads/images/thumbnail.webp", Thumb:"https://en.anqicms.com/uploads/images/thumbnail-thumb.webp", Images:[]string{}, Flag:"c", OriginUrl:"", CanonicalUrl:"", CustomUrl:"", ReadLevel:0, Price:0.00, Stock:0, Views:1234, CommentCount:10, Likes:50, Dislikes:2, Sort:0, Status:1, CombineIds:nil, ExtraFields:map[string]interface {}{"author":"张三"}}

From the above output, we can clearly see that:

  • This is amodels.ArchiveThe structure of the type(&models.Archive{...})
  • It includesId(article ID),Title(Title),Link(Link),Keywords(Keywords),Description(Description),Content(Content),Logo(Cover image),Views(Views) and many other fields.
  • The name of each field (for exampleId) and its current stored value (for example1) are all displayed at a glance.
  • likeExtraFieldsEven complex fields will show their internal details.mapthe structure.

This information is very helpful for confirming whether a variable is assigned correctly, or if there is a spelling error in a field name.

Scenario two: Check the system configuration variables

When we need to get the global system settings, such as the website nameSiteNameyou can usesystemLabel. If we are not sureSiteNameHow the variables are passed, or if you want to seesystemWhat other information the label can provide,dumpFilters can also be used:

<pre>{{ system|dump }}</pre>

You might get a structure that contains all system configurations, allowing you to discover and use more useful fields.

Scenario three: Debugging custom parameters or complex objects

For custom fields or complex data objects passed from the backend, their internal structure is often difficult to grasp.dumpThe filter can display these data objects unchanged, including nested structures, arrays, or mappings.

For example, in the document parameter tagarchiveParamsIn Chinese, we get an array containing custom fields. To understand eachitemYou can use it like thisdump:

{% archiveParams params %}
    {% for item in params %}
        <p>参数名称:{{item.Name}}</p>
        <pre>{{ item|dump }}</pre>
    {% endfor %}
{% endarchiveParams %}

The output may look like:

&models.ArchiveExtraField{Name:"作者", Value:"张三", FieldName:"author", Type:"text", Required:false, Content:""}

This tells usitemIsmodels.ArchiveExtraFieldType, there isName/Valuefields, as well as their specific values.

When should it be useddumpFilter?

  • when the content of a variable is unclear.: When you suspect a variable is null,nilor contains unexpected data.
  • when the data structure is complex.When handling nested structures such as arrays, mappings, or complex data types, it is necessary to understand their internal fields and levels.
  • Error occurred during template rendering.If the page rendering fails, it is suspected that the variable access path is incorrect or the data type does not match,dumpit can help you quickly locate the problem.
  • Custom field debuggingAfter using custom content models and fields, it is necessary to confirm whether these custom fields are passed to the template as expected.

UsedumpFilter considerations

  • Used only for debugging:dumpThe filter is a powerful debugging tool, but it should never be kept in the code of the production environment.It exposes the internal data structure and sensitive information of the website, increases security risks, and also outputs a large amount of unexpected content on the page, affecting user experience and page loading performance.
  • Remove in a timely mannerOnce debugging is complete, be sure to remove all from the template.{{ 变量名|dump }}code.
  • Combine<pre>TagAs shown in the above example, we will.dumpEnclosed in the output<pre>Tags in, can better maintain the format, improve readability.

Proficient in masteringdumpThe filter will bring great convenience to your AnQiCMS template debugging work, allowing you to build and maintain websites more efficiently.


Frequently Asked Questions (FAQ)

  1. dumpCan the filter be used in a production environment?It is not recommended to use the filter in a production environmentdumpA filter. It will directly output the detailed internal structure and value of the variable, which may contain sensitive information (such as database fields, system paths, etc.), posing a threat to the security of the website.At the same time, the output of a large amount of debugging information will also affect the page loading speed and user experience.Before deploying online, be sure to remove alldumpfilter.

  2. dumpCan the filter display detailed information about all types of variables?Yes,dumpThe filter is implemented based on the underlying data reflection mechanism of Go language, which can display the detailed structure, type, and current value of almost all type variables in Go language, including basic data types (strings, numbers, boolean values), arrays, slices (slice), maps (map), structs (struct) and their combinations.

  3. How to use without destroying the page layoutdumpFilter?To avoiddumpThe content output by the filter disrupts the page layout, the simplest and most effective method is to wrap it in HTML's<pre>tags, for example<pre>{{ 变量名|dump }}</pre>.<pre>The tag preserves whitespace and line breaks in text and is usually displayed in a monospace font, which makes complex structured output easier to read. In addition, you can temporarily comment out the surrounding HTML code to retain onlydumpObserve the output.

Related articles

What is the result of repeatedly outputting an empty string in the `repeat` filter of AnQiCMS templates?

AnQiCMS's template system is renowned for its concise and efficient Django-style syntax, which allows content developers to easily build dynamic pages.In daily template development, we often use various filters to process and format data.Among them, the `repeat` filter is a very practical tool that can repeat a string according to the specified number of times.However, when using the `repeat` filter, some developers may encounter a seemingly simple but easily confusing problem: when

2025-11-09

How does the `split` filter handle empty strings or strings without the specified delimiter in AnQiCMS templates?

In AnQiCMS template development, string processing is a common requirement.Sometimes we need to split a string containing multiple values into individual items so that they can be traversed on the page for display or further processing.At this time, the `split` filter has become a powerful assistant for template developers.It can split a string into an array (or slice) based on a specified delimiter, greatly simplifying the logic of complex data display.However, some specific scenarios when using the `split` filter may confuse developers who are new to the field

2025-11-09

How to use the `join` filter in AnQiCMS template to handle empty arrays or arrays with a single element?

In AnQi CMS template development, the flexibility of data display is crucial.We often need to present the data list obtained from the backend in a beautiful and consistent manner on the front-end, which includes the need to concatenate the elements of an array (or list) into a string.The AnQi CMS template engine provides a powerful `join` filter, which not only handles common arrays containing multiple elements but also demonstrates its unique and practical behavior in special cases such as empty arrays or arrays containing only a single element.The `join` filter is a very practical tool in the Anqi CMS template

2025-11-09

How to judge whether an array or string is empty in AnQiCMS template using the `length` filter?

In Anqi CMS template development, we often need to decide the display content of the page based on the existence or absence of data, for example, a list of articles. If the list is empty, we may need to display "No content" instead of a blank area.At this time, the `length` filter becomes our powerful assistant, which can help us easily judge whether arrays, strings, and other data are empty.### Getting to know the `length` filter The `length` filter is a very practical feature provided by the Anqi CMS template engine

2025-11-09

What does the `stringformat` filter return when it fails to handle formatting in the AnQiCMS template?

AnQiCMS (AnQiCMS) boasts its efficient architecture based on the Go language and flexible template engine, bringing great convenience to content management.In daily content operation, we often need to present data in a specific format on the website front-end, which cannot do without various practical template filters.The `stringformat` filter is a powerful tool for data formatting, allowing us to format numbers, strings, and even more complex data types in a precise way, similar to the `fmt.Sprintf` function in the Go language.Understand

2025-11-09

In AnQiCMS template, which filters or tags behave in three states of 'or and not' logic for `nil` values?

In AnQiCMS template development, efficiently and robustly handling data is the key to building dynamic websites.Especially when the data obtained from the backend may contain `nil` (empty) values, it is particularly important to elegantly display content, provide default values, or execute specific logic in the template.AnQiCMS provides various filters and tags that can help us display three states of existence, non-existence, or undefined for `nil` values, thereby enhancing the flexibility and user experience of templates.### One, directly judge whether the data exists: `if`

2025-11-09

How to ensure that the AnQiCMS template does not cause page rendering errors when the variable is `nil`?

When building a website with AnQiCMS, we often encounter the situation where template variables may be empty (`nil`).For example, a document may not have a thumbnail set or a custom field may not be filled in.If the template code does not properly handle these potential null values, errors may occur during page rendering, affecting user experience and even causing some website functions to be unavailable.Fortunately, the AnQiCMS template engine provides various mechanisms to help us elegantly handle the cases where variables are `nil`, ensuring the stability and smoothness of the page

2025-11-09

How to combine `if` and `yesno` filters in AnQiCMS templates to display different prompts based on VIP status and expiration time?

In website operations, providing differentiated services and content prompts to different user groups is a key link in improving user experience and achieving business goals.Especially on websites with a VIP membership system, displaying personalized information based on the user's VIP status and even the expiration time of the membership can greatly enhance the user's sense of belonging and willingness to interact.AnQiCMS (AnQiCMS) relies on its flexible template engine and rich tags, filters, making everything simple and efficient.Today, let's discuss how to use the AnQiCMS template

2025-11-09