/**
* In this example we shall look into recursive macros.
* See example4.tlt for more information.
*/
import ee.mare.indrek.jtlt.*;
import ee.mare.indrek.jtlt.macros.*;
class GirlsMacro extends MacroAdapter {
GirlsMacro ()
{
super ("GIRLS");
}
public String process (TemplateParams ctx, String name, String args)
{
return "Girls like " + args + ", but <!--BOYS:" + args +
"-->.<!--KEY:ahem-->";
}
public boolean isRecursive ()
{
return true;
}
}
public class Example4 {
public static void main (String[] args) throws Exception
{
// Generate our own context
TemplateContext ctx = new TemplateContext ();
// Register our custom macro
ctx.registerMacro (new GirlsMacro ());
// A good chance to demonstrate the nonrecursive enclose macro
// that just wraps the macro argument between specified prefix
// and sufix.
ctx.registerMacro (new EncloseMacro("BOYS", "boys like ", " too"));
// Create generator and instance a template.
TemplateGenerator gen = new TemplateGenerator ("example4.tlt", ctx);
Template tlt = gen.createTemplate();
// This is confusing on purpose :)
tlt.replace ("fruit", "orange_key");
tlt.replace ("orange_key", "This is an apple");
// Our girls macro output also has a key named ahem. Lets provide it.
tlt.replace ("ahem", " Ahem!");
// Key-s are not recursive macros, see for yourself:
tlt.replace ("key_test", "Key contents - <!--GIRLS:flowers-->");
// Print the macro contents out
System.out.println (tlt.toString());
}
}
syntax highlighted by Code2HTML, v. 0.9.1