How to use the `dump` filter to debug variable structure in AnQi CMS template?

Calendar 👁️ 60

As an experienced CMS website operator, I am well aware of the importance of being able to quickly discern data structures during template development and content optimization. Although the AnQiCMS template engine is similar in syntax to the Django template engine and offers a wealth of tags and filters, upon close examination of the provided documentation, it can be found that there is no explicit listing of nameddumpThe filter. This may confuse developers accustomed to other template languages (such as Twig'sdump, Laravel Blade'sddetc).

Even though the official documentation of AnQiCMS does not provide directlydumpSuch a debugging filter, we can still effectively explore and debug the variable structure through its flexible template syntax and variable output mechanism.The core of template debugging lies in outputting the content or structure of variables to the page so that we can intuitively understand the data.

Understand the debugging basics of AnQiCMS template engine

AnQiCMS template engine supports using double curly braces{{ 变量 }}Directly output the value of the variable, as well as through{% 标签 %}and{% 过滤器 %}Perform more complex data processing and display. For debugging variable structures, we can comprehensively use these basic functions to "simulate"dumpThe effect of the filter.

Directly output simple variables.

For string, number, boolean and other simple types of variables, the most direct way to debug is to output their names within double curly braces. For example, if you want to view a variablemyVariableThe value can be directly inserted into the template{{ myVariable }}So, when the page is rendered, the current value of the variable will be displayed on the page to help you judge whether it meets the expected results.

Traverse complex data structures to explore their content

When the variable is an array, slice (list) or dictionary (map) and other complex data structures, output directly{{ complexVariable }}It may only display its type information or cannot provide detailed content. At this time, we can use the AnQiCMS template engine's{% for %}Loop the tags to iterate over their elements and reveal their internal structure.

For example, ifarchivesIt is an array of document lists, and you can iterate over and output the key properties of each document like this:

