The `dump` filter has what practices in debugging Anqi CMS templates, and how to clearly view the structure and value of complex variables?

Calendar 👁️ 70

During the development of Anqi CMS templates, we often encounter the need to view variable content and structure, especially when dealing with complex objects passed from the background.If you cannot clearly know what is inside the variable, debugging will be as difficult as a blind man feeling an elephant.The AnQi CMS adopts the syntax of Django's Pongo2 template engine, providing rich tags and filters to help us build dynamic pages, one of which is an extremely powerful debugging tool isdumpfilter.

dumpFilter: The Perspective Mirror of the Template World

In AnQi CMS template syntax,dumpIt is a very practical built-in filter. Its main function is to clearly print the structure, data type, and current value of any variable. Unlike other filters such astruncatecharsUsed to truncate characters,dateUsed to format time) focuses on data transformation,dumpThe purpose is to reveal the original appearance of the data, which is particularly valuable during template debugging.

When we encounter a template variable and are unsure about which fields it contains or whether the value of a field is correct, we simply add|dumpIt is clear at a glance. Its usage is very intuitive:

{{ obj|dump }}

Among themobjIt is any variable you need to view.

WhydumpThe filter is a powerful tool for template debugging?

dumpThe filter is被誉为a CMS template debugging tool, thanks to its efficient performance in various complex scenarios:

  1. Exploring unknown variablesWhen you take over a new template project, or modify an unfamiliar module, the template may pass many unknown variables. At this time, you can try to use these uncertain variables at any location in the template.|dumpIt will immediately tell you whether the variable exists, what type it is, and its specific value, providing a clear direction for your subsequent code writing. For example, you may want to know whether the current page has a variable namedpageInfo:“

    <pre>{{ pageInfo|dump }}</pre>
    
  2. Parse complex object structure: The data model of Anqi CMS (such as articles, products, categories, etc.) often contains rich fields and nested structures. For example,archiveListTagging for obtainingarchivesover each element in the listitemOrarchiveDetailTagging for obtainingarchiveAn object, may be a complex structure with dozens, even more fields. If you need to access a certain deep field, but are unsure of the path, use the entire object directly|dumpIt can fully display its internal structure, including all available field names and their current values, which is much more efficient than guessing or looking up lengthy documents.

    {% archiveList archives limit="1" %}
        <p>第一个文档的详细结构和值:</p>
        <pre>{{ archives[0]|dump }}</pre>
    {% endarchiveList %}
    
    {% if archive %} {# 假设在文档详情页 #}
        <p>当前文档对象 archive 的完整结构:</p>
        <pre>{{ archive|dump }}</pre>
        <p>当前文档所属分类 category 的完整结构:</p>
        <pre>{{ archive.Category|dump }}</pre>
    {% endif %}
    

    By such an output, you can clearly seearchiveObjects haveId/Title/Contentand basic fields, as well as possibly existingCategorynested objects, etc., greatly simplifying the exploration process of data access paths.

  3. Debugging condition judgment and loop errorsWhen your{% if ... %}condition does not take effect, or{% for ... %}when the loop result is not as expected, it is likely because the variable values involved in the judgment or loop do not meet expectations. Use|dumpIt allows you to view the exact value of related variables before and after conditional judgments, or within each iteration of a loop. For example, if aifstatement depends onitem.StatusIs it1And it always fails, you can inifAdd before the statement{{ item.Status|dump }}To checkStatusThe actual value.

  4. Understand nested data structures: Many of the data in Anqi CMS are nested, such asarchiveAn object may containCategoryan object,CategoryAn object may also containParentIdetc. When you need to accessarchive.Category.TitleifCategoryAn object itself may be empty or incorrectly structured,dumpCan help you delve deeper layer by layer until you find the root cause.

  5. Avoid front-end rendering interference.: Sometimes, the content of a variable may be hidden by CSS styles or dynamically modified by JavaScript.dumpDirectly output the original text representation of the variable, without HTML parsing and style rendering, ensuring that you see the most genuine data transmitted from the backend, and avoiding interference from the frontend environment.

How to use efficientlydumpFilter?

In order to make better use ofdumpFilter, here are some practical suggestions:

  • Combine<pre>Tag: Due todumpThe output is the original structured text, the browser defaults to rendering it as plain text, which may cause formatting chaos. Wrap it in<pre>Within tags, the original text format (including line breaks and indentation) can be retained to make it easier to read in the browser.
    
    <pre>{{ someComplexVariable|dump }}</pre>
    
  • Iterate through the loop to view: When debugging list data, you can useforLoop internally for eachitemperformdumpTo check the specific content of each element.
    
    {% archiveList archives limit="3" %}
        {% for item in archives %}
            <h3>{{ item.Title }}</h3>
            <pre>{{ item|dump }}</pre>
        {% endfor %}
    {% endarchiveList %}
    
  • Accurately locate the problem scopeIf you know that the problem may be in a certain sub-object or field within a large object, you can directly use the sub-object|dumpThis is not for the entire large object. It can reduce the amount of output and quickly find the key information.

Important notes: in the production environment.dumpFilter

dumpThe filter is powerful, but it is mainly used forDevelopment and debugging phase. Before deploying the website toBe sure to remove alldumpcalls. The reason is as follows:

  • Security risk:dumpIt would expose the internal data structure and sensitive information of the website, which could be exploited by malicious users.
  • Performance impactPrinting complex variables consumes additional server resources and network bandwidth, which may cause page loading speed to slow down.
  • User Experience: Debug information will be displayed directly on the front-end page, affecting the normal browsing experience of users.

Therefore, after debugging is completed, please carefully check the template file to ensure that all{{ ...|dump }}The code has been deleted or commented out.

In summary,dumpThe filter is a powerful tool for Anqin CMS template developers, providing a direct and efficient way to inspect the internal structure and values of template variables.Mastering its usage will greatly enhance your efficiency in developing and debugging Anqi CMS templates.


Frequently Asked Questions (FAQ)

**Q1:dumpCan the filter

Related articles

How to safely output HTML code passed from the backend in Anqi CMS template using the `safe` filter without escaping?

Build and manage websites in AnQi CMS, we often make good use of its powerful and flexible template system to present colorful content.The template engine of AnQi CMS has adopted the syntax of Django templates, which brings us a familiar development experience and powerful features.However, when handling HTML code passed from the backend to the frontend, we encounter an important security mechanism: **automatic escaping**.

2025-11-08

How to automatically identify URLs and email addresses in the text and convert them into clickable links in `urlize` and `urlizetrunc` filters in AnQi CMS?

In Anqi CMS, we often encounter scenarios where we need to display website or email addresses in the article content, comments, or other user input text.If these addresses are only plain text, users will not be able to click and jump directly, which will greatly reduce the user experience of the website.Fortunately, Anqi CMS is built-in with powerful template filters, among which `urlize` and `urlizetrunc` are specifically designed to solve this problem. They can automatically convert identified URLs and email addresses into clickable HTML links.

2025-11-08

How to use the `wordwrap` filter in AnQi CMS template to automatically wrap long text and avoid layout chaos or overflow?

In website content operation, we often encounter situations where the text content is too long, causing page layout chaos, even overflowing the container, which seriously affects the user experience.Especially when a continuous English word without spaces or a long string of Chinese characters appears in a narrow area, the default line break mechanism of the browser may not meet our design requirements.Fortunately, AnQi CMS provides a very practical tool for template developers to elegantly solve this problem - that is the `wordwrap` filter.

2025-11-08

How to quickly count the number of words (or Chinese words) in the content of an article in AnQi CMS using the `wordcount` filter, for SEO or reading time evaluation?

In website operation, the quality and presentation of content are crucial.The number of words in an article not only affects its performance in search engines, but is also a key indicator for evaluating user reading experience and estimating reading time.No doubt, manually counting the number of words in content is a tedious task, especially when the amount of content is vast.Luckily, AnQiCMS (AnQiCMS) has provided us with a small yet powerful tool—the `wordcount` filter, making content word count easy. ### The Importance of Word Count in Content

2025-11-08

How to concatenate the article title (Title) with the system-defined website name (SiteName) through the `tdk` filter and output it to the <title> tag in the Anqi CMS template?

In website operation, the importance of the `<title>` tag is self-evident. It is not only the key for search engines to understand the theme of the page, but also the content that users see first in the search results.A well-crafted page title that can effectively increase click-through rates and have a positive impact on SEO optimization.Therefore, how to organically integrate the core title of the article with the brand name of the website to form an accurate and attractive `<title>` tag is a fundamental and important link in the design of website templates.AnQiCMS as a feature-rich enterprise-level content management system

2025-11-08

How to configure the separator (`sep`) and whether to display the parent category title (`showParent`) for the `tdk` filter in Anqin CMS?

During website operations and SEO optimization, the page title (Title) is a key element to attract user clicks and improve search engine rankings.AnQiCMS provides a flexible `tdk` filter, allowing us to finely control the display of page TDK (Title, Description, Keywords).Among these parameters, `sep` and `showParent` play a crucial role in building page titles with rich hierarchy and clarity.

2025-11-08

How to use the `stringformat` filter in Anqi CMS template to format price numbers as currency (such as “¥%.2f”)?

When displaying product or service prices on a website, we all hope that they look clear and professional, easy to understand.A sequence of price numbers without currency symbols and inconsistent decimal places, which not only affects the appearance but may also raise doubts about the professionalism of the product. 幸运的是,AnQiCMS(AnQiCMS)强大的模板引擎提供了多种实用的过滤器,其中 `stringformat` 过滤器就是将数字格式化为标准货币形式的利器。

2025-11-08

How does the `stringformat` filter handle null or invalid input in AnQi CMS, will it return an error or a default value?

In Anqi CMS template design, we often encounter scenarios where we need to format variable output.At this point, the `stringformat` filter is particularly important, as it helps us clearly display numbers, strings, and even other types of data according to the format we preset.However, when using such tools, a common and crucial question is: how will `stringformat` handle it when the variable itself is null or invalid?Is it a direct error that causes the page to crash, or is there a more elegant default behavior?

2025-11-08