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>