Freemarker macro and function usage and difference, freemarker macro usage

Source: Internet
Author: User

Freemarker macro and function usage and difference, freemarker macro usage

1. macro (macro)

<# 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)
<# 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.

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: 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.