Common List handling methods in freemarker

Source: Internet
Author: User

 

List is frequently used in freemarker.

For example, you need a simple traversal (taking the year as an example) on the page without calling any data, which is very convenient.

 

Code:

In this case, a problem occurs. When the data exceeds three digits, freemarker automatically uses a comma to intercept the format, for example, 2,008.

How can this problem be solved?

1> Add. tostring (), for example: $ {(userid). tostring ()}
2> Add? C, such as: $ {(userid )? C}
3> Add freemarker configuration file freemarker. PropertiesNumber_format = #

Judge whether it is null:

1. <# If XX? Exists>

2. <# If XX?>

3. <# If XX? Default ("XXX")>

Otherwise, when xxlist has no data, a huge yellow page will be waiting for you.

Data type conversion:

$ {XX? String} // string
$ {XX? Number} // integer
$ {XX? Currency} // decimal number
$ {XX? Percent} // percentage

Truncation String Length:

Sometimes we do not need to display such long strings on the page, such as the news title. In this way, we can use the following columns to customize the display length.

<Lt. <= LTE.> GT.> = GTE

<# If jstbqkvo.gz dd? Length Lt 8>
<A href >$ {jstbqkvo.gz dd? Default ("")} </a>
<# Else>
<A href Title = "zookeeper jstbqkvo.gz dd}" >$ {jstbqkvo.gz dd [0 .. 3]? Default ("")}... </a>
</# If>

This means that if the length of this string is less than 8, it will be displayed normally, and otherwise, it will take four digits.

Nested list:

In many cases, numbers are stored in the database, and the Chinese characters to be displayed on the page are not in the same table. Check the column.

<# List jssklist as jsskvo>

<# List kclist as KCVO>
<# If KCVO. kch = jsskvo. kch> (KCVO contains numbers and names, while jsskvo only has numbers)
$ {KCVO. KCM}
</# If>
</# List>

</# List>

Sort:

1. Ascending. sort_by ()

<# List? Sort_by ("field") as x>
</# List>

2. Descending Order. sort_by ()? Reverse

<# List? Sort_by ("field ")? Reverse as x>
</# List>

Remove space:

$ {XX? Trim}

Format Date:

$ {Date? String ("yyyy-mm-dd hh: mm: SS ")}
$ {Date? String ("yyyymmdd ")}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.