This is the 12th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

FreeMarker’s List directive

Grammar 1

Else Optional, showing else content if there is no iterated data

Sequence The name of the data variable you want to iterate over

Item Specifies the variable name of the iteration item

Syntax 2

When you want to use tags such as lists and tables in the list directive, if you do not nest items directive, when the data has no content, the output will be empty list, so we use items to avoid empty list and empty table in the HTML page.

Sequence The name of the data variable you want to iterate over

Item Specifies the variable name of the iteration item

Sep instruction

When you need to put something in each iteration project, we can use the sep directive (insert nothing before or after the first item).

Break the instructions

Jump out during any iteration and use it in conjunction with the if instruction

Note: Like else and items, break can only be used inside the instruction body and cannot be moved out of the macro or contained template

Ascending sort_by ()

Grammar: < # list list? Sort_by (” field “) as x> </#list>

Descending sort_by ()? reverse

Grammar: < # list list? Sort_by (” field “)? reverse as x> </#list>

The list traversal Map

<#list dataMap? keys as key>${dataMap[key]}</#list>

Grammar reference freemarker. Foofun. Cn/index. The HTML

FreeMarker’s built-in function

We’ve already touched on the built-in functions

sort_by

reverse

has_next

What they have in common

Are they all written on? The back of the no.

Can implement a function.

What is a built-in function

Built-in functions are functions that exist syntactically. These functions are included in the compiler’s runtime library. Programmers do not have to write code to implement them, but simply call them. Their implementation is done by the compiler’s corresponding vendor. Simply put, it is a function that can be used without importing any external resources.

Except for the alphabetical index, all functions are built – in.

FreeMarker in Web projects

Change the example using JSP to FreeMarker

  1. Create a new Springboot project and replace the STARTER JSP part of the previous JSP project with the FreeMarker part, otherwise unchanged
  2. Copy the contents of controller, Repository, entity directly
  3. To set up the configuration file, in addition to the database, you need to configure the FreeMarker suffix to FTL
  4. Create an FTL file in the templates folder, copy the contents of the JSP file, and rewrite the code
  5. test