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

As an experienced CMS website operation personnel, I am well aware of the importance of quickly understanding data structures in the process of template development and content optimization. Although the template engine of AnQiCMS is similar in syntax to the Django template engine and provides a rich set of tags and filters, upon a detailed examination of the provided documentation, it can be found that there is no explicit list 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 understand the data intuitively.

Understand the debugging basics of AnQiCMS template engine

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

Directly output simple variables.

For simple types such as strings, numbers, and booleans, the most direct way to debug is to output the variable name within double curly braces. For example, if you want to view a variablemyVariableThe value can be directly inserted into the template{{ myVariable }}. When the page is rendered, the current value of the variable will be displayed on the page to help you judge whether it meets your expectations.

Traverse complex data structures to explore their content

Directly output when the variable is an array, slice (list), or dictionary (map) etc., complex data structure{{ complexVariable }}可能只会显示其类型信息或无法提供详细内容。此时,我们可以利用AnQiCMS模板引擎的English{% for %}Loop through the tags to iterate over their elements, thereby revealing their internal structure.

For example,archivesIt 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 convenient as one buttondumpIt comes with brevity, but it allows you to precisely control which information is output and can target in-depth into nested structures. For specific objects such as documents, categories, pages, etc., their attribute names (such asId,Title,Link,Descriptionauto in the document are explicitly stated and can be quoted directly.

Use the `archiveParams` tag to view the structure of custom fields.

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 labeling, it will treat all 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 using:{% archiveParams params with sorted=false %},paramsIt will be an unordered map object, you can access its properties through the dot.to access its properties, for example:{{params.yuedu.Name}}This provides convenience for directly obtaining detailed information when the custom field name is known.

Combine conditional judgment for more fine-grained debugging.

During debugging, you may only want to output variable information under specific conditions, or check if the 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 nonedumpFilter, but AnQiCMS providestag-filters.mda variety of filters listed in the middle, which can be used to format or truncate output for easier readability. For example,truncatecharsit can truncate long strings,joinThe array elements can be concatenated into a string with a delimiter.

For example, concatenate a variable array into a string and output it:

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

Debug summary and **practice**

Although AnQiCMS's template system does not directly providedumpfilterers, but by combining their usage with{{ 变量 }},{% for %},{% if %}and specific tags (such asarchiveParams)and some basic filters, we can effectively explore the structure and content of template variables. When debugging templates, please follow the following **practices:**

  • Localized debugging:Insert debugging code only in the template areas that require debugging, and 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 debug information to facilitate control of its display between development and production environments.
  • Check the page source code:The browser developer tools are your good helper, by checking the page source code you can confirm whether the template variables are rendered correctly into the HTML.
  • Use the log:For deeper backend data issues, it is often more efficient to investigate with the AnQiCMS backend logs, but for data structures at the template level, it is the most intuitive way to output them directly in the template.

By these methods, you can debug the variable structure in AnQiCMS templates with ease, ensuring the accurate presentation of content.


Common Questions (FAQ)

What types of variables does AnQiCMS template engine support?AnQiCMS template engine supports various variable types, including strings, numbers, boolean values, arrays (slices), mappings (dictionaries), and struct objects in Go language..Access, for example{{ item.Title }}.

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

How to avoid too much output information during debugging to cause page chaos? To avoid page chaos, it is recommended to only output variables at specific locations where debugging is needed, and try to use{% if %}Label condition judgment, ensure that debugging information is displayed only under specific conditions. At the same time, it can be used totruncatecharsTruncate overly long output content of filters to maintain readability of the page. After debugging is completed, be sure to delete or comment out the debug code in time.