In website operations, it is crucial to maintain the timeliness and accuracy of content, especially information like copyright years, which, if updated manually, is not only cumbersome but also prone to omissions.The Anqi CMS provides a very convenient way for us to dynamically retrieve and display the current year in templates, ensuring that the website information is up-to-date.

The core of dynamically obtaining the current year:nowTag

The Anqi CMS template system adopts a syntax similar to the Django template engine, which has a very practicalnowThe tag is used specifically to retrieve and display the current date and time in the template.The strength of this tag lies in the flexibility to specify the output time format according to your needs, and getting the current year is one of its most common applications.

To display the current year only, we just need tonowSpecify a special formatting string after the label. This string is not a common oneYYYYoryearbut a time formatting reference specific to the Go language.2006So, when you need to display the current year in a template, just write:

{% now "2006" %}

This way, regardless of when your website is accessed, the displayed year will always be the current year of the server, without the need for manual modification, greatly enhancing the efficiency of content maintenance.

Apply the current year to the actual scene

UnderstoodnowAfter the basic usage of the tag, we can then apply it to various places on the website where dynamic year display is needed

1. The copyright information at the bottom of the website

The copyright statement at the footer of the website usually includes the current year, this isnowThe most typical application scenario of tags. We can combine the provided by Anqi CMS.systemLabel to retrieve the website name, forming a complete dynamic copyright statement:

<p>&copy; {% now "2006" %} {% system with name="SiteName" %} All Rights Reserved.</p>

This code will automatically display the current year, followed by the website name configured in the background \

2. Date提示 in the content or on the page

In addition to copyright information, you may also want to display the current date, even more detailed time, in certain specific pages or content areas.nowTags can also handle it easily. For example, if you want to prompt "Today is: [current date]" on a page:

<p>今天是:{% now "2006年01月02日" %}</p>
<p>当前时间是:{% now "15:04:05" %}</p>

Please note,"2006年01月02日"and"15:04:05"The same is the time formatting reference value in Go language, which represents year, month, day, hour, minute, and second.By adjusting this formatted string, you can obtain any date or time display effect you want.

Some details need to be noticed.

While usingnowWhen dynamically retrieving and displaying the current year, there are several details that are worth paying attention to:

  • Go language time formattingAs mentioned before, the time formatting string in Anqi CMS template follows the Go language specification, which means2006represents the year,01represents the month,02represents the date,15represents the hour (24-hour format),04represents the minute,05Represents seconds. At first glance, it may seem a bit special, but once you remember this set of numbers (2006-01-02 15:04:05), you can easily master various time formats.
  • Server time:{% now %}The tag retrieves the current time of the server on which the website is deployed. Therefore, make sure your server time is set correctly to avoid displaying inaccurate dates and times.

BynowThe flexible use of tags allows us to keep the date information on the Anqi CMS website dynamic, reducing the cost of manual maintenance, and also providing visitors with more timely and accurate content experience.


Frequently Asked Questions (FAQ)

Q1: Why{% now %}The formatted string of the tag should be used2006Instead of likeYYYYSuch a general representation?

This is because AnQi CMS is developed based on Go language, its template rendering engine follows the standard of Go language in time formatting. In Go language, time formatting is not done through predefined letters such asY/M/D),instead through a reference date(2006年01月02日15时04分05秒)to define. The date-time string you write in your template is actually parsed by Go language, representing the part you need to output with the numbers at the corresponding positions in the reference date. For example,“}]2006Represents the year,01Represents the month,02Understanding this will allow you to easily master the Go language's time formatting rules.

Q2: I can use{% now %}Do you want to display the publication year of the article?

Cannot be used directly{% now %}Do you want to display the publication year of a specific article?{% now %}Tags are used to retrieve and displaycurrentThe server time. If you need to display the publication year of an article or content, you should use the publication time field contained in the article data itself (for exampleitem.CreatedTime), and combine it with the information provided by AnqicmsstampToDateLabel formatting. For example:{{ stampToDate(item.CreatedTime, "2006") }}This can accurately display the publication year of each article.

Q3:{% now %}What time zone is the time label obtained in?

{% now %}The label gets the current time of the time zone where the website server is located.If you need to display the time in a specific time zone, you may need to adjust the server's system time settings, or look for a related time zone configuration option in the Anqi CMS backend global settings (if the system provides one).In most cases, websites use the local time of the server as the standard.