Freemarker the use and difference of macros and functions

Source: Internet
Author: User

1. Macros (macro)

wording :
< #macro page URL page masksize=5>
< #if (number = = pages) >

<li><a href= "javascript:void (0);" > Next </a></li>

<li><a href= "javascript:void (0);" > Last </a></li>

< #else >

<li><a href= "${buildpageurl (url2,page.pagenum+1,page)}" > (note: This place 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 </a></li>

</#if >

</#macro >

Introduction Method: < #import "/USER/COMMON/MACRO/PAGE.FTL" as PG >
Invocation Mode:< @pg. Page url= "${queryurl}" Page=page masksize=10/>


2. Functions (function)
notation:
< #function buildpageurl URL pagenum data>

< #assign Pageurl = "${url}?pagenum=${pagenum}&pagesize=${data.pagesize}" >

< #return pageurl/>

</#function >
Introduction Method:< #include "/USER/COMMON/MACRO/DATE.FTL"/>
Invocation Mode:${buildpageurl (url2,page.pagenum+1,page)}

3. Different places

< #return the use of/>, the introduction method, the calling method are different.

Macro: Can be returned early, such as < #return/>
But not < #return 1> A macro cannot return a value

Macro, the main role is to splice the content, the macro inside the string display, return value is meaningless.

functions : Unlike macros, they are called directly${buildpageurl (Url2,page.pagenum+1,page)} executes the function.
The return value is the most critical result, not to display the string content inside the function.

---------------

Add that the function is defined in the FTL where the macro is located, and the function can be used directly in the macro.

Freemarker the use and difference of macros and functions

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.