Ride Time: In Anqi CMSarchiveListLoop instampToDateThe Art of Date Formatting
In the world of content management, the publication time, update time, and other date information of website content are not only key elements providing critical context to readers, but also important manifestations of the website's professionalism and user experience.For AnQiCMS such an enterprise-level content management system based on Go language, which focuses on efficiency and flexibility, how to elegantly present these original timestamps stored in the database to the users is a common and core requirement in template development.archiveListin the loop,stampToDateHow tags ensure that each document's creation time is accurately and beautifully formatted.
UnderstandingarchiveListWith the original timestamp
Firstly, we know that the Aanqi CMS goes througharchiveListTags, which can easily fetch a series of document (articles, products, etc.) data from the backend. This tag function is powerful, and it can filter and sort content based on category ID, module ID, recommendation attributes, or even search keywords, ultimately presenting multipleitemThe list of objects is presented in the template for us to iterate over.
in theseitemobjects,item.CreatedTimeanditem.UpdatedTimeSuch a field usually stores a Unix timestamp—an integer representing the number of seconds since 00:00:00 UTC (Coordinated Universal Time) on January 1, 1970.This storage method is very friendly to database and program processing, but directly displaying this string of numbers on the website front-end is obviously not in line with users' reading habits and does not appear professional enough.This brings up the need for us to 'translate' timestamps.
stampToDate: The translator of timestamps.
To solve the timestamp display problem, Anqi CMS provides a very practical template tag -stampToDate。Its core function is to convert the original timestamp into the date or time string we hope for. The usage of this tag is intuitive and flexible:{{stampToDate(时间戳, "格式")}}.
Here, the most critical part is the second parameter - 'Format'. Unlike many programming languages, the Go language uses a unique 'reference time' mechanism when formatting time, rather than relying on abstract letter codes (such asY-m-d)。English CMS的stampToDate标签沿用了这一特性。其格式化字符串需要基于Go语言的固定参照时间:2006年01月02日 15时04分05秒This means, if you want to display the year, write “2006”; if you want to display the month, write “01”; if you want to display the hour, write “15”, and so on.
For example, if we need to format the timestamp as “2023-10-27”, then the format string is"2006-01-02"If you want to display the date and time accurate to the minute, such as"2006年01月02日 15:04"This design may look a bit special at first glance, but once mastered, you can appreciate its precise and powerful control ability.
stampToDateInarchiveListHow to ensure proper formatting in a loop
Now, let's takearchiveListandstampToDateCombine and see how it ensures that each document's creation time is correctly formatted in practice.
When we use templates in the templatearchiveListWhen getting the list of documents and looping through them, for example:
{% archiveList archives with type="page" limit="10" %}
{% for item in archives %}
<article>
<h2><a href="{{item.Link}}">{{item.Title}}</a></h2>
<p>
发布于: {{stampToDate(item.CreatedTime, "2006年01月02日 15:04")}}
<span class="views">阅读量: {{item.Views}}</span>
</p>
<div class="description">{{item.Description}}</div>
</article>
{% empty %}
<p>暂时没有可供显示的文档。</p>
{% endfor %}
{% endarchiveList %}
The key point here isforThe iteration of the loop. When the loop processesarchivesin the listitemwhenstampToDateThe function is called independently and receives the currentitemofCreatedTimeas its first parameter.
Specifically, eachfor item in archiveswhen executed:
- The system will extract from the current iteration.
itemthe object.CreatedTimeThe original timestamp value of the field. - This original timestamp value will be used as
stampToDatethe first parameter of the function. stampToDatethe function based on the second parameter (e.g."2006年01月02日 15:04")defined format, to convert the current timestamp.- The converted date and time string will replace the part in the template.
{{stampToDate(...)}}and displayed on the page.
Since this process is for every single item in the loop,itemit is executed independently, so regardless of how many documents are in the list,stampToDateit can ensure each document'sCreatedTimeHave been correctly and consistently formatted. This individual processing mechanism ensures a high degree of correspondence and accuracy in time and formatting, greatly simplifying the complexity of front-end date display.
Practice significance and operation value
For website operation, an accurate and unified date and time format has multiple values:
- Enhance user experience:The clear and understandable publication time allows readers to see at a glance, enhancing the timeliness and readability of the content.
- Optimize content management:Whether it is an article, product, or event, standardized date display helps users understand the lifecycle of content and aids in decision-making.
- Enhance brand professionalism:The details determine success or failure, a consistent date format reflects the meticulous and professional operation of the website.
- Help SEO:Although it is not a direct SEO ranking factor, a good user experience and content presentation help reduce the bounce rate, increase the time spent on the site, and indirectly improve the SEO effect.
PassarchiveListWithstampToDateThe perfect combination, Anqi CMS provides powerful tools for website operators, making timestamp formatting simple, flexible and efficient, truly achieving the service of technology for content, enhancing the overall operation efficiency and user satisfaction of the website.
Common Questions and Answers (FAQ)
1. Ifitem.CreatedTimeEmpty or invalid timestamp,stampToDatehow should it be handled?Anqi CMS'sstampToDateThe tag is quite robust. If the input is,时间戳The parameter is empty (nil)or is not a valid 10-digit timestamp, it usually returns an empty string, or returns a default, non-error display result according to the internal implementation strategy to avoid page crashes. In actual development, if you are concerned about the irregularity of the data, you can add it before the call{% if item.CreatedTime %}Such judgment ensures that formatting is only performed for valid timestamps.
2. BesidesstampToDateDoes the Anqi CMS have other tags or filters for formatting dates?The template engine (similar to Django syntax) of Anqi CMS also provides some built-in filters, such asdateFilters. However, it is important to note that the documentation explicitly states,dateThe filter requires its input to be a type object in Go language,time.Timenot a raw timestamp. Therefore, if your data source is a timestamp (likeitem.CreatedTime)stampToDateThis is a more direct and recommended solution. If your Go backend code has already converted the timestamp intotime.Timeand passed it to the frontend, thendatethe filter can also be used.
3. How to use different date formats on different pages while maintaining flexibility?
stampToDateThe second parameter is a string, which means you can flexibly pass different format strings according to different page requirements. For example, on the article list page, it may only display"2006-01-02"(Year-Month-Day), and it can be displayed on the article detail page"2006年01月02日 15:04:05"(Precise to seconds).You can even store format strings in a variable to achieve more advanced dynamic formatting, thereby meeting the diverse display needs of date and time for different areas of the website.