/**
  * This is a simple example that demonstrates the most basic
  * features of the template system. See example1.tlt for
  * more information.
  */

import ee.mare.indrek.jtlt.*;
import ee.mare.indrek.jtlt.macros.*;

class SimpleMacro extends MacroAdapter {
  SimpleMacro ()
  {
    super ("GIRLS");
  }

  public String process(TemplateParams ctx, String name, String args)
  {
    return "Little girls like to " + args;
  }
}

public class Example1 {
  public static void main (String[] args) throws Exception
  {
    // Generate our own context
    TemplateContext ctx = new TemplateContext ();

    // We complain to stdout
    ctx.setComplainTo (System.out);

    // Register our custom macro
    ctx.registerMacro (new SimpleMacro());

    // Create generator and instance a template.
    TemplateGenerator gen = new TemplateGenerator ("example1.tlt", ctx);
    Template tlt = gen.createTemplate();

    // Replace a single key in current open block
    tlt.replace ("hw", "Hello, world!");

    // Instance the 'block' block.
    tlt.lock ("block");
    tlt.replace ("val", "this should be in block");
    tlt.unlock ();

    // Now demonstrating the nested block usage
    tlt.nreplace ("nested_key", "nested key replacement");
    tlt.lock ("level1");
    tlt.lock ("level2");
    tlt.instance ("level3");  // lock/unlock the block level3 three times.
    tlt.instance ("level3");
    tlt.instance ("level3");
    tlt.unlock ();
    tlt.unlock ();

    // Now creating a mixed set of different types of rows.
    tlt.lock ("row");
    tlt.instance ("A");
    tlt.unlock ();
    tlt.lock ("row");
    tlt.instance ("B");
    tlt.unlock ();
    tlt.lock ("row");
    tlt.instance ("A");
    tlt.unlock ();
    tlt.lock ("row");
    tlt.instance ("B");
    tlt.unlock ();

    // What happens when same key is replaced twice or more?
    tlt.replace ("twice_key", "set value first time");
    tlt.replace ("twice_key", "set value second time");

    System.out.println (tlt.toString());
  }
}



syntax highlighted by Code2HTML, v. 0.9.1