During the AnQiCMS template development process, we may encounter situations where variables are not displayed as expected, or we need to delve into the specific structure and value of a variable in order to better perform content layout and logical control.AnQiCMS uses a syntax similar to Django template engine, simple yet powerful, but even so, mastering debugging skills can also make our development process smoother and more efficient.
Today, let's talk about how to accurately view the detailed structure and values of variables in AnQiCMS templates, enabling you to gain deep insights into the data in the templates.
Reveal the mysteries of variables:dumpFilter
When we are puzzled by a variable, AnQiCMS provides a very practicaldumpFilter, it can clearly print out the complete structure, data type, and current value of the variable.This is like taking an X-ray of a variable, allowing all internal details to be seen in full.
UsedumpFilter is very simple, just add it to the variable you want to debug|dumpThat's it, for example:
{{ archive|dump }}
This simple code will output when you use it on the document detail pagearchiveThis document object includes all properties, including its ID, title, content, classification, and even its internal Go language structure information. With this information, you can quickly understandarchiveWhat fields are available in the object and what are the current values of these fields.
It is especially important to note that if your variable may contain HTML content, such as articles,ContentField, to avoid the browser from parsing it as an actual HTML tag, which may cause layout issues on the page or even worse, introduce security issues (such as XSS), it is recommended to use it in conjunction with other measures|safeFilter.|safeThe filter informs the template engine that the content you output is safe and does not require HTML escaping. Therefore, when you debug rich text content, you can use it like this:
{{ archive.Content|dump|safe }}
This can display the complete structure of the content and ensure that it is presented in its original HTML form, making it convenient for you to check it.
In-depth Exploration: Precise positioning with other tags
dumpThe filter is powerful, but by combining with other built-in tags of AnQiCMS template, we can be more flexible in variable debugging.
1. Directly print: Quickly check variable values
The most basic way is to use double curly braces directly{{ variable }}Print the final value of the variable. This is the quickest way to confirm if a field is output correctly. For example, to check if the article title is correctly retrieved:
<h1>{{ archive.Title }}</h1>
2. Condition judgment: Verify the existence of a variable or a specific state
In actual development, we often need to judge whether a variable exists or meets a specific condition. At this time,ifThe logic judgment label comes into play. You can use it firstifto judge if a variable exists, thendumpprint to avoid errors caused by the non-existent variable:
{% if archive %}
<p>文档对象存在:</p>
{{ archive|dump|safe }}
{% else %}
<p>当前页面没有文档对象</p>
{% endif %}
You can also combinedumpFilter, output debug information only when specific conditions are met, for example, only print the full information when the document ID is 10:
{% if archive.Id == 10 %}
<p>正在调试ID为10的文档:</p>
{{ archive|dump|safe }}
{% endif %}
3. Looping through: Exploring lists and nested structures
When you need to process list data, likearchiveListThe returned document list,categoryListThe returned category list, orarchiveParamsThe returned custom parameter list when,forThe loop traversal tag is indispensable. CombineddumpThe filter is used inside the loop, which can check the structure and value of each element in the list one by one:
{% archiveList recentPosts with limit="3" %}
{% for post in recentPosts %}
<p>这是第 {{ forloop.Counter }} 篇文档的详细信息:</p>
{{ post|dump|safe }}
<hr>
{% empty %}
<p>没有找到相关文档。</p>
{% endfor %}
{% endarchiveList %}
For the custom parameters of the document, they usually exist in the form of an array object. You can traverse and print the names and values of each custom parameter in a similar manner:
{% archiveParams customFields %}
{% for field in customFields %}
<p>自定义字段:</p>
{{ field|dump }}
<hr>
{% endfor %}
{% endarchiveParams %}
PassdumpFilter, you can clearly see eachfieldthe object containsName(field name) andValue(field value) and other properties.
4. Define temporary variables: Simplify the debugging of complex expressions
Sometimes the template may contain complex variable paths or expressions. UsesetorwithLabels can define temporary variables, break down complex expressions into smaller parts, and debug them one by one, which can greatly improve debugging efficiency.
{% set firstCategory = archive.Category.NavList|first %}
{% if firstCategory %}
<p>第一个子分类信息:</p>
{{ firstCategory|dump }}
{% endif %}
Practical tips to enhance the debugging experience
- Start with a small range:Avoid placing directly at the top of the page.
{{ website|dump }}This may output a large amount of information, making you dizzy. Start from the local variables you suspect and gradually expand the scope. - Positional placement:to
dumpThe filter is placed near the position where you expect the variable to appear, making it easier to match page output and code logic. - Use background template editing:AnQiCMS Back-end built-in template editing function.During development, you can directly modify the template file in the background and save it. The front-end will refresh automatically, saving the trouble of frequent file uploads and greatly accelerating the debugging and iteration speed.
- Pay attention to variable naming conventions:AnQiCMS's variable naming usually follows camel case naming convention (such as
archive.Id,category.Title). Familiarity with these conventions (refer to设计约定document) can help you guess and find the correct variable name more quickly.
Master these debugging techniques, and you will be able to develop and maintain AnQiCMS templates more skillfully, quickly locate problems, and ensure the perfect presentation of website content.
Common Questions and Answers (FAQ)
1. Why did I use{{ variable|dump }}What did not display?
This usually means that the variable does not exist in the current context or its value isnil(empty). You can try to add one beforedump.{% if variable %}Determine if a variable exists. Additionally, make sure you are on the correct page type (such as, debugging on the article detail page)archiveObject ()下进行调试,否则相关的变量可能确实不存在。
2. I modified the template code and saved it, but there was no change on the front-end page, what's going on?
This is likely because the system cache of AnQiCMS is still effective. You need to log in to the backend, find 'Update Cache' in the left menu