Macro, nested, return instruction

Source: Internet
Author: User

Macro can be used to implement custom directives, by using custom directives, you can define a section of a template fragment as a user instruction, using the macro instruction syntax in the following format:
< #macro name param1 param2 ... paramn>
...
< #nested loopvar1, Loopvar2, ..., loopvarn>
...
< #return >
...
</#macro >
In the above format fragment, the following sections are included:
The Name:name property specifies the name of the custom directive and can pass in multiple parameters when using a custom directive
PARAMX: This property specifies the use of the custom directive times parameter, which must be passed to the value when using the custom directive
Nested directive: Nested label output when using the middle part of a custom directive
Cyclic variables in the nested directive: This loop variable is specified by the macro definition section, passed to the template using the label
Return Directive: This directive can be used to end the custom instruction at any time.

Look at the following example:
< #macro book>//define a custom directive
J2ee
</#macro >
< @book/>//Use the instructions you just defined
The above code outputs the result: Java EE

In the above code, it may be difficult to see the usefulness of a custom label, because the book directive we define contains very simple content, in fact, the custom label can contain a lot of content, which can achieve better code reuse. In addition, you can specify parameters for custom directives when you define custom directives. Look at the following code:
< #macro book booklist>    //define a custom directive Booklist is a parameter
< #list Booklist as book>
   ${book}
</#list >
</#macro >
< @book booklist=["Spring", "java ee"]/>  //Use the directive you just defined
The above code passes a parameter value for the book Directive, and the output of the above code is: Spring EE

Not only that, you can also use the nested directive when customizing directives to output the middle part of a custom directive, see the following example:
< #macro page title>
< html>
< head>
   <title>freemarker Sample Page-${title?html}</title>
< body >
      < #nested >      //Tag body for introducing user-defined Directives
</body>
</#macro >
The above code defines an HTML page template as a page directive, so you can do so in other pages:
< #import "/COMMON.FTL" as com>     //Assuming the template page above is named COMMON.FTL, Import page
< @com. Page title= "book list" >
< u1>
< li>spring</ Li>
< li>j2ee</li>
</ul>
</@com .page>

As can be seen from the above example, the use of macro and nested instructions can be very easy to achieve page decoration effect, in addition, you can also use the nested instruction, specify one or more loop variables, see the following code:
< #macro book>
< #nested 1>//Specify a loop variable value when using the book instruction
< #nested 2>
</#macro >
< @book;x> ${x}. Books </@book >
When you use the nested directive to pass in a variable value, you need to use a placeholder (such as after the book instruction; x) When you use the custom directive. The above code output text is as follows:
1. Books 2. Books

The return instruction is used to end the macro instruction, and once the return instruction is executed in the macro command, Freemarker does not continue to process the contents of the macro instruction, as shown in the following code:
< #macro book>
Spring
< #return >
J2ee
</#macro >
< @book/>
The above code output: Spring, and the EE is not output after the return instruction.

Macro, nested, return instruction

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.