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.