When building and maintaining websites, we often need to display some dynamic information on the page, such as the current year.This is very useful for copyright statements, annual reports, or any scenario that requires real-time year information.AnQiCMS Thanks to its simple and efficient template system, it makes it easy to obtain and display the current year.
AnQiCMS's template system borrows the syntax of Django template engine, which means it provides intuitive tags and variables to manipulate content.Next, we will discuss how to easily implement this feature in your AnQiCMS template.
The core method to get the current year:{% now %}tags
English translation: A powerful feature is built into AnQiCMSnowLabel, used specifically to obtain and display the current time in templates. To obtain the current year, we just need to use it with the Go language's time formatting rules.
Its basic usage is:{% now "格式字符串" %}.
This 'format string' is crucial. For the year, we usually use"2006"As a format string.You might find it strange why it is '2006' instead of the current year, such as '2023' or '2024'??This is the unique feature of Go language time formatting.YYYY/MMSuch placeholders, but uses a fixed reference time2006-01-02 15:04:05.999999999 -0700 MSTDefine the output format. You need to write the reference value of the time part you want to output into the format string, and Go will infer the output format you want based on your input.
So, when you use{% now "2006" %}it will output the current year based on the year part '2006' in the reference time.
Actual operation: Add the current year to your template
The most common application scenarios include displaying the current year in the copyright statement of the website footer.
For example, if you want to display “© 2024 Your website name All Rights Reserved.” at the footer, you can add the following code to your template file (for examplefooter.htmlorbash.html) as follows:
<footer>
<p>© {% now "2006" %} {% system with name="SiteName" %} 版权所有.</p>
</footer>
In this code block:
{% now "2006" %}It will be automatically replaced with the current year.{% system with name="SiteName" %}It is a tag used in AnQiCMS to get the website name of the website backend settings, and it ensures that the website name is also displayed dynamically.
Thus, the year of your website footer will be automatically updated every year, without manual modification, making it convenient and hassle-free.
More time format options
Except for the year,nowLabel combines the reference time format of the Go language and can output various other time information. Just modify the format string:
- Only display the last two digits of the year:
{% now "06" %}(For example: 24) - Display the full date:
{% now "2006-01-02" %}(For example: 2024-03-15) - Display time (hour:minute:second):
{% now "15:04:05" %}(For example: 10:30:00) - Display the full date and time:
{% now "2006-01-02 15:04:05" %}(例如:2024-03-15 10:30:00) - Show the English abbreviation of the month:
{% now "Jan" %}(例如:Mar) - Show the English abbreviation of the day of the week:
{% now "Mon" %}(例如:Fri)
For example, if you want to display the current date and time on a page, you can use it like this:
<p>当前更新时间:{% now "2006年01月02日 15时04分" %}</p>
For existing timestamps:stampToDatetags
In addition to getting the current time, you may sometimes need to format a timestamp (usually a 10-digit or 13-digit number) obtained from a database or other sources. AnQiCMS also providesstampToDateLabel to handle this situation.
For example, if you have an array namedarchive.CreatedTimeThe variable stores the document creation timestamp, you can format it like this:
<p>发布日期:{{ stampToDate(archive.CreatedTime, "2006-01-02") }}</p>
Here are thestampToDateThe label also follows the Go language time formatting rules, allowing you to flexibly display any timestamp data.
Tips and **Practice
- Familiarize yourself with the Go language time formatting rules:The time formatting in Go language is indeed unique, remember
2006-01-02 15:04:05This reference time, then replace the corresponding numbers and letters according to the part you want, and it will make twice the work with half the effort. - Clear cache:After modifying the template code, if the page does not update immediately, remember to clear the system cache of AnQiCMS backend to ensure that the new code takes effect.
- Template test:Before formal launch, test several time formats in the development environment to ensure they are displayed as expected in English.
Pass{% now %}and{{ stampToDate }}These two concise and powerful tags, AnQiCMS enables you to easily obtain and display various dynamic time information in templates, adding flexibility and automation to your website.
Common Questions (FAQ)
Q1:{% now "2006" %}What does "2006" in the label mean? Why isn't it the current year?
A1: “2006” is not a reference to a specific year, but a time formatting rule in the Go language.Reference value.Go Language uses a fixed date and time format2006年01月02日 15时04分05秒 -0700 MSTas a formatting template. When you use{% now "2006" %}When, the Go language will extract and display the current year based on the format string you provide (here "2006" corresponds to the year part of the reference date). If you want to display the month, you would use{% now "01" %}Because '01' in the reference date represents January.
Q2: Besides the year, can I also use{% now %}tags to get what time information?
A2: Combine the reference time format of the Go language,{% now %}Tags are very flexible. You can get almost any date and time information you want, such as:
- Month number:
{% now "01" %}(如 03) - Date number:
{% now "02" %}(如 15) - Hour (24-hour format):
{% now "15" %}(如 10) - Minutes:
{% now "04" %}(如 30) - Seconds:
{% now "05" %}(如 00) - 月份全称:
{% now "January" %}(如 March) - 星期几全称:
{% now "Monday" %}(如 Friday)
你可以根据需要组合这些参考值来创建自定义的日期时间格式。
Q3: If I need to format the timestamp stored in the database (such as the article posting time), rather than the current time, which tag should I use?
A3: If you need to format the data stored in the databaseTimestamp(usually an integer representing the number of seconds), you should use the tags provided by AnQiCMS{{ stampToDate(时间戳变量, "格式字符串") }}For example, ifarchive.CreatedTimeis a timestamp, you can use{{ stampToDate(archive.CreatedTime, "2006-01-02") }}to format it asYYYY-MM-DDformat.{% now %}tags are used to get and formatthe current system time.