Freemarker custom methods and instructions

Source: Internet
Author: User

1.From
Definition
Method to inherit the templatemethodmodel.
Example:
Public class indexofmethod implements templatemethodmodel {

Public templatemodel exec (list ARGs) throws templatemodelexception {
If (ARGs. Size ()! = 2 ){
Throw new templatemodelexception ("wrong arguments ");
}
Return new simplenumber (
(String) args. Get (1). indexof (string) args. Get (0 )));
}
}
After adding the file to the root user, you can directly use the file in the template:
Root. Put ("indexof", new indexofmethod ());
The call method is as follows:
<# Assign x = "something">
$ {Indexof ("met", x )}
$ {Indexof ("foo", x )}

2.From
Definition
Command to inherit the templatedirectivemodel.
Example:
Package com. example;
Import java. Io. ioexception;
Import java. Io. Writer;
Import java. util. Map;

ImportFreemarker
. Core. environment;
ImportFreemarker
. Template. templatedirectivebody;
ImportFreemarker
. Template. templatedirectivemodel;
ImportFreemarker
. Template. templateexception;
ImportFreemarker
. Template. templatemodel;
ImportFreemarker
. Template. templatemodelexception;

/**
*Freemarker
User-Defined directive that progressively transforms
* The output of its nested content to upper-case.
*
*
* <P>Directive info
</P>
*
* <P> directive parameters: None
* <P> loop variables: None
* <P> directive nested content: Yes
*/
Public class upperdirective implements templatedirectivemodel {

Public void execute (Environment ENV,
Map Params, templatemodel [] loopvars,
Templatedirectivebody)
Throws templateexception, ioexception {
// Check if no parameters were given:
If (! Params. isempty ()){
Throw new templatemodelexception (
"This Directive doesn't allow parameters .");
}
If (loopvars. length! = 0 ){
Throw new templatemodelexception (
"This directive doesn' t allow loop variables .");
}

// If there is non-empty nested content:
If (body! = NULL ){
// Executes the nested body. Same as <# nested> in FTL, limit t
// That we use our own writer instead of the current output writer.
Body. Render (New uppercasefilterwriter (Env. getout ()));
} Else {
Throw new runtimeexception ("missing body ");
}
}

/**
* A {@ link writer} that transforms the character stream to upper case
* And forwards it to another {@ link writer }.
*/
Private Static class uppercasefilterwriter extends writer {

Private Final writer out;

Uppercasefilterwriter (writer out ){
This. Out = out;
}

Public void write (char [] cbuf, int off, int Len)
Throws ioexception {
Char [] transformedcbuf = new char [Len];
For (INT I = 0; I <Len; I ++ ){
Transformedcbuf [I] = character. touppercase (cbuf [I + Off]);
}
Out. Write (transformedcbuf );
}

Public void flush () throws ioexception {
Out. Flush ();
}

Public void close () throws ioexception {
Out. Close ();
}
}

}
You can add it to the root user and directly call it in the template:
Root. Put ("upper", new COM. example. upperdirective ());
You can also call the following method in the template after initialization:
<# Assign upper = "com. example. upperdirective "? New ()>
The call method is as follows:
<@ Upper>

Bar
<# -- All kind of FTL is allowed here -->
<# List ["red", "green", "blue"] as color>
$ {Color}
</# List>
Baaz
</@ Upper>

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.