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:
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>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 see
archiveObjects haveId/Title/Contentand basic fields, as well as possibly existingCategorynested objects, etc., greatly simplifying the exploration process of data access paths.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.Understand nested data structures: Many of the data in Anqi CMS are nested, such as
archiveAn 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.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 use
forLoop 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