Freemarker custom commands and freemarker commands

Source: Internet
Author: User

Freemarker custom commands and freemarker commands

1 Overview

Custom commands can be defined using macro commands, which is a concern of template designers. Java programmers can use the freemarker. template. TemplateDirectiveModel class to extend the definition of commands in Java instead of implementing definition commands in templates.


2 Basic Content

The macro command itself does not print anything. It just creates macro variables, so there will be a variable named greet. Content (called macro definition) between <# macro greet> and </# macro> is executed when it is used as a command.

<#macro greet><font size="+2">Hello Joe!</font></#macro>
You can use the custom command in the FTL tag by @ instead. Use the variable name as the command name.

<@greet></@greet>
<@greet/>

3 parameters

The position behind the macro name is used to define variables.

<#macro greet person><font size="+2">Hello ${person}!</font></#macro>
You can use this macro as follows:
<@greet person="Fred"/> and <@greet person="Batman"/>
When a predefined command is used, the parameter value (the value after =) can be an FTL expression. In this way, unlike the quotation marks of HTML, "Fred", and "Batman", you don't need to use them. <@ Greet person = Fred/> also means that the variable value Fred is used as the person parameter, rather than the string "Fred ".

You can also use a complex expression (for example, someParam = (price + 50) * 1.25) on the left of "= ).

You can have multiple parameters.

<#macro greet person color>    <font size="+2" color="${color}">Hello ${person}!</font></#macro>
Macro can be used as follows:

<@greet person="Fred" color="black"/>
<@greet color="black" person="Fred"/>
You can only use the parameters defined in the macro command. At the same time, you must also specify the values of all parameters in the macro.

Specify the default value:

<#macro greet person color="black"><font size="+2" color="${color}">Hello ${person}!</font></#macro>
<@ Greet person = "Fred"/> because it is the same as <@ greet person = "Fred" color = "black"/>

If you want to set the color to "red", write it as: <@ greet person = "Fred" color = "red"/>


SomeParam = foo and someParam = "$ {foo}" are different. If the someParam Command requires a numeric value, do not use the second method.

4. nested content

<#macro border>   <table border=4 cellspacing=0 cellpadding=4><tr><td><#nested>   </td></tr></table></#macro>
<# Nested> the template code segment between the start and end mark commands. Write the following statement:

<@border>The bordered text</@border>
The output is as follows:
<table border=4 cellspacing=0 cellpadding=4><tr><td>The bordered text</td></tr></table>
The nested command can also be called multiple times.
<#macro do_thrice>   <#nested>   <#nested>   <#nested></#macro><@do_thrice>   Anything.</@do_thrice>
The output is as follows:
Anything.Anything.Anything.

The nested content can be any valid FTL, including other user-defined commands. This is also true:
<@border> <ul>  <@do_thrice>   <li><@greet person="Joe"/>  </@do_thrice> </ul></@border>
<table border=4 cellspacing=0 cellpadding=4><tr><td><ul> <li><font size="+2">Hello Joe!</font> <li><font size="+2">Hello Joe!</font> <li><font size="+2">Hello Joe!</font></ul></tr></td></table>
In the nested content, macro local variables are invisible.
<#macro repeat count> <#local y = "test"> <#list 1..count as x>  ${y} ${count}/${x}: <#nested> </#list></#macro><@repeat count=3>${y!"?"} ${x!"?"} ${count!"?"}</@repeat>
test 3/1: ? ? ?test 3/2: ? ? ?test 3/3: ? ? ?
The local variable settings are called by each macro.
<#macro test foo>${foo} (<#nested>) ${foo}</#macro><@test foo="A"><@test foo="B"><@test foo="C"/></@test></@test>
A (B (C () C) B) A

5. Macro and cyclic Variables
The name of the cyclic variable is given, and the variable value is set by the instruction itself.
<# Macro do_thrice> <# nested 1> <# nested 2> <# nested 3> </# macro> <@ do_thrice; x> <# -- use ";" to replace "as" -- >$ {x} Anything. </@ do_thrice>
1 Anything.2 Anything.3 Anything. The syntax rule is special
The parameter of the nested command (of course, the parameter can be any expression. The name of the loop variable is determined by a semicolon after the parameter of the Start mark (<@...>) of the custom command.

Multiple cyclic variables can be used (the order of variables is important ):
<#macro repeat count> <#list 1..count as x>  <#nested x, x/2, x==count> </#list></#macro><@repeat count=4 ; c, halfc, last> ${c}. ${halfc}<#if last> Last!</#if></@repeat>
1.2.3.4.0.511.52 Last!

Variables can be left unspecified, as shown below:

<@repeat count=4 ; c, halfc, last> ${c}. ${halfc}<#if last> Last!</#if></@repeat><@repeat count=4 ; c, halfc> ${c}. ${halfc}</@repeat><@repeat count=4> Just repeat it...</@repeat>
If more variables than the nested command are specified after the semicolon, the last loop variable will not be created (not defined in the nested content ).






In freemarker, how do I call a java method based on built-in functions?

<@ Custom function name/> This method can be obtained

How to Use the struts2 tag in the freemarker Template

1. Copy the tag library definition file (that is, the tld file) required by the system to the web application. For the struts2 system, you need to copy the struts2-core-2.0.6.jar in the struts-tag.tld to the web application. Note: the struts-tag.tld is under the struts2-core-2.0.9.jar folder of the META-INF package. 2. In the web. start jspsuppservlet servlet in xml <servlet-namejspsuppservlet servlet </servlet-name <servlet-classorg.apache.struts2.views.JspSupportServlet </servlet-class <load-on-startup1 </load-on-startup </servlet3, use in freemarker Template import the assign command to the tag library, the code for importing the tag library is as follows: 4. Use the <@ ww. replace text with <@ s. text: <@ s. debug/

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.