How can the `dump` filter be used in the template development process for debugging and viewing the structure and value of variables?

Calendar 👁️ 80

During the development of Anqi CMS templates, we often need to handle various dynamic data.This data may come from a database, system configuration, or user input, and it is passed to the template as variables for display.However, sometimes the results displayed on the page are not what we expect - a field is empty, the data format is incorrect, or the elements contained in a collection are not as expected.In this case, efficiently "viewing" the internal structure, type, and specific values of variables becomes the key to troubleshooting and accelerating development progress.

The Anqi CMS template engine provides many practical filters (filters), one of which is particularly useful for debuggingdumpA filter. It's like an X-ray machine that can penetrate the appearance of variables, clearly displaying its details - including the underlying data structure, type, and the specific value stored at the moment.This greatly facilitates our understanding of the actual form of the data in the template, thus accurately locating problems.

How to usedumpView the structure and value of the filter?

dumpThe filter usage is very intuitive and simple. Just add it after the variable you need to check|dumpand. For example, suppose we are iterating over a variable namedbannersa list, where each element is aitemto view a singleitemdetailed structure and values, we can write the template code as follows:

{% for item in banners %}
    <div>{{ item|dump }}</div>
    {# ... 模板的其他内容 ... #}
{% endfor %}

Or, if we need to view a single variable, for examplearchiveWe can access all properties of an object like this:

<div>{{ archive|dump }}</div>

When you visit the page containing this code in the browser, the detailed information of the variable will be directly output on the page. For example, ifitemIs aBannerItemAn object of the type, you might see an output similar to this:

&config.BannerItem{Logo:"https://en.anqicms.com/uploads/202309/14/eba5aa9dc4c45d18.webp", Id:1, Link:"", Alt:"Hello", Description:"", Type:"default"}

This output information is very valuable. It tells us:

  • The type of variableFor example,&config.BannerItemIndicates it is a pointer toconfigUnder packageBannerItemPointer to the structure.
  • Fields contained::LikeLogo/Id/Link/Alt/DescriptionandType, these areBannerItemProperties defined in the structure.
  • The current value of the fieldEach field is followed by the specific data it is currently storing, for exampleLogoThe value of the field is"https://en.anqicms.com/uploads/202309/14/eba5aa9dc4c45d18.webp",Idhas a value of1.

With this information, we can quickly judge:

  1. whether the variable is empty (if it is empty,dumpMay not output anything or output an empty value).
  2. Is the variable the expected data type?
  3. Does the variable contain all the fields we need?
  4. Are the values of each field correct, for example, is the image URL valid, does the ID match?

Application scenarios in practice

dumpThe application scenarios of filters in template debugging are very extensive:

  • Investigating missing dataWhen a certain template{{ variable.Field }}does not display any content{{ variable|dump }}you can checkvariableIs itnilOrFieldwhether it exists and has a value.
  • inside the loop body checkWhen processing list data, for example, usingarchiveListorcategoryListtags to loop through output content within a loop{{ item|dump }}can check eachitemdata to ensure that the structure and values of each element meet expectations.
  • Complex Object Exploration: For objects such asarchive(Document) orcategory(Classification) such complex objects containing multi-level data, directly{{ archive|dump }}It can help us quickly understand all the accessible properties it contains, even the structure of nested sub-objects.
  • Validation of function or tag return value:Sometimes we are not sure about the data format returned by a custom tag or function, usingdumpwe can directly see its return result.

Points to note

ThoughdumpThe filter is powerful, but it should only be used as an auxiliary tool during development and testing stages. It exposes the internal data structures and sensitive information of the system.Absolutely should not be retained in the production environment after going onlineMake sure to clear all debugging information before deploying onlinedumpFilter code to ensure website security and data privacy.

MasterdumpFilter, it will significantly improve your debugging efficiency in Anqi CMS template development, allowing data issues to be easily identified, thus enabling you to build websites that meet requirements and run stably more quickly.


Frequently Asked Questions (FAQ)

Q1:dumpHow to view only the parts of interest when the filter output is too much?A:dumpThe filter outputs the complete structure and value of the variable, which can indeed lead to very long output for large or complex objects. If you are only interested in a specific attribute, you can directly use it on that attributedumpFilter, for example{{ item.Title|dump }}Instead of the wholeitemThe object. Moreover, in the browser, you can use the developer tools (usually opened by pressing F12 key) in the 'Elements' or 'Console' tab to easily find and collapse the output content, which enhances the viewing efficiency.

Q2: Why did I use{{ variable|dump }}But no content was displayed on the page?A: If the page does not displaydumpThe output of the filter, usually with several possibilities. First, make sure the variable name is spelled correctly. Second, the variable may indeed be empty in the scope of the current template (nilOr undefined). You can try in a higher scope (for example, in the parent template calling the current template) or on more basic variables to gradually narrow the search range.dumpTo gradually narrow the search range.

Q3:dumpCan the filter be used in a production environment?A: No.dumpThe filter is designed for development and debugging, it will expose the internal data structure, type, and variable values that may contain sensitive information. It should be retained in a production environment.dumpThe filter poses a serious security risk and potential data leakage issues. Therefore, be sure to thoroughly check and remove all before any website goes live.dumpthe use of filters.

Related articles

How does the `divisibleby` filter determine if a number is divisible by another number, and what conditions are commonly used?

The template engine of AnQiCMS provides a powerful and flexible tool for the dynamic display of website content.Amongst them, the `divisibleby` filter is a small but effective feature that enhances the logic of templates, mainly used to determine if a number can be evenly divided by another number.Understand and make good use of this filter, which can help us achieve more intelligent effects in content presentation and page layout.### `divisibleby` filter's core function

2025-11-08

How to set default values for `default` and `default_if_none` filters to avoid displaying blank when the variable is empty or `nil`?

In daily website content management, we often encounter such situations: certain variables may be empty due to unfilled content, missing data, or other reasons (or in programming terms, `nil`).If not handled, these variables may be displayed as blank areas on the front-end page, which not only affects the beauty of the page but may also confuse visitors and reduce the professionalism of the website.AnQiCMS (AnQiCMS) is well aware of this pain point

2025-11-08

How do the `date` and `time` filters display `time.Time` type time values in a specified format?

In AnQi CMS template design, flexibly displaying dates and times is a key aspect of content presentation.In order to meet this refined requirement, AnQi CMS provides a series of practical time processing tools, among which the `date` and `time` filters are important functions for formatting and displaying `time.Time` type time values.

2025-11-08

How to remove specified characters from any position in the string using the `cut` filter in the Anqi CMS template?

In website content operation, we often need to process strings, such as cleaning up extra characters, or standardizing the display content.AnQiCMS template engine provides a series of powerful filters to help us complete these tasks, among which the `cut` filter is a very practical tool, specifically used to remove specific characters from any position in a string.### Understanding the working principle of the `cut` filter The `cut` filter plays a very direct role in the AnQi CMS template

2025-11-08

What security escaping features do the `escape`, `e`, and `escapejs` filters provide when outputting HTML or JavaScript code?

It is crucial for the safety of output content in website operations.Especially when displaying user-submitted data or information obtained from external sources, if not properly processed, websites are easily vulnerable to cross-site scripting attacks (XSS) and other threats.AnQiCMS as a content management system that focuses on security, provides a powerful security escaping mechanism at the template rendering level, among which the `escape`, `e`, and `escapejs` filters are the key tools to ensure output security.AnQiCMS's template engine draws inspiration from Django

2025-11-08

How to enable or disable automatic escaping for specific code blocks in templates using the `autoescape` tag?

In AnQi CMS template development, how to balance flexibility and security when handling dynamic content is an important consideration.Amongst, the HTML automatic escaping mechanism (`autoescape`) plays a key role, which is aimed at preventing cross-site scripting (XSS) attacks, while also allowing us to display native HTML content when needed.Understand and master the usage of the `autoescape` tag, which will make your template creation more skillful.Understanding automatic escaping

2025-11-08

The `safe` filter in Anqi CMS template, when and why to use it to cancel the default escaping of HTML content?

Master the `safe` filter in AnQiCMS templates: unlock the correct rendering and safety boundaries of HTML content AnQiCMS, as an enterprise-level content management system based on Go language, adopts syntax similar to Django template engine in template design, which provides great flexibility for content display.When building website content, we often need to display the information edited on the backend on the front-end page.

2025-11-08

How does the `fields` filter split a single line string into an array of strings?

During the template development process of AnQi CMS, flexibly handling and displaying data is the key to improving website functionality and user experience.Sometimes, the content we receive from the backend may be a single line string containing multiple pieces of information, such as a list of keywords, product tags, or a set of feature descriptions.If we need to treat this information as separate elements for processing or display, for example, as a list, we would need a method to split this single string into multiple independent items.At this time, the `fields` filter provided by the Anqi CMS template engine can come into play.

2025-11-08