When developing website templates, we often encounter such confusion: what data does a variable in the template contain?What is its structure?What is the type?How much is the specific value?These questions, if not answered quickly, will make the debugging process extremely tedious and time-consuming.dumpA filter that helps us easily reveal the 'true face' of variables.
AnQiCMS is developed in Go language and uses a template engine syntax similar to Django, which makes template writing both flexible and efficient.It is crucial to understand the data structure passed to the template during the development process.dumpThe filter is born for this, it can print the structure type and current value of any variable, making it clear at a glance.
dumpFilter: Your template debugging helper
Imagine you are writing a product list page, and you want to display the title, image, and description of each product. You might know the variable name isitem, but when in doubtitemWhen trying to figure out what specific fields are available, guessing one by one or consulting the documentation can be inefficient. At this time,dumpthe filter can come into play.
The usage is very simple, just add it after the variable you want to view in the template|dumpas follows:
{{ your_variable|dump }}
when you add this code to your.htmlAt any position in the template file, such as within a loop or in the main content area of a detail page, after saving and refreshing the page, you will see the detailed structured output of this variable on the web page.This output is usually presented in the form of a Go language struct, clearly showing all the field names, data types, and current values of the variables.
For example, when developing an article list, we might use it like this:archiveListTags to iterate over articles:
{% archiveList archives with type="list" limit="10" %}
{% for item in archives %}
{# 在这里插入dump过滤器,查看单个文章对象 item 的结构和值 #}
{{ item|dump }}
<p>{{ item.Title }}</p>
<img src="{{ item.Thumb }}" alt="{{ item.Title }}">
<p>{{ item.Description }}</p>
{% endfor %}
{% endarchiveList %}
When you visit this page, you will see output similar to this in your browser (the specific content will vary depending on your data):
&models.Archive{Id:1, Title:"AnQiCMS模板制作指南", SeoTitle:"", Link:"/article/1.html", Keywords:"AnQiCMS,模板", Description:"这是一篇关于AnQiCMS模板制作的详细指南...", Content:"<p>...</p>", ModuleId:1, CategoryId:2, UserId:1, ParentId:0, Price:0, Stock:0, ReadLevel:0, OriginUrl:"", Views:123, Flag:"c", Images:[]string{"https://en.anqicms.com/uploads/thumb/1.webp"}, Logo:"https://en.anqicms.com/uploads/thumb/1.webp", Thumb:"https://en.anqicms.com/uploads/thumb/1.webp", CommentCount:5, Status:1, CreatedTime:1678886400, UpdatedTime:1678886400, DeletedAt:(*time.Time)(nil)}
From this output, we can clearly see:itemis amodels.ArchiveA structure of types, which includesId/Title/Link/Thumb/Descriptionand many fields, as well as their specific values. With this information, you can accurately use{{ item.Title }}/{{ item.Thumb }}to render the corresponding data.
Similarly, on the article detail page, if you want to understandarchiveTo get the full content of this global variable, just place it simply:
{{ archive|dump }}
This will print out all the details of the current article, including custom fields, etc., to help you better organize the page layout.
Debug custom fields and complex objects
AnQiCMS provides a flexible content model custom field feature. If you have added custom fields like 'Author Introduction' or 'Product Parameters' to the article model in the background, but forgot how to call them in the template, dumpThe filter can also provide assistance.
You might use it on the document detail page.archiveParamsTags can be used to get custom parameters. Use them within its loop.dump:
{% archiveParams params %}
{% for item in params %}
{{ item|dump }}
<div>
<span>{{ item.Name }}:</span>
<span>{{ item.Value }}</span>
</div>
{% endfor %}
{% endarchiveParams %}
You might see something like:
&models.ArchiveParam{Name:"产品特性", FieldName:"product_feature", Value:"轻量级,高性能", Type:"text"}
This clearly tells you, eachitemincludesName(field display name) andValue(field value), allowing you to correctly display customized content.
UsedumpTips for the filter
- Use with caution, remove promptly:
dumpThe filter is very useful in the development and debugging stages, but it may print a large amount of debugging information to the page, potentially exposing sensitive data, and it will increase the page rendering overhead.Therefore, be sure to remove it from the template file before the website goes live or is deployed to the production environment. - Use in conjunction with the browser developer tools:
dumpThe Go struct information output sometimes is quite long, which is not convenient to read directly on the web.You can combine the browser's developer tools (F12), view these outputs in the 'Elements' or 'Console', or copy them to a text editor to use its highlighting feature to assist in reading.
MasterdumpFilter, which will greatly enhance your debugging efficiency in AnQiCMS template development, allowing you to focus more on content presentation and user experience optimization.
Common Questions (FAQ)
1.dumpThe output information of the filter is too much, making it difficult to see the specific variable structure and values. Is there a way to simplify the output?
Answer:dumpThe filter outputs the complete structure and value of the variable. If the variable contains a large amount of data, it can indeed appear cluttered. In this case, you may consider filtering only a specific sub-attribute or a particular field of the variable.dumpfor example{{ item.Title|dump }}to view only the title type. Moreover, you can also try combining other filters likestringformat:"%#v"to output in the form of Go language source code snippets, which can sometimes be clearer, or usesliceFilter a portion of the array/strings to view, for example,{{ archives|slice:":3"|dump }}Only view the first three elements.
2. Why did I use a filter in the template?dumpAfter the filter, there is no change on the page?
Answer: This is usually caused by the cache mechanism of AnQiCMS.When you modify the template file, AnQiCMS may still be using the old cache file to render the page.You need to log in to the AnQiCMS backend, find the 'Update Cache' feature (usually at the bottom of the sidebar or related menus under 'System Settings'), and click to clear the system cache.At the same time, it is also recommended to clear the browser's cache (press Ctrl+F5 to force refresh the page) to ensure that the latest template file is loaded.
3. Misused in production environmentdumpWhat risks does the filter have?
Answer: Misused in production environmentdumpThe filter has the following risks:
- Information leakage:
dumpThis will output all internal structures and values of the variable, which may include database IDs, API keys, user sensitive information, etc. Once obtained by malicious users, it may cause serious security issues. - Performance impact:
dumpThe filter needs to serialize the variable and output it to the page, which will increase the CPU and memory overhead of the server, significantly increase the size of the page file, and thus reduce the website loading speed, affecting user experience and SEO performance. - Page confusion:A large amount of debugging information will be displayed directly on the page, destroying the page layout and design, and providing a poor experience for visitors.
Therefore, it should not be used in any non-development or debugging environment.
dumpFilters are strongly not recommended, be sure to thoroughly check and remove all debug code before deployment.