{% for item in archives %}
    <p>文档ID: {{ item.Id }}</p>
    <p>文档标题: {{ item.Title }}</p>
    <p>文档链接: {{ item.Link }}</p>
    <p>文档描述: {{ item.Description }}</p>
    {# 更多属性可以继续输出 #}
{% endfor %}

This method is not as good as one-clickdumpIt is concise, but it allows you to precisely control which information is output and can targetively delve into nested structures. For specific objects such as documents, categories, pages, and so on, their attribute names (such asId,Title,Link,DescriptionAll are clearly stated in the document and can be directly cited.

View the structure of custom fields using the `archiveParams` tag.

In AnQiCMS, document, category and other content models can define custom fields.archiveParamsTags are an effective tool for exploring the structure of these custom fields. When you use them in templates,{% archiveParams params %}When a tag is used, it will take all the custom parameters of the current document as an array objectparamsReturn. You can iterate through this array to view the names and values of each custom parameter, which is equivalent to performing a local "dump" of custom fields.

For example, to view all custom parameters of the current document:

{% archiveParams params %}
    {% for item in params %}
        <p>参数名称: {{ item.Name }}</p>
        <p>参数值: {{ item.Value }}</p>
    {% endfor %}
{% endarchiveParams %}

The document also mentions that if you use{% archiveParams params with sorted=false %},paramsIt will be an unordered map object, you can access its properties by dot.such as{{params.yuedu.Name}}This provides convenience in directly obtaining detailed information when the custom field name is known.

Fine-tune debugging by combining condition judgments.

During debugging, you may want to output variable information under specific conditions, or check if a variable exists.{% if %}Labels are very useful in this case.

You can check if a variable exists or is not empty:

{% if myVariable %}
    <p>myVariable 存在且不为空: {{ myVariable }}</p>
{% else %}
    <p>myVariable 不存在或为空。</p>
{% endif %}

Or check if a property exists in a complex object:

{% for item in archives %}
    {% if item.Thumb %}
        <p>文档ID {{ item.Id }} 有缩略图: <img src="{{ item.Thumb }}" alt=""></p>
    {% endif %}
{% endfor %}

Use other filters to format the output

Even though there is notdumpBut AnQiCMS provides filters,tag-filters.mdwhich are listed among the many filters, used to format or truncate output to make it more readable. For example,truncatecharsit can truncate long strings,joinConnect array elements into a string using a delimiter.

For example, output a string by concatenating an array variable:

{% set my_list = ["apple", "banana", "cherry"] %}
<p>我的列表: {{ my_list|join:", " }}</p>

Debug summary with **practice

Although the AnQiCMS template system does not provide directlydumpa filter, but by combining it with{{ 变量 }},{% for %},{% if %}and specific tags likearchiveParams) and some basic filters, we can effectively explore the structure and content of template variables. When performing template debugging, please follow the following **practices:**

  • Localized debugging:Insert debugging code only in the template area that requires debugging, delete it promptly after completion to avoid unnecessary code residue affecting page performance and user experience.
  • Comment out debugging code:Use{# 调试代码 #}or{% comment %} 调试代码 {% endcomment %}Wrap debugging information to facilitate control of its display when switching between development and production environments.
  • Check the page source code:Browser developer tools are your helpful assistant, by viewing the page source code you can confirm whether the template variables are rendered correctly into the HTML.
  • Using logs:For deeper backend data issues, combining AnQiCMS backend logs for troubleshooting is often more efficient, but for template-level data structures, directly outputting in the template is the most intuitive way.

By these methods, you can operate like an experienced operator, skillfully debug the variable structure in AnQiCMS templates, ensuring the precise presentation of content.


Frequently Asked Questions (FAQ)

What types of variables does AnQiCMS template engine support?The AnQiCMS template engine supports various variable types including strings, numbers, booleans, arrays (slices), mappings (dictionaries), and struct objects in Go language.The field of the structure object can be accessed by dot.Visit, for example{{ item.Title }}.

Why does AnQiCMS not providedumpor similarddThe filter? AnQiCMS focuses on providing efficient and concise content management solutions, its template engine is designed to provide core functions, while leaving complex debugging tools to the more general Go language backend debugging.In addition, by combining the existing label and variable output methods, developers can already meet most template debugging needs.

How to avoid outputting too much information during debugging to prevent page chaos? To prevent page chaos, it is recommended to only output variables at specific debugging locations and try to use{% if %}Label for conditional judgment to ensure that debug information is displayed only under specific conditions. At the same time, it can be used totruncatecharsTruncate the output content that is too long to maintain readability on the page. Be sure to delete or comment out the debug code after debugging is completed.

Related articles

How to implement user group management and VIP paid content settings in AnQi CMS?

How to implement user group management and VIP paid content settings in AnQi CMS?As an experienced website operator, I am well aware that fine-grained user management and flexible content monetization capabilities are crucial for the success of a website.AnQiCMS (AnQiCMS) provides strong support in this aspect, allowing website operators to build differentiated user services and effectively realize the monetization of content value through its user group management and VIP system.

2025-11-06

How to configure and display the `hreflang` tag in the Anqi CMS template?

As a website operator who deeply understands the operation of Anqi CMS, I am very clear about the importance of multilingual websites in today's global market and how to enhance the international visibility of the website through refined SEO strategies, such as the `hreflang` tag.The flexibility of AnQi CMS in multilingual support and template customization provides a solid foundation for us to achieve these goals.

2025-11-06

How to use `join` and `split` filters to handle strings and arrays in AnQi CMS template?

As an experienced CMS website operation person in Anqi, I am well aware of the importance of flexible data handling in content management and display.The AnQi CMS template engine provides a rich set of filters to help us achieve this goal, among which the `join` and `split` filters are particularly powerful in handling string and array data, allowing our content display to be more dynamic and accurate.

2025-11-06

How to use `truncatechars` and `truncatewords` in Anqi CMS template?

As a website operator who is well-versed in the operation of AnQiCMS, I deeply understand the subtleties of content presentation.How to elegantly handle the display of text content when building an engaging and user-friendly website experience.AnQi CMS, with its flexible class Django template engine, provides us with powerful content control capabilities, among which the text truncation feature is a commonly used tool for optimizing page layout and improving reading experience.

2025-11-06

What are the specific steps for one-click deployment of AnQiCMS on aaPanel/Baota Panel using Docker?

As a website operator who deeply understands the operation of AnQiCMS, I am fully aware of the importance of an efficient and stable content management system for the development of a company.AnQiCMS with its high-performance architecture based on the Go language, flexible content model, and SEO-friendly design, provides an excellent solution for small and medium-sized enterprises and content operation teams.It is committed to making website deployment simple, secure operation, elegant interface, and extremely fast execution speed, effectively avoiding many potential security issues.

2025-11-06

How to determine if my Baota panel version meets the Docker deployment requirements of AnQiCMS?

All colleagues, hello.As a senior website operator, I know that when deploying and managing websites, the compatibility of the infrastructure is the key to ensuring stable operation of the system.Today, let's discuss a common question: How to determine if your Baota panel version meets the Docker deployment requirements of AnQiCMS (AnQiCMS).

2025-11-06

How to configure reverse proxy to bind my domain after deploying AnQiCMS with Docker using aaPanel/Baota panel?

As a senior security CMS operator, I fully understand your needs for website deployment and domain binding.The Anqi CMS, with its high efficiency and flexibility, can greatly simplify operation and maintenance work through Docker deployment.Now, let's delve into how to configure reverse proxy to bind your domain after deploying AnQi CMS on Docker using Baota panel (aaPanel), ensuring that your website goes online smoothly and runs stably.

2025-11-06

How to select and configure MySQL database when installing AnQiCMS in Docker environment?

As a website operator who has been deeply involved in AnQiCMS (AnQiCMS) for many years, I know the importance of a stable and efficient backend system for content operation.Deploying AnQiCMS in a Docker environment not only provides excellent isolation and portability, but also the rational selection and configuration of the database is the foundation for its efficient operation.Below, I will combine the documents you provided to elaborate on how to select and configure the MySQL database in a Docker environment to support AnQiCMS.

2025-11-06