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—the 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 value of a variable becomes the key to troubleshooting and accelerating the development process.
The template engine of AnQi CMS provides many practical filters (filters), one of which is particularly useful for debuggingdumpFilter.It's like an X-ray machine that can penetrate the surface of variables, clearly displaying its detailed information including the underlying data structure, type, and the specific value stored at the moment.This provides great convenience for us to understand the actual form of the data in the template, thus accurately locating the problem.
How to usedumpFilter view variable structure and value?
dumpThe usage of the filter is very intuitive and simple. Just add it after the variable you need to check.|dumpYou can. For example, suppose we are traversing a variable namedbannersa list, where each element is aitemto view a singleitemdetailed structure and values, we can write the template code in this way:
{% for item in banners %}
<div>{{ item|dump }}</div>
{# ... 模板的其他内容 ... #}
{% endfor %}
Or, if we need to view a single variable, such asarchiveto get all the properties of an object, we can do this:
<div>{{ archive|dump }}</div>
When you access the page containing this code in a browser, the detailed information of the variable will be output directly on the page. For example, ifitemis aBannerItemAn object of 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 the variableFor example,
&config.BannerItemindicating that it is a pointer toconfigUnder packageBannerItemPointer of the structure. - Fields included: Like
Logo/Id/Link/Alt/DescriptionandType, these areBannerItemAttributes defined in the structure. - The current value of the field: Each 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",IdThe value of1.
By this information, we can quickly judge:
- Whether the variable is empty (if it is empty,)
dumpit may not output any content or output an empty value). - Whether the variable is the expected data type.
- Whether the variable contains all the fields we need.
- Whether the value of each field is correct, for example, whether the image URL is valid, and whether the ID matches.
Actual application scenarios
dumpThe application scenarios of filters in template debugging are very extensive:
- Data missing troubleshootingWhen a certain template
{{ variable.Field }}does not display any content, first{{ variable|dump }}you can checkvariableIs itnil, orFieldExists and has a value. - Check inside the loop body: When processing list data, for example, using
archiveListorcategoryListLabel loop output content, within the loop{{ item|dump }}Check each one by oneitemThe data, ensuring that each element's structure and value meet the expectations. - Complex object exploration: For like
archive(Document) orcategory(Category) such a complex object that includes multiple levels of data directly{{ archive|dump }}can help us quickly understand all the accessible properties it contains, even the structure of nested child objects. - Function or tag return value validation: Sometimes we are unsure about the data format returned by a custom tag or function, using
dumpwe can directly see its return result.
Precautions
AlthoughdumpThe filter function is powerful, but it should only be used as an auxiliary tool during development and testing stages. Since it exposes the internal data structures and sensitive information of the system,Absolutely should not keep it in the production environment after going onlineMake sure to clear all debugging information before deploying it onlinedumpFilter code to ensure the security and data privacy of the website.
MasterdumpFilter, it will significantly improve your debugging efficiency in the AnQi CMS template development, allowing data issues to be exposed, thus enabling you to build websites that meet requirements and run stably much faster.
Frequently Asked Questions (FAQ)
Q1:dumpThe filter output too much content, how to only view the parts of interest?A:dumpThe filter outputs the complete structure and value of the variable, which can indeed result in very long output for large or complex objects. If you are only interested in a specific property, you can directly use it on that property.dumpFilter, for example{{ item.Title|dump }}instead of the wholeitemObject.In addition, using the developer tools (usually opened by pressing F12) in the 'Elements' or 'Console' tab makes it easier to find and collapse output content, improving viewing efficiency.
Q2: Why did I use{{ variable|dump }}, but nothing is displayed on the page?A: If nothing is displayed on the pagedumpThe output of the filter, usually has 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-level scope (such as in the parent template that calls the current template) or with more fundamental variables.dumpto gradually narrow down the troubleshooting range.
Q3:dumpCan the filter be used in a production environment?A: No.dumpThe filter is designed for development and debugging, it exposes the internal data structure, types, and variable values that may contain sensitive information. It should be retained in a production environment.dumpThe filter can pose serious security risks and potential data leakage issues. Therefore, it is imperative to thoroughly check and remove all filters before any website goes live.dumpThe use of filters.