As an experienced website operations expert, I am well aware that AnQiCMS (AnQiCMS) brings great convenience to small and medium-sized enterprises and content operation teams with its efficient Go language architecture and rich features.System design focuses on practicality and scalability, especially in template customization and content presentation, which provides 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 AnQi CMS templates: how to handle timestamps (Unix timestamp), especially when we need to usestampToDateHow to elegantly view the original input timestamp value of this powerful formatting tag.

UnderstandingstampToDateThe role of tags.

In AnQi CMS template system, 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. At this time,stampToDateThe label comes 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>

Herearchive.CreatedTimeIt is a timestamp value, while"2006年01月02日 15:04"is a Go language style formatted string, which tells the system how to render the timestamp into the date and time text we expect.stampToDateThe strength lies in its simplification of the time formatting process, but sometimes we don't want to see the formatted result, but rather want to explore and see what the original timestamp is.

Why do you need to view the original timestamp?

View during template debugging, checkstampToDateThe original input timestamp value has many practical meanings:

first, Data ValidationWhen the time displayed on the page is incorrect, or when the default value such as "1970-01-01" appears, the original timestamp can help us quickly determine whether the problem lies in the timestamp itself (such as a value of 0, negative, or an illegal format), or instampToDateThe formatting parameters on.

secondly,Integrated or compared with other systems.. When managing multiple sites or performing data migration, we sometimes need to ensure that the timestamp within the Anqin CMS is consistent with the timestamp of the external system.Directly viewing the original value can provide accurate reference.

Moreover,Understanding data types.. AlthoughstampToDateThe tag intelligently handles timestamps, but the underlying variables (such as)archive.CreatedTime) may be a Go language'stime.TimeA structure may also be a pure integer type. Viewing its original value and type helps us understand the data model more deeply, laying the groundwork for subsequent secondary development or complex logic processing.

Unveiling the debugging tool:dumpFilter

The template system of Anqi CMS provides developers with a rich set of filters, one of which is an extremely useful 'secret weapon' for debugging.dumpA filter. As the name suggests,dumpCan 'dump' the complete structure, type, and current value of a variable, as if it were an X-ray machine, revealing the inner workings 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 original input variable is processed, it outputs the underlying Go language structure, which includes the original Unix timestamp value we want to see.

Practice exercise: Print in the templateCreatedTimeoriginal timestamp

Let's take a common scenario as an example: You are debugging an article detail page and need to viewarchive.CreatedTimeoriginal timestamp.

Generally, 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.CreatedTimeThe original timestamp value, you just need to find it in the templatearchive.CreatedTimeisstampToDateHandle the position before, or insert a line of debug code 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 after that, you will find that the page not only displays the normal article title and formatted publishing time, but also an additional line of output for the "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...)}

HereextThe field (or similar-named internal field) typically contains the original Unix timestamp (in seconds or nanoseconds, depending on the system implementation), and you can use this value for verification or further processing.

Experience has taught me that it is a good habit to wrap debug output in HTML comments, so that you can 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>

This way, you just need to view the page source code (usually through the "Inspect" or "View Page Source" feature in the browser context menu), and you can find this debugging information, while the page itself still looks neat.

Template debugging practices

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

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