When operating a website, we often need to display the current year in the footer copyright statement, for example, “© 2023 All Rights Reserved.”. As time goes by, this year needs to be updated every year. If it is manually modified, it is undoubtedly a repetitive and easily overlooked task.AnQi CMS fully considers this common requirement and provides a very intuitive and powerful template tag, allowing you to easily implement dynamic year display, so that your website's copyright information is always up to date.

The core method of dynamically obtaining the current year

The Anqi CMS template system is based on the powerful capabilities of Go language, providing a rich set of tags and filters. To get and display the current year, you just need to add a specific tag to the template, which is{% now %}.

This tag can directly obtain the current date and time of the server in the template. What we are concerned about is just the year. Therefore, we can use{% now %}Specify a formatting string to precisely control its output.

To get and display the current year, you just need to add it in the template:

{% now "2006" %}

You might be curious about this."2006"What does it mean? It does not refer to a specific year, but rather a special reference time used to define time formats in the Go language.2006This numeric string represents the four-digit format of the year. When you use it in{% now %}tags, the system will automatically extract and display the four-digit year of the current date on the server.

For example, if the current year is 2023, then write in the template{% now "2006" %}and it will be displayed on the final page2023. When it comes to 2024, it will automatically change to2024Do not make any manual modifications.

More time formatting options

Except for the year,{% now %}Labels can display full date and time in multiple formats.Once you master the Go language time formatting string rules, you can flexibly combine the required date and time format.Some common formatting strings and their effects include:

  • Display the full date and month and day: {% now "2006-01-02" %}(For example: 2023-10-27)
  • Display the date in Chinese format: {% now "2006年01月02日" %}(For example: October 27, 2023)
  • Show hours, minutes, and seconds: {% now "15:04:05" %}(For example: 10:30:00)
  • Show the full date and time: {% now "2006-01-02 15:04:05" %}(For example: 2023-10-27 10:30:00)

By these combinations, you can dynamically display current date and time information in any format according to the specific requirements of the website design.

It is worth mentioning that if your requirement is to display the publication year of the article instead of the current year, AnQi CMS also provides{{stampToDate(时间戳, "格式")}}Such a filter. It can format the timestamp field stored in the database (such as theCreatedTimeorUpdatedTime), but it is different from{% now %}the purpose of obtaining the current system time.

Actual application in template

Generally, we would place the copyright information at the footer of the website,<footer>Within the tag. To ensure that all pages of the website are displayed uniformly and to avoid adding the same code repeatedly on each page, it is recommended to set it in the public template file of the website, such asbase.htmlorpartial/footer.htmlto configure.

Before yourbase.htmlorpartial/footer.htmlIn the file, find the location where copyright information is usually placed, and then replace the static year with a dynamic tag:

<!-- 示例:将静态年份替换为动态年份 -->
<footer>
    <p>&copy; {% now "2006" %} 您的公司名称. All Rights Reserved.</p>
    <!-- 其他页脚内容 -->
</footer>

After saving the template file, refresh your website, and you will find that the copyright year at the footer has automatically displayed as the current year.

Brings convenience and value

By using in Anqi CMS template{% now %}Tag dynamically displays the year, you have gained the following significant convenience and value:

  • Save time and energy:Say goodbye to the tedious work of manually updating the copyright year every year, giving you more time to focus on website content and operations.
  • Ensure information accuracy:The website's copyright information is always kept up to date to avoid outdated information due to forgetting to update.
  • Enhance the professionalism of the website:A real-time updated website detail, leaving visitors with a more professional, well-maintained impression.
  • Reduce maintenance costs:Automated processing details, reducing the need for manual intervention, thereby reducing the long-term maintenance costs of the website.

The AnQi CMS is committed to providing an efficient, customizable and easy-to-use content management solution.Such small features as dynamic year display are a reflection of the design philosophy in the details, aiming to help users manage and operate the website more conveniently.


Frequently Asked Questions (FAQ)

  1. Question:{% now "2006" %}and{{stampToDate(archive.CreatedTime, "2006")}}What is the difference?Answer: The main difference between them lies in the source of time acquisition.{% now "2006" %}The year displayed is the current year of the server, used for dynamic content that changes with the actual date, such as copyright information, real-time timestamps, and so on. And{{stampToDate(archive.CreatedTime, "2006")}}is used for formattingThe creation or update timestamp of specific content (such as articles or products)It displays the year of content publication or last modification and does not automatically update with the current year.

  2. Ask: Have I set up{% now "2006" %}Do I need to manually change the year every year?Answer: Absolutely not. Once you have correctly used it in the template{% now "2006" %}Label, the system will automatically retrieve and display the current year from the server. This means that your copyright information will always be up to date without any manual intervention.

  3. Question: Can I use{% now %}Does the label display other time information?Answer: Of course you can.{% now %}The label supports Go language time formatting strings. For example, you can use{% now "2006-01-02" %}Display the complete year, month, and day (e.g., 2023-10-27), or{% now "15:04" %}Display the current time (e.g., 10:30). Once you master the Go language time formatting reference rules (for example2006Represents the year,01Represents the month,02represents the day,15represents hours,04Represents minutes, etc.), you can flexibly combine it according to your needs to display any date and time format you want.