net.sourceforge.jtpl
Class Jtpl

java.lang.Object
  extended by net.sourceforge.jtpl.Jtpl

public class Jtpl
extends Object

Jtpl: a very simple template engine for Java
Contact: emmanuel.alliel@gmail.com
Web: http://jtpl.sourceforge.net

Version:
$LastChangedRevision: 51 $
Author:
Emmanuel ALLIEL, Staffan Olsson

Template syntax:
   Variables:
      {VARIABLE_NAME}
   Blocks:
      <!-- BEGIN: BlockName -->
          <!-- BEGIN: SubBlockName -->
          <!-- END: SubBlockName -->
      <!-- END: BlockName -->

License: Apache 2.0


Constructor Summary
Jtpl(File file)
          Constructs a Jtpl object and reads the template from a file.
Jtpl(Reader template)
          Constructs a Jtpl object and reads the template from arbitrary input source.
Jtpl(String fileName)
          Constructs a Jtpl object and reads the template from a file.
 
Method Summary
 void assign(String varName, String varData)
          Assign a template variable.
protected  String escape(String replacement)
          Template parsing uses regex replace to insert result text, which means that special characters in replacement string must be escaped.
protected  Set locateBlock(String blockName)
          Lists the blocks that end with the given blockName.
 String out()
          Generates the HTML page and return it into a String.
 void parse(String blockName)
          Parse a template block.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Jtpl

public Jtpl(String fileName)
     throws IOException
Constructs a Jtpl object and reads the template from a file. For backwards compatibility this constructor enables 'failSilently'.

Parameters:
fileName - the file name of the template, exemple: "java/folder/index.tpl"
Throws:
IOException - when an i/o error occurs while reading the template.

Jtpl

public Jtpl(File file)
     throws IOException
Constructs a Jtpl object and reads the template from a file.

Parameters:
file - readable file containing template source
Throws:
IOException - when an i/o error occurs while reading the template.

Jtpl

public Jtpl(Reader template)
     throws IOException
Constructs a Jtpl object and reads the template from arbitrary input source.

Parameters:
template - the template source
Throws:
IOException - when an i/o error occurs while reading the template.
Method Detail

assign

public void assign(String varName,
                   String varData)
Assign a template variable. For variables that are used in blocks, the variable value must be set before parse is called.

Parameters:
varName - the name of the variable to be set.
varData - the new value of the variable.

out

public String out()
Generates the HTML page and return it into a String.


parse

public void parse(String blockName)
           throws IllegalArgumentException
Parse a template block. If the block contains variables, these variables must be set before the block is added. If the block contains subblocks, the subblocks must be parsed before this block.

Parameters:
blockName - the name of the block to be parsed.
Throws:
IllegalArgumentException - if the block name is not found (and failSiletly==false)

escape

protected String escape(String replacement)
Template parsing uses regex replace to insert result text, which means that special characters in replacement string must be escaped.

Parameters:
replacement - The text that should appear in output.
Returns:
Text escaped so that it works as String.replaceFirst replacement.

locateBlock

protected Set locateBlock(String blockName)
Lists the blocks that end with the given blockName.

Parameters:
blockName - The name as used in parse
Returns:
Blocks where blockName is the child (the Set's toString lists the full names)


Copyright © 2009. All Rights Reserved.