When operating a website, we often need to display the current year in the footer copyright statement, such as “© 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.The AnQi CMS fully considers this common need 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 to dynamically get the current year
The template system of Anqi CMS is based on the powerful capabilities of the Go language, providing rich tags and filters. To get and display the current year, you only need to add a specific tag in the template, which is{% now %}Label.
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 simply by{% now %}Label specifies a format string to control the output precisely.
To get and display the current year, you just need to add it in the template:
{% now "2006" %}
You may be curious about,"2006"What does it mean? It does not refer to a specific year, but is a special reference time used for defining time formats in the Go language.2006This number string represents the four-digit format of the year. When you use it in the label, the system will automatically extract and display the four-digit year of the current date of the server.{% now %}The system will automatically extract and display the four-digit year of the current date of the server when used in the label.
For example, if the current year is 2023, then you write in the template{% now "2006" %}and it will be displayed on the final page2023. When it comes to 2024, it will automatically become2024No manual modification is required.
More time format options
Except for the year,{% now %}Labels can also display the full date and time in various formats.If you master the regularity of Go language time format strings, you can flexibly combine the date and time format you need.
- Display the full year, month, and day:
{% now "2006-01-02" %}(e.g., 2023-10-27) - Display Chinese formatted date and month and day:
{% now "2006年01月02日" %}(For example: October 27, 2023) - Display hours, minutes, and seconds:
{% now "15:04:05" %}(For example: 10:30:00) - Display the full date and time:
{% now "2006-01-02 15:04:05" %}(e.g., 2023-10-27 10:30:00)
Through 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 year of the article's publication rather than the current year, Anqi CMS also provides{{stampToDate(时间戳, "格式")}}Such a filter. It can format the timestamp field stored in the database (such as the articles'CreatedTimeorUpdatedTime), but it is different from{% now %}the purpose of obtaining the current system time.
actual application in the template
Generally, we would place copyright information in the footer section of the website.<footer>Labels inside. To ensure that all pages of the website display uniformly and avoid adding the same code repeatedly on each page, it is recommended that you set it in the public template files of the website, such asbase.htmlorpartial/footer.html.
In yourbase.htmlorpartial/footer.htmlIn the document, find the usual location for copyright information, and then replace the static year with a dynamic tag:
<!-- 示例:将静态年份替换为动态年份 -->
<footer>
<p>© {% 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.
The convenience and value it brings
Through using the Anqi CMS template{% now %}Dynamic display of the year with tags, you have gained the following significant convenience and value:
- Save time and energy:Completely bid farewell to the cumbersome task of manually updating the copyright year every year, allowing you to focus more time on website content and operations.
- Ensure information accuracy:The copyright information of the website is always kept up to date to avoid outdated information due to forgetting to update.
- Enhance the professionalism of the website:A website detail that is updated in real time, leaving visitors with a more professional and well-maintained impression.
- Reduce maintenance costs:Automated processing of details, reducing the need for manual intervention, thereby lowering the long-term maintenance costs of the website.
Safe CMS is dedicated to providing a highly efficient, customizable and easy-to-use content management solution.Features such as dynamic year display are a reflection of the design philosophy in the details, aiming to help users manage and operate websites more conveniently.
Common Questions (FAQ)
Q:
{% 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 *currently* displayed is used for dynamic content that changes with the actual date, such as copyright information, real-time timestamps, and so on.{{stampToDate(archive.CreatedTime, "2006")}}used for formattingThe timestamp for the creation or update 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.Question: Have I set it up?
{% now "2006" %}Then, do I need to manually change the year every year?Answer: Not at all. Once you have correctly used it in the template,{% now "2006" %}Tags, 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 on your part.Q: Besides the year, can I use
{% now %}Does the tag display other time information?Answer: Of course you can.{% now %}The tag supports Go language time formatting strings. For example, you can use{% now "2006-01-02" %}Show the full year, month, and day (e.g., 2023-10-27), or{% now "15:04" %}Show the current hour and minute (e.g., 10:30). As long as you master the time formatting reference rules of the Go language (such as2006representing a year,01Represents the month, 【en】:02Represents the day, 【en】:15represents the hour,04You can combine them flexibly according to your needs, displaying any date and time format you want to show.