During the template development process of AnQi CMS, we often encounter the need to view variable content and structure, especially when dealing with complex objects passed from the background.If it is not clear what is inside the variable, debugging will be as difficult as a 'blind man feeling an elephant'.dumpFilter.
dumpFilter: Template World's Perspective Mirror
In the template syntax of AnQi CMS,dumpIt is a very practical built-in filter. Its main function is to clearly print out the structure, data type, and current value of any variable. It is similar to other filters such astruncatecharsUsed to truncate characters.dateUsed for time formatting) focuses on data conversion, which is different.dumpThe function is to reveal the original appearance of the data, which is particularly valuable in template debugging.
When we encounter a template variable and are uncertain about which fields it contains, or whether the value of a certain field is correct, we can simply add|dumpIt is self-evident. Its usage is very intuitive:
{{ obj|dump }}
Among themobjIt is any variable you need to check.
WhydumpIs the filter a powerful tool for template debugging?
dumpThe filter is hailed as a powerful tool for debugging Anqi CMS templates, thanks to its efficient performance in a variety of complex scenarios:
Exploring unknown variablesWhen you take over a new template project, or modify an unfamiliar module, many unknown variables may be passed in the template. At this time, you can try to use these uncertain variables at any position 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 namedpageInfoa variable:<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,
archiveListtags obtainedarchivesin the listitem, orarchiveDetailtags obtainedarchiveAn object, may be a complex structure with dozens of fields, even more. If you need to access a deep field within it but are unsure of the path, you can directly use the entire object.|dumpIt can completely display its internal structure, including all available field names and their current values, which is much more efficient than guessing or consulting 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 %}Through such output, you can clearly see
archivethe object hasId/Title/Contentbasic fields such as, as well as possibleCategorynested objects, greatly simplifying the exploration process of the data access path.Check condition judgment and loop errorsWhen you
{% if ... %}The condition does not take effect, or{% for ... %}The loop results are not as expected, it is likely because the values of the variables involved in the judgment or loop do not meet expectations. Use|dumpCan let you view the exact value of related variables before and after conditional judgments, or inside each iteration of a loop. For example, if aifstatement depends onitem.StatusIs it1,and it always fails, you can addifbefore the statement{{ item.Status|dump }}Check in.Statusactual value.understanding nested data structures:Many of the data in Safe CMS are nested, for example
archiveThe object may containCategoryan object,CategoryThe object may also containParentIdetc. When you need to accessarchive.Category.TitleifCategoryThe object itself is empty or incorrectly structured,dumpit can help you delve deeper layer by layer until you find the problem.to avoid interference from frontend renderingEnglish: 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 authentic data transmitted from the backend, and avoiding interference from the frontend environment.
How to use efficientlydumpFilter?
To better utilizedumpFilter, here are some practical suggestions:
- Combine
<pre>tagsDue todumpOutput is the original structured text, browsers will render it as plain text by default, which may cause formatting to become chaotic. Wrap it in<pre>Tags can preserve the original format of text (including line breaks and indentation), making it easier to read in the browser.<pre>{{ someComplexVariable|dump }}</pre> - Iterate through the loop to view: When debugging list data, you can in
forLoop internally for eachitemperformingdumpto 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 the problem may be in a certain sub-object or field of a large object, you can directly use the sub-object
|dumpEnglish instead of the entire large object. This can reduce the output volume and find the key information faster.
Important notes: In the production environment,dumpFilter
dumpThe filter is powerful, but it is mainly used forDevelopment and debugging phaseBefore deploying the website toEnsure that all are removed before going to thedumpcallReasons are as follows:
- Security risks:
dumpThis may 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 lead to slower page loading speed.
- 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 files to ensure that all{{ ...|dump }}The code has been deleted or commented out.
In short,dumpThe filter is a powerful tool for AnQi CMS template developers, providing a direct and efficient way to explore the internal structure and values of template variables.Master its usage, and it will greatly enhance your efficiency in the development and debugging process of Anqi CMS templates.
Common Questions (FAQ)
**Q1:dumpCan the filter