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:"http://127.0.0.1:8001/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::Like
Logo/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 example
LogoThe value of the field is"http://127.0.0.1:8001/uploads/202309/14/eba5aa9dc4c45d18.webp",Idhas a value of1.
With this information, we can quickly judge:
- whether the variable is empty (if it is empty,
dumpMay not output anything or output an empty value). - Is the variable the expected data type?
- Does the variable contain all the fields we need?
- 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, using
archiveListorcategoryListtags 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 as
archive(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, using
dumpwe 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.