How to debug variable content and type in AnQiCMS template (using dump filter)?

Calendar 👁️ 71

When using AnQiCMS to build websites and customize templates, we often encounter situations where the content displayed on the page is not as expected, or it seems that a variable has not been passed correctly.At this moment, it is particularly important to be able to quickly view the actual content and data type of variables in the template.Fortunately, AnQiCMS's template engine (which adopts a syntax similar to Django) provides us with a very practical tool -dumpFilter, helping us easily reveal the mysteries of variables.

Core function:dumpThe wonder of filters

dumpThe filter acts like an X-ray machine, able to penetrate the appearance of template variables and directly print out their internal structure, data type, and current value. When you are confused about the value of a variable or whether it is the type you expect, dumpYour helper.

UsedumpThe filter is very simple, just apply it to the variable you want to check. Its basic syntax is:

{{ 你的变量 | dump }}

For example, suppose you are iterating over a list of images and want to view the specific information of each image item, you can do it like this:

{% bannerList banners %}
    {% for item in banners %}
        <a href="{{item.Link}}" target="_blank">
            <img src="{{item.Logo}}" alt="{{item.Alt}}" />
            <h5>{{item.Title}}</h5>
        </a>
        {# 就在这里,我们使用dump过滤器来检查当前的item变量 #}
        <pre>{{ item | dump }}</pre>
    {% endfor %}
{% endbannerList %}

After you refresh the page, you may see similar output below each image item:

&config.BannerItem{Logo:"https://en.anqicms.com/uploads/202309/14/eba5aa9dc4c45d18.webp", Id:1, Link:"", Alt:"Hello", Description:"", Type:"default"}

This line of output contains rich information:

  • &config.BannerItem: This indicatesitemA variable is a pointer toconfig.BannerItemPointer to a structure. This is the type defined by the AnQiCMS bottom layer Go language, allowing you to clearly know where the object comes from.
  • {Logo:"...", Id:1, Link:"", Alt:"Hello", Description:"", Type:"default"}This lists directly.BannerItemThe names of all public fields in the structure and their current values. You can check them immediatelyLogo/Id/Link/AltWhether the values of these fields meet the expectations.

In this way, whether it is a string, number, boolean value, or more complex arrays, slices, maps (map) or structures,dumpThey can clearly show their true colors.

Advanced debugging: combined with other filters

exceptdumpCombined with other practical filters, it can further improve your debugging efficiency:

  1. stringformat:"%#v"Output in Go language source code formatIfdumpThe output may not be clear enough to you, or you may want to view the variables in a way closer to the Go language source code declaration, you can usestringformatFilter and pass in%#vParameters. It will output the variable in the Go language struct syntax, which can also help you understand the underlying representation of variables in depth.

    <pre>{{ item | stringformat:"%#v" }}</pre>
    
  2. length: Check the length of a set or stringWhen you are dealing with lists, arrays, or strings, and you doubt whether they are empty or do not meet the expected length,lengthThe filter can be useful.

    {% archiveList archives with type="list" limit="10" %}
        {# 检查列表是否为空 #}
        {% if archives|length == 0 %}
            <p>没有找到任何文档。</p>
        {% else %}
            {% for item in archives %}
                {# 检查文档标题的长度 #}
                <p>文档标题:{{ item.Title }} (长度: {{ item.Title|length }})</p>
            {% endfor %}
        {% endif %}
    {% endarchiveList %}
    

    It can help you quickly determine if an element is in the set or if a string contains content.

  3. safe: Processes content containing HTML.AlthoughdumpThe filter usually outputs plain text, but when you debug variables (such as article contentitem.ContentWhen the content itself contains HTML structure, you may need to ensure that it is parsed correctly by the browser rather than displayed as plain text.safeThe filter is born for this. During debugging, if the HTML of the content variable is not rendered correctly, you can try to usesafeto exclude escaping issues.

    {# 如果archiveContent包含HTML,确保其被正确解析 #}
    <div>文档内容:{{ archiveContent | safe }}</div>
    

Actual operation process and scenarios

Generally, you would follow the following steps to usedumpPerform variable debugging:

  1. Determine the debugging target:Find the abnormal part displayed on the page, locate the template file involved (usually in/templatein the directory).
  2. Insertdumpcode:Insert near the variable you suspect has a problem, or inside a loop.{{ 你的变量 | dump }}If you are in a loop, each loop item will output its information.
  3. Refresh the page:After saving the template file, refresh the browser page. If the page does not update immediately, please try clearing the AnQiCMS backend cache.
  4. Analysis output:Check carefullydumpInformation output on the page.
    • Does the variable exist?If the variable isnilor empty,dumpIt will display an empty value ornil.
    • Is the type correct?For example, you expect a string, but it shows up as a number, or as a complex structure.
    • Does the value meet the expectation?Check each field's specific value for correctness.
  5. Modify and verify:Based ondumpAdjust your backend data, template tags, or variable logic based on the provided information.
  6. removedumpcode:Be sure to remove from the template after debugging is complete.dumpA filter to prevent the leakage of sensitive information or affect page performance.

dumpA filter is useful when dealing with complex data structures, such as fromarchiveListorcategoryListList items obtained from tags. You can see eachitemAll properties of the object, including its associated ID, title, link, thumbnail, etc., so as to quickly locate the problem.

Points to note

  1. Do not retain in the production environment for a long time: dumpThe filter will expose the internal data structure and content of your website, which is a great security risk. Please remove it from the template after debugging is complete.
  2. Delete after use:Suggest to delete immediately after debuggingdumpCode, develop good habits.
  3. Update the cache in time:Sometimes after modifying the template, the front-end page may not take effect immediately. Remember to go to the AnQiCMS background, use the 'Update Cache' feature to clear the system cache, and ensure

Related articles

How to implement pagination display and like function for comment content in AnQiCMS?

In website operation, user comments and interaction are the key to enhancing the vitality of content.AnQiCMS (AnQiCMS) provides powerful content management capabilities, among which the comment feature is an important bridge for user interaction with content.This article will discuss in detail how to implement pagination for comment lists in Anqi CMS, as well as add a like function for comment content, enhancing the interactive experience of your website to a higher level.### CMS Review Function Overview The CMS has built-in article comment and comment management functions from the early version, which provides native user interaction support for the website

2025-11-08

How to enable anti-crawling interference code and image watermark in AnQiCMS to protect the display of content?

In today's era of increasingly rich digital content, protecting original content on websites is particularly important.AnQiCMS (AnQiCMS) understands the pain points of content creators and corporate users, and has specifically built anti-crawling interference codes and image watermarking functions to help users effectively prevent their content from being maliciously scraped and stolen, while also strengthening brand identity.### Protect content security: Enable anti-capture interference code The value of original content is self-evident, but information collection tools on the Internet may copy your articles in large quantities without permission, which not only harms the rights and interests of the original creators but may also divert website traffic

2025-11-08

How to safely parse a URL string into a clickable a tag in AnQiCMS template?

In website content operation, we often need to display some website URLs, email addresses, and other information in articles, introductions, or custom fields.If this information is just plain text, users cannot click to jump directly, which not only affects the user experience but may also reduce the interactivity of the content and the SEO friendliness of the website.Convert these URL strings securely and intelligently into clickable `<a>` tags, which is a very practical feature in the AnQiCMS template.AnQiCMS as an enterprise-level content management system has fully considered the flexibility and security of content display from the beginning of its design

2025-11-08

How to implement the filtering function of the article list in AnQiCMS, displaying different results according to custom parameters?

How to make it easier for users to find the content they need on the website is the key to improving user experience and conversion rates.The filtering function of the article list is an important means to achieve this goal.AnQiCMS provides a flexible and powerful mechanism that allows us to easily filter and display different list results of articles based on custom parameters, which is very helpful for building personalized and efficient content platforms.### The Foundation of Building a Filtering Function: Custom Content Model To implement an article list filter based on custom parameters

2025-11-08

How to call data from other sites in a multi-site environment using the siteId parameter?

In the multi-site environment of AnQiCMS, data intercommunication is a key factor in improving operational efficiency and achieving content integration.Sometimes, we may need to display the content of a sub-site on the main site, or call the contact information of another brand site in a business site.At this time, the `siteId` parameter provided by AnQiCMS is particularly important, as it helps us easily implement cross-site data calls and display.### Understanding the multi-site advantages of AnQiCMS AnQiCMS was initially designed with the need for multi-site management in mind

2025-11-08

How to traverse an array and control display style with loop tags (for) in AnQiCMS templates?

During website operation, we often need to display various list data, such as article lists, product lists, image galleries, or navigation menus.AnQiCMS (AnQiCMS) provides a powerful mechanism based on the Go language template engine, where the `for` loop tag is the core tool to meet this requirement.By flexibly using the `for` tag and its auxiliary functions, we can easily traverse array or list data and accurately control the display style of each element according to actual needs.

2025-11-08

How to set up a canonical link (CanonicalUrl) in article content and ensure it displays correctly on the page?

In website operation, we often encounter situations where content is similar or repetitive, which may not only scatter the search engine's crawling budget, but also dilute the page weight, affecting SEO performance.Fortunately, AnQi CMS provides us with the function to set canonical links (Canonical URL), which can effectively help us solve these problems and clearly inform search engines which page is the 'original' or 'preferred' version of the content.The value of understanding the significance of canonical URL in simple terms

2025-11-08

How to configure and display structured data (Json-LD) in AnQiCMS to enhance search engine understanding?

In today's digital world, search engines are the main way users discover website content.In order to help search engines better "understand" the content on our website, structured data plays a crucial role.Json-LD (JSON for Linking Data) is a lightweight, easy-to-implement standardized data format that can clearly describe web content in a structured way, thereby enhancing the visibility and display effects of websites in search results.AnQiCMS as a content management system focusing on SEO optimization

2025-11-08