As an experienced website operation expert, I know that AnQiCMS, with its efficient architecture in Go language and rich features, brings great convenience to small and medium-sized enterprises and content operation teams.System design focuses on practicality and scalability, especially in template customization and content presentation, providing us with great freedom.In daily content operation and maintenance, template debugging is an indispensable part, and correctly obtaining and understanding the original data of variables in the template is crucial for efficient troubleshooting.

Today, we will delve into a very practical skill in the debugging of security CMS templates: how to handle Unix timestamp, especially when we need to usestampToDateThis powerful formatting tag, how to elegantly view its original input timestamp value.

UnderstandingstampToDateThe tag and its function

In the template system of AnQi CMS, we often encounter the need to convert the timestamp stored in the database (usually a 10-digit or 13-digit Unix timestamp) into a user-friendly date and time format.stampToDateLabels come in handy. As described in its documentation, its basic usage is{{stampToDate(时间戳, "格式")}}. For example, you may see such code on the document details page, used to display the publication time of the article:

<span>发布时间:{{stampToDate(archive.CreatedTime, "2006年01月02日 15:04")}}</span>

Here are thearchive.CreatedTimeis a timestamp value,"2006年01月02日 15:04"is a Go-style formatted string, it tells the system how to render the timestamp into the date-time text we expect.stampToDateThe strength lies in simplifying the process of time formatting, but sometimes, we do not want to see the formatted result, but rather want to take a closer look and see what the original timestamp is.

Why is it necessary to check the original timestamp?

During template debugging, checkstampToDateThe original input timestamp value has multiple practical significances:

Firstly,Data validation。When the time displayed on the page is incorrect, or a default value such as “1970-01-01” appears, the original timestamp can help us quickly determine whether the problem lies in the timestamp itself (for example, a value of 0, negative, or illegal format), or instampToDateThe format parameters.

Secondly,Integrated or compared with other systems.When managing multiple sites or performing data migration, we sometimes need to ensure that the timestamps within Safe CMS are consistent with those of external systems.Directly viewing the original value can provide an accurate reference.

Moreover,Understanding data types. AlthoughstampToDateLabels intelligently handle timestamps, but the underlying variables (such asarchive.CreatedTime) may be a Go language.time.TimeA structure, or may also be a pure integer type.Viewing the original value and type helps us to understand the data model more deeply, laying the foundation for subsequent secondary development or complex logic processing.

Reveal debugging tools:dumpFilter

The template system of Anqi CMS provides developers with a rich set of filters, one of which is an extremely practical 'secret weapon' for debugging:dumpFilter. As the name suggests,dumpcan "dredge up" the full structure, type, and current value of a variable, much like an X-ray machine, allowing a clear view of the internal details of the variable.

dumpThe usage of the filter is extremely concise, you just need to add it after any variable you want to check|dumpThat's it, for example:{{ 变量名称|dump }}.

When you are going todumpThe filter is applied tostampToDateWhen the input variable is specified, it outputs the underlying Go language structure of the variable, which includes the original Unix timestamp value we want to see.

Practice Exercise: Print in the templateCreatedTimeThe original timestamp

Let's take a common scenario as an example: You are debugging a detail page of an article, and you need to viewarchive.CreatedTimeThe original timestamp.

通常,You will see the following code to display the formatted time:

{# 假设这是您的文章详情模板文件中的一部分,例如`archive/detail.html` #}
<p>
    文章标题:{{ archive.Title }} <br>
    发布时间(格式化):{{ stampToDate(archive.CreatedTime, "2006-01-02 15:04:05") }}
</p>

To viewarchive.CreatedTimeto find the original timestamp value in the template:archive.CreatedTimebystampToDateThe position before processing, or insert a debugging line nearby, like this:

{# 调试代码:插入在您需要检查的变量旁边 #}
<p>
    文章标题:{{ archive.Title }} <br>
    发布时间(格式化):{{ stampToDate(archive.CreatedTime, "2006-01-02 15:04:05") }} <br>
    原始CreatedTime数据(调试用):{{ archive.CreatedTime|dump }}
</p>

Save the template file and refresh the browser. You will find that in addition to the normally displayed article title and formatted publish time on the page, there will also be an extra line of output: "Original CreatedTime data". This line of output is likely to look like a Go languagetime.TimeStructure, for example:

&time.Time{wall:0x...000000, ext:63799651200, loc:(*time.Location)(0x...)}

Here are theextThe field (or similarly named internal field) usually contains the original Unix timestamp (in seconds or nanoseconds, depending on the system implementation), which you can use for verification or further processing.

Experience tells me that wrapping debug output in HTML comments is a good habit, as it allows you to see the information without affecting the normal layout and style of the page:

{# 将调试信息放入HTML注释,方便查看源代码 #}
<!-- 原始CreatedTime数据(调试用):{{ archive.CreatedTime|dump }} -->
<p>
    文章标题:{{ archive.Title }} <br>
    发布时间(格式化):{{ stampToDate(archive.CreatedTime, "2006-01-02 15:04:05") }}
</p>

Thus, you can simply view the page source code (usually through the "Inspect" or "View Page Source" feature in the browser's right-click menu) to find this debugging information, while the page itself remains neat.

Template debugging practice

MasterdumpFilters are just the first step, in the template debugging of AnQi CMS, we still have some universally applicable practices:

  1. Immediate cleanupEnglish: Debug code should only be added when necessary, and it must be immediately removed from the template once the problem is solved.Retaining too much debug information not only increases the page size, but may also expose sensitive data.
  2. Accurate positioningAvoid adding blindly at uncertain positions|dumpFirst, judge roughly which area or which variable may be problematic based on the page performance, and then insert debugging code specifically.
  3. Combine the browser developer tools:dumpThe filter outputs the server-side rendered HTML content, combined with the browser developer tools (F12), it can be more convenient to view elements, network requests, and JavaScript errors, forming a complete debugging workflow.
  4. Understand the template context: InforLoop orifWhen debugging variables in conditional blocks, make sure you are using `|dump` in the correct context