In daily website content operations, we often encounter the need to extract specific information from data, such as extracting a year from a long number.The Auto CMS provides rich template filters and tags, helping us flexibly handle these data.get_digitFilter, and discuss its application in the specific scenario of extracting years, as well as other methods more suitable for extracting years.
Firstly, let's understand it.get_digitThe filter is for what purpose.This filter is designed very cleverly, its main function is to extract a single digit from a longer number in reverse order according to the specified position.It is especially important to note the keywords “reverse” and “single digit”.get_digitWhen, it starts counting from the rightmost digit (ones place), marking this position as 1. For example, if there is a number1234567890, we want to extract the third digit from the end, which is the number8English translation: ,那么就可以这样使用它:{{ 1234567890|get_digit:3 }}English translation: ,执行后,您会得到8English translation: 这个结果。如果指定的位置不存在,它通常会返回整个数字。
However, when our goal is to extract 'year', such as from20231225such numbers.2023,get_digitThe filter seems inadequate. This is because it can only extract one digit at a time. The year is usually a sequence of four digits,get_digitNot designed to extract continuous digital segments. It is more like 'pointing out' a specific digit from a string of numbers. Therefore, if used directly,get_digitIt is impossible to extract the complete four-digit year.
Then, how should we efficiently and accurately extract the year from the data in the AnQi CMS? There are several more recommended and practical methods.
First method: Using timestamp formatting function
In the AnQi CMS, the creation time of the article (CreatedTime)et dates fields are usually stored in the form of timestamps. If you need to obtain the year from such timestamps,stampToDateTags are your ideal choice. It can convert timestamps according to the format you specify, including extracting the year separately.
For example, if you want to display the publication year of an article detail page, you can use the following code:{{ stampToDate(archive.CreatedTime, "2006") }}Here are thearchive.CreatedTimeIt will get the current timestamp of the article."2006"is a special format string used to represent the year in Go language. In this way, you can easily get information like2023such as year information.
第二种方法:结合字符串操作提取年份
如果您的“较长的数字”并非时间戳,而是类似于English20231225(Year-Month-Day) such a pure number format, you can first convert it to a string, and then use string slicing to extract the year.
This process requires two steps:
- Convert numbers to strings:Use
stringformatThe filter can convert numbers to strings. - Extract the year part:Then, use
sliceThe filter extracts the first four characters from the string.
Suppose you have a variablelongDateThe value is20231225You can operate like this to get the year:{% set longDate = 20231225 %}
{{ longDate|stringformat:"%s"|slice:"0:4" }}This code will first convert20231225to strings"20231225"Then,slice:"0:4"It will extract characters from index 0 to index 4 (not inclusive), that is:"2023"Therefore, you get the year.
In short,get_digitFilter is a tool used to extract a single specific position digit from a number, not for extracting consecutive number sequences, such as years. In Anqi CMS, based on the storage form of your data, you can flexibly choose to usestampToDateLabel the timestamp formatting, or combinestringformatandsliceThe filter performs string operations to accurately extract the required year information. Choosing the most suitable method for your data type can make your content operation work more efficient and convenient.
Common Questions (FAQ)
Q1:get_digitFilter can extract consecutive digits from a number, such as a month or date?A1: No.get_digitThe design purpose of the filter is to extract in reverse order from a numbera single digitfor example from12345Extract the second-to-last digit from4.It cannot extract consecutive numeric sequences in one go, such as months (two digits) or dates (two digits).If you need to extract continuous numeric fragments, you usually need to convert the number to a string first, and then use the relevant filter or method to slice the string.
Q2: If my date data is not a timestamp, nor like20231225How can I extract the year from such pure numbers, but like 'December 25, 2023' such a string?A2: For date strings like "2023-12-25", you may consider usingsliceFilter performs truncation, but the prerequisite is that the position of the year part is fixed. For example, if the year is always in the first four digits, you can do this:{{ "2023年12月25日"|slice:"0:4" }}If the date format varies, it may be necessary to handle it in frontend JavaScript or match and extract it on the backend (if the template supports more complex regular expressions).
Q3: Besides the year, can I also usestampToDateTag formatting which date information?A3:stampToDateTags are very powerful, they support all date formatting strings in Go language. Besides the year ("2006"),You can also format the month ("01")、date ("02")、hour ("15")、minute ("04")、second ("05")et al. You can combine these format strings to display the full date and time, for example{{stampToDate(item.CreatedTime, "2006-01-02 15:04")}}will be displayed年-月-日 时:分.