In website operation, we often need to dynamically display the current year or time, such as displaying the latest copyright year at the bottom of the website, or marking the generation time of the page accurately in articles.AnqiCMS provides flexible and powerful template functions, making these operations very simple.Next, we will discuss how to implement this requirement in the AnqiCMS template.
To dynamically display the current year or time: usingnowTag
AnqiCMS provides a very convenient built-in tagnowUsed to dynamically obtain and display the current date or time during template rendering. The key to this tag lies in its time formatting string, which follows the unique date and time format representation of the Go language, rather than the ones commonly used in PHP or JavaScript.Y-m-d H:i:sThis format.
In Go language, formatting strings are actually a specific reference time:2006年01月02日 15时04分05秒. You need to match the "layout" of the time element you want to display with the corresponding part of this reference time.
Here are some commonly usednowSign examples and their output:
Only show the current year:If you want to display the copyright year at the footer of the website, you can use it directly:
© {% now "2006" %} 版权所有。It will be displayed on the page as:
© 2023 版权所有。(Assuming the current year is 2023)Display the full date:If you need to display the current full date on the page, for example
年-月-日Format:今天是:{% now "2006-01-02" %}The page display may be:
今天是:2023-10-26Display the full date and time:If you need to include the time to the second:
页面加载于:{% now "2006-01-02 15:04:05" %}The page display may be:
页面加载于:2023-10-26 10:30:45Custom format display:You can combine them as needed, for example, displaying "Year/Month/Day Week Hour:Minute":
更新时间:{% now "2006/01/02 Mon 15:04" %}The page display may be:
更新时间:2023/10/26 Thu 10:30
Furthermore,nowThe tag also has an optional parameterfakeUsed to force a fixed reference time during development and debugging, which is convenient for style debugging or data display, for example:
测试时间:{% now "2006-01-02 15:04:05" fake %}
This will display the reference time of the Go language itself, not the current system time.
Format an existing timestamp: usestampToDatefunction
In addition to displaying the current time, we often need to display information such as the article publication time and update time.These times are usually stored in databases in the form of Unix timestamps.AnqiCMS providedstampToDateA function to help us format these timestamps into readable dates or times.
This function accepts two parameters: a 10-digit timestamp (such as fromarchive.CreatedTimeoritem.UpdatedTimeAccess), and a string formatted with the samenowtag in Go language formatting.
The following are somestampToDatecommon examples of functions:
Format the creation date of the article:On the article detail page or list page, you can display the article's publish date like this:
发布于:{{ stampToDate(archive.CreatedTime, "2006年01月02日") }}The page display may be:
发布于:2023年09月15日Format the article update date and time:If you need to display the exact update time:
最后更新:{{ stampToDate(item.UpdatedTime, "2006-01-02 15:04") }}The page display may be:
最后更新:2023-10-25 18:00Only display time:You can only extract the time part to display:
具体时间:{{ stampToDate(archive.CreatedTime, "15:04") }}The page display may be:
具体时间:10:30
archive.CreatedTimeanditem.UpdatedTimeIs a common variable in AnqiCMS templates, representing the timestamp of the creation time and update time of the current document (or an entry in the list).
Application scenarios in practice
Website footer copyright information:One of the most common uses, ensuring the copyright year is automatically updated:
<footer> <p>© {% now "2006" %} All Rights Reserved. {{ system.SiteName }}</p> </footer>Article publishing/update metadata:Display key time information below the article title or in the sidebar:
<div class="article-meta"> <span>发布日期:{{ stampToDate(archive.CreatedTime, "2006年01月02日") }}</span> <span>更新日期:{{ stampToDate(archive.UpdatedTime, "2006-01-02 15:04") }}</span> </div>Dynamic page prompt:For example, displaying the real-time update of data on a list page or tool page:
<p>数据最后更新于:{% now "2006-01-02 15:04" %}</p>
BynowTags andstampToDateFunction, AnqiCMS templates provide powerful flexibility in handling date and time display, whether it is to get the current time or format existing timestamps, it can easily cope with it, keeping your website content timely and professional.
Frequently Asked Questions (FAQ)
1. Why is the date formatting string in the AnqiCMS template2006-01-02 15:04:05in this form instead of the commonYYYY-MM-DD?
This is because the AnqiCMS template engine is based on Go language, and the Go language uses a fixed reference time for date and time formatting function2006-01-02 15:04:05.999999999 -0700 MST(That is, at 3:04:05 PM on January 2, 2006) to define the layout.You need to match the date and time elements you want to display (such as year, month, day, hour, minute, second) with the corresponding numbers in this reference time, rather than using placeholders like other languages.For example, to display the year, you write2006instead ofYYYY.
2. Can I use this syntax?{{ ... }}is used{% now "2006" %}Can I use this syntax?
No. In AnqiCMS templates,{{ ... }}used to output variable values, while{% ... %}used to execute control logic or call tags.nowIt is a tag, so it must be used{% now ... %}to call the syntax. If you want to assignnowthe result of the tag to a variable and output it{% set current_year = now "2006" %}Then{{ current_year }}output it directly but not{% now "2006" %}usually more concise.
3. How can I display the current month, day of the week, or other specific time elements, in addition to showing the year and complete date and time?
You just need to refer to the Go language's formatting layout, find the corresponding time element, and put it into your formatting string. For example: