Freemarker macro and function usage and difference, freemarker macro usage
1. macro (macro)
Statement:
<# Macro page url page maskSize = 5>
<# If (number = pages)>
<Li> <a href = "javascript: void (0);"> next page </a> </li>
<Li> <a href = "javascript: void (0);"> last page </a> </li>
<# Else>
<Li> <a href = "$ {buildPageUrl (url2, page. pageNum + 1, page)} "> (Note: You can directly call the function in the FTL where the macro is located.) Next page </a> </li>
<Li> <a href = "$ {buildPageUrl (url2, page. pages, page)}"> last page </a> </li>
</# If>
</# Macro>
Introduction Method: <# import "/user/common/macro/page. ftl" as pg>
Call method: <@ pg. page url = "$ {queryUrl}" page = page maskSize = 10/>
2. Functions)
Statement:
<# Function buildPageUrl url pageNum data>
<# Assign pageUrl = "$ {url }? PageNum =$ {pageNum} & pageSize =$ {data. pageSize} ">
<# Return pageUrl/>
</# Function>
Introduction Method: <# include "/user/common/macro/date. ftl"/>
Call method: $ {buildPageUrl (url2, page. pageNum + 1, page )}
3. Different Places
<# Return/> the usage, import method, and call method are different.
MACRO: return in advance, for example, <# return/>
But not <# return 1>, A macro cannot return a value
Macro, the main function is to splice the content and display the internal strings of the macro. the return value is meaningless.
Function: Unlike macro calls, $ {buildPageUrl (url2, page. pageNum + 1, page)} executes the function directly.
The return value is the most critical result, not to display the string content inside the function.
---------------
In addition, the function is defined in the ftl where the macro is located, which can be directly used in the macro.