During AnQiCMS template development, we may encounter variables that do not display as expected, or we may need to gain a deeper understanding of the specific structure and value of a variable in order to better carry out content layout and logical control.AnQiCMS uses a syntax similar to the Django template engine, simple yet powerful, but even so, mastering debugging techniques can make our development process smoother and more efficient.
Today, let's talk about how to accurately view the detailed structure and value of variables in the AnQiCMS template, allowing you to gain a deep insight into the data in the template.
Reveal the mystery of variables:dumpFilter
When we are puzzled by a variable, AnQiCMS provides a very practicaldumpA filter that can clearly print the complete structure, data type, and current value of a variable.This is like taking an X-ray of a variable, showing all the internal details at a glance.
UsedumpThe filter is very simple, just add it after 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 its properties, including its ID, title, content, classification, and even its internal Go language structure information. Through this information, you can quickly understandarchiveWhat fields does the object contain, and what are the current values of these fields.
It should be noted that if your variable may contain HTML content, such as the content of an article,ContentField, to avoid the browser from parsing it as actual HTML tags, which may cause layout chaos on the page or, worse, may introduce security issues (such as XSS), it is recommended to use in conjunction with|safefilter.|safeThe filter informs the template engine that the content you output is safe and does not require HTML escaping. Therefore, when debugging rich text content, you can use it like this:
{{ archive.Content|dump|safe }}
This can help you see the complete structure of the content and ensure that it is displayed in its original HTML form for easy checking.
Further Exploration: Use other tags for precise positioning.
dumpThe filter is indeed powerful, but combined with the other built-in tags of the AnQiCMS template, we can perform variable debugging more flexibly.
1. Direct print: Quickly check variable value
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 whether a field is output correctly. For example, to check if the article title is correctly retrieved:
<h1>{{ archive.Title }}</h1>
2. Conditional judgment: Verify the existence or specific status of a variable
In actual development, we often need to judge whether a variable exists or meets specific conditions. At this time,ifLogical judgment labels come into play. You can first useifCheck if the variable exists, then usedumpPrint to avoid errors caused by the variable not existing:
{% if archive %}
<p>文档对象存在:</p>
{{ archive|dump|safe }}
{% else %}
<p>当前页面没有文档对象</p>
{% endif %}
You can also combinedumpA filter that outputs debug information only when certain conditions are met, for example, only when the document ID is 10, it will print the full information:
{% if archive.Id == 10 %}
<p>正在调试ID为10的文档:</p>
{{ archive|dump|safe }}
{% endif %}
3. Loop traversal: Explore lists and nested structures
When you need to process list data, such asarchiveListThe returned document list,categoryListThe returned category list, orarchiveParamsThe returned custom parameter list when,forThe loop iteration tag is indispensable. CombineddumpThe filter is used within the loop to check the structure and value of each element in the list:
{% 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 name and value of each custom parameter in a similar manner:
{% archiveParams customFields %}
{% for field in customFields %}
<p>自定义字段:</p>
{{ field|dump }}
<hr>
{% endfor %}
{% endarchiveParams %}
BydumpFilter, you can clearly see eachfieldobject containsName(field name) andValue(field value) and other properties.
4. Define a temporary variable: Simplify the debugging of complex expressions.
Sometimes the template may contain complex variable paths or expressions. UsesetorwithTags 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 debugging experience.
- Start from a small range:Avoid placing directly at the top of the page
{{ website|dump }}This may output a large amount of information, confusing you. Start from the local variables you suspect and gradually expand the scope. - Positional placement:to
dumpFilter it at the location where you expect the variable to appear, so it is easier to compare the page output and code logic. - Use background template editing:AnQiCMS backend has built-in template editing functionality.During development, you can directly modify and save the template file in the background, refresh the front-end to see the effect, saving the trouble of frequent file uploads, and greatly accelerating the debugging and iteration speed.
- Pay attention to variable naming conventions:AnQiCMS variable naming usually follows camel case naming (such as
archive.Id,category.Title)。Familiarizing with these conventions (refer to设计约定the 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.
Frequently Asked Questions (FAQ)
1. Why did I use the tag in the template{{ variable|dump }}Nothing was displayed?
This usually means that the variable does not exist in the current context or its value isnil(empty). You can try adding one beforedump.{% if variable %}Check to verify if the variable exists. In addition, make sure you are on the correct page type (for example, debugging on the article detail page.archiveDebugging under the object, otherwise the relevant variables may indeed not exist.
2. After I modified the template code and saved it, there was no change on the front-end page, what's the matter?
This is likely because the AnQiCMS system cache is still active. You need to log in to the admin panel, find "Update Cache" in the left menu.