How to debug template in AnQiCMS and view the structure and value of variables (using dump filter)?

Calendar 👁️ 74

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? And what is the specific value?If these questions cannot be answered quickly, the debugging process will become particularly cumbersome and time-consuming.Fortunately, AnQiCMS has provided us with a powerful and convenient debugging tool——dumpA filter that can help 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 flexible and efficient.It is crucial to understand the data structure passed to the template during development.dumpThe filter is exactly 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 may know that the variable name isitem, but you are not sureitemWhen you don't know what fields are inside, guessing one by one or looking up the documentation is very inefficient. At this point,dumpa filter can come into play.

Its usage is very simple, just add it after the variable you need to view in the template|dumpand it is done:

{{ your_variable|dump }}

When you add this code to your.htmlAt any position in the template file, such as inside a loop, or in the main content area of the detail page, after saving and refreshing the page, you will see the detailed structured output of the 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 thisarchiveListtags to traverse the 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 an output similar to this (the specific content will vary according to 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)}

We can clearly see from this output:itemIs amodels.ArchiveA type of structure that includesId/Title/Link/Thumb/Descriptionand many fields with their specific values. With this information, you can accurately use them in the template{{ item.Title }}/{{ item.Thumb }}to render the corresponding data.

Similarly, on the article detail page, if you want to understandarchiveTo view the complete content of this global variable, simply place:

{{ archive|dump }}

This will print all the details of the current article, including custom fields, etc., to help you better organize the page layout.

Debugging custom fields and complex objects

AnQiCMS provides flexible content model custom field functionality. If you have added custom fields such as "author bio" or "product parameters" for the article model in the background, but forgot how to call them in the template, dumpThe filter can also provide help.

You might use it on the document details page.archiveParamsUse tags 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 this:

&models.ArchiveParam{Name:"产品特性", FieldName:"product_feature", Value:"轻量级,高性能", Type:"text"}

This clearly tells you, eachitemContainsName(field display name) andValue(Field value), allowing you to correctly display custom content.

UsedumpTip for the filter

  • Use with caution, remove promptly: dumpThe filter is very useful in the development and debugging stage, but it will print a large amount of debugging information to the page, which may expose sensitive data and increase the rendering overhead.Therefore, it must be removed from the template file before the website goes live or is deployed to the production environment.
  • Cooperate with the browser developer tools: dumpThe information of the Go struct output is sometimes too long, it is not convenient to read directly on the web.You can combine the browser's developer tools (F12) to view the outputs in the "Elements" or "Console", or copy them to a text editor to use its highlighting feature for辅助 reading.

MasterdumpThe filter will greatly enhance your debugging efficiency in AnQiCMS template development, allowing you to focus more on content presentation and user experience optimization.


Frequently Asked Questions (FAQ)

1.dumpThe filter outputs too much information, making it difficult to see the specific variable structure and values. Is there a way to simplify the output?

Answer:dumpThe filter will output the complete structure and value of the variable, and if the variable contains a large amount of data, it can indeed be messy. In this case, you can consider only processing a sub-attribute or a specific field of the variable.dumpFor example{{ item.Title|dump }}To only view the type of the title. Additionally, you can try combining other filters such asstringformat:"%#v"To output the source code snippet in Go language, which can sometimes be clearer, or usesliceFilter a portion of the array/substring to view, for example{{ archives|slice:":3"|dump }}Only view the first three elements.

2. Why did I usedumpthe filter in the template, and the page shows no change?

Answer: This is usually due to 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 under the 'System Settings' related menu), and click to clear the system cache.Also, it is recommended to clear the browser cache (press Ctrl+F5 to force refresh the page) to ensure that the latest template file is loaded.

3. Used incorrectly in production environmentdumpWhat risks does the filter have?

Answer: Used incorrectly in production environmentdumpThe filter has the following risks:

  1. Information leakage: dumpThis will output all the internal structure and values of the variables, which may include database IDs, API keys, user sensitive information, etc., and once malicious users obtain them, it may cause serious security issues.
  2. 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, and significantly increase the size of the page file, thereby reducing the website loading speed, affecting user experience and SEO performance.
  3. Page is confused:A large amount of debugging information is 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.dumpThe filters are strongly not recommended, be sure to thoroughly check and remove all debug code before deployment.

Related articles

How to generate random text of a specified length as placeholder content in AnQiCMS template?

## Generate random text of a specified length as placeholder content in AnQiCMS template During website development and content operation, we often encounter scenarios where we need to fill in placeholder content (Placeholder Content).Whether it is designing a new template, testing page layout, or giving an initial preview when the content is not fully ready, placeholder text can help us quickly build prototypes, intuitively evaluate the visual effects, and do not need to wait for the real content to be filled in.

2025-11-08

How does AnQiCMS filter or replace external links in content to standardize display?

External links contained in website content, if not managed properly, may have a negative impact on the website's search engine optimization (SEO) effect, user experience, and even the quality of the content.AnQiCMS as a comprehensive content management system, provides a series of powerful and flexible tools to help you effectively filter or replace external links in the content, ensuring the standardized display of website content.

2025-11-08

How to retrieve and display details of a specific user group (such as VIP level) in AnQiCMS template?

In AnQiCMS, managing users and content, we often encounter the need to display different information based on the user's identity (such as VIP level).The powerful template engine and user group management function of AnQiCMS makes it very intuitive and efficient to achieve this goal. Today, let's discuss how to retrieve and flexibly display detailed information of specific user groups in the AnQiCMS template, such as the name of the VIP level.

2025-11-08

How to display the visitor message form and manage the message results in AnQiCMS?

Maintaining interaction with website visitors, collecting user feedback is a very important link that each website operator attaches great importance to.Whether it is to answer doubts, collect suggestions, or provide customer service, an efficient and convenient comment system can greatly enhance user experience and operational efficiency.AnQiCMS fully understands this and provides us with a simple and efficient visitor message function, making the entire process from form display to result management easy and effortless. ### Easily Build Visitor Message Form AnQiCMS uses flexible template tags to display visitor message forms on the website frontend.

2025-11-08

How to safely extract strings or HTML content and add an ellipsis in AnQiCMS templates?

When operating a website, we often need to display article summaries, product descriptions, or user comments and the like.This text, if not processed, is displayed directly and completely. Long text may disrupt the page layout, affecting the overall aesthetics and information transmission efficiency.This is particularly important to truncate the content appropriately and add an ellipsis.AnQiCMS as an efficient and customizable content management system, its powerful template engine developed based on the Go language provides us with a simple and intelligent solution.Pass through built-in filters (filters)

2025-11-08

How to format a timestamp into the display format of "Year-Month-Day" in AnQiCMS?

In daily website operations, the display format of dates is crucial to user experience.A clear, consistent, and easy-to-understand date format that allows visitors to easily obtain information and enhance the professionalism of the website.For users of AnQiCMS, we often encounter data stored in the form of timestamps for content publishing time, update time, and so on. How to convert these long strings of numbers into a friendly display format such as 'Year-Month-Date' is an indispensable part of content presentation.

2025-11-08

How can simple mathematical arithmetic operations be performed in the AnQiCMS template to display the result?

Handling arithmetic operations in AnQiCMS templates is an important link to the dynamic and personalized display of website data.No matter whether you need to calculate the total price of goods, display discount information, or make conditional judgments based on certain values, AnQiCMS's powerful template engine can provide flexible support.Thanks to its Django template engine features, simple mathematical arithmetic operations can be performed directly in the template without complex code logic.### Directly perform expression calculation in the template AnQiCMS template engine supports double braces `{{ }}`

2025-11-08

How to split a string into an array by a specified separator and iterate and display it in the template in AnQiCMS?

In website operations and content management, we often encounter such scenarios: a certain field in the background stores multiple related information, such as multiple keywords of an article, various functional tags of a product, or multiple aliases of an author.This information is usually connected into a string with commas, semicolons, or other specific symbols.When it is necessary to display this information one by one or perform style processing in the website front-end template, it is necessary to split this string into independent items and then traverse and display them one by one.AnQiCMS provides a powerful and flexible template engine, its syntax style is similar to

2025-11-08