In daily website content operations, we often encounter the need to extract specific information from data, such as extracting the year from a long number.AnQi CMS provides rich template filters and tags to help us flexibly handle these data.Today, let's talk aboutget_digitA filter, and explore its application in the specific scenario of extracting years, as well as other methods that are more suitable for extracting years.
First, let's understandget_digitWhat is the filter used for. 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 should be especially noted that the keywords "reverse" and "single digit" are involved.When we useget_digitWhen, it starts counting from the rightmost side (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 number8Then it can be used like this:{{ 1234567890|get_digit:3 }}After execution, you will get8This result. If the specified position does not exist, it usually returns the entire number.
However, when our goal is to extract the "year" from20231225such numbers.2023,get_digitThe filter seems to be inadequate. This is because it can only extract one digit at a time. The year is usually a sequence of four digits, andget_digitNot designed to extract continuous number segments. It is more like 'pointing out' a single digit in a number string. Therefore, if used directly,get_digitIt is not possible to extract the full four-digit year.
How can we efficiently and accurately extract years from the data in Anqi CMS? There are several more recommended and practical methods here.
The first method: Using timestamp formatting function
In Anqi CMS, the creation time of the article (CreatedTime) Date fields are usually stored in timestamp format. If you need to extract the year from such a timestamp,stampToDateTags are your ideal choice. It can convert timestamps into 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") }}Herearchive.CreatedTimeIt will get the current creation timestamp of the article, while"2006"It is the special format string used to represent the year in Go language. In this way, you can easily get information like2023such year information.
The second method: extract the year by combining string operations
If your 'long number' is not a timestamp, but more like20231225(Year-Month-Day) such a pure number format, you can first convert it to a string, and then use the string slicing function 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:Next, use
sliceThe filter extracts the first four characters from the beginning of the string.
Assuming you have a variablelongDateThe value is20231225To get the year, you can do this:{% set longDate = 20231225 %}
{{ longDate|stringformat:"%s"|slice:"0:4" }}This code will first convert20231225Convert to string"20231225", thenslice:"0:4"It will cut from index 0 to index 4 (not included), that is"2023"So that you get the year.
In summary,get_digitA filter is a tool used to extract individual specific position digits from numbers, not for extracting continuous number sequences, such as years. In Anqi CMS, you can flexibly choose to use it according to the form of your data storage.stampToDateLabel timestamp formatting or combinestringformatandsliceThe filter performs string operations to accurately extract the required year information. Choose the method most suitable for your data type to make your content operation work more efficient and convenient.
Frequently Asked Questions (FAQ)
Q1:get_digitCan the filter extract consecutive digits from a number, such as months or dates?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 digit4. It cannot extract a continuous sequence of numbers at one time, such as months (two digits) or dates (two digits).If you need to extract continuous numeric segments, you usually need to convert the number to a string first, and then use relevant filter or method for string slicing.
Q2: If my date data is not a timestamp, nor like20231225How do I extract the year from a string like '2023年12月25日' instead of pure numbers?A2: You can consider using for date strings like "2023 December 25"sliceThe filter performs the cut, but the position of the year part must be 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 on the backend (if the template supports more complex regular expressions) to match and extract.
Q3: Besides the year, I can also usestampToDateWhich date information can be formatted by the tag?A3:stampToDateThe tag is very powerful, it supports all date formatting strings in the Go language. Besides the year ("2006"),You can also format the month("01")、the date("02")、the hour("15")、the minute("04")、the second("05")such. You can combine these format strings to display the full date and time, for example{{stampToDate(item.CreatedTime, "2006-01-02 15:04")}}It will display年-月-日 时:分.