Dynamic Template in Java
JDynamiTe is a simple yet powerful tool used to create dynamic documents from "template" documents. It is based on the same concept as those of "FastTemplate", which is a very popular PHP extension used in many Web sites to create dynamic HTML pages. Note: JDynamiTe is not a Java port of FastTemplate.
The main benefit of JDynamiTe is to allow a true separation between the presentation (HTML, XML, etc) and content generation code (written in Java). JDynamiTe is not another template language, or a complete framework. It is a simple "brick" in your "MVC" architecture, "glue" between views and models.
JDynamiTe is a "pure" Java package, which is designed to be flexible and open.
JDynamiTe is free, under the GNU Library General Public License.
This project is hosted in SourceForge domain.
First simple exampleA simple example is better than a long explanation. Below is an example of a dynamic HTML document generation, from JSP (Java Server Page).
There are the three steps in JDynamiTe process:
1. Create a “Template Document”.This is the input file. You can see the HTML source code here, and visualize the page here.2. Use JDynamiTe Java code to parse and develop it.Click here to see the JSP code.3. Output the result to a file, to an HTTP client, etc.Click here to see the result.Note:- There is no HTML code in JSP code.
- There is only “pure” HTML in a template document (JDynamiTe tags are enclosed in HTML comments).
- You can find this example in the “main” method of the JDynamiTe class.
- See the "News" chapter which uses this example again with an interesting new feature.
Simple example with the "Ignored block" featureThis example is similar to the first example, but it shows the use of the "Ignored Element" block new feature (since JDynamiTe 1.1).
1. Create a “Template Document”.Now we can insert concrete data in this input file. This page is more realistic and can be a true working document for designers. You can see the new HTML source code here, and visualize the page here.2. Use JDynamiTe Java code to parse and develop it.This is the same code as in the first example ! Because "ignored blocks" are only helpful for page designers, not for programmers. Click here to see the JSP code.3. Output the result to a file, to an HTTP client, etc.We obtain the same page as the first example. Click here to see the result.
Another simple example with the "getVariableKeys" method (since JDynamiTe 1.2)Look at this small piece of code in the javadoc of "getVariableKeys".
JDynamiTe is not another template language, it is a Java package. It is very simple to use : you only need to use one class and 4 or 5 methods to parse and develop dynamic documents from templates (see example). These template documents can be any ASCII or Unicode texts. To work JDynamiTe only needs to recognize three kinds of “tag” in the template document (“Variable”, “Begin Dynamic Element”, “End Dynamic Element”). A "Dynamic Element" is a "block" which can be dynamically developed. Example of use: list, table, enumeration, etc. See documentation for more details. Since JDynamiTe analyser is highly customizable, any syntax can be defined for these tags. For example with the HTML template document (default syntax), the three tags are enclosed within the HTML comments. It enables template reuse (one analyse and several dynamic generations). It enables “Dynamic Elements” nesting. It enables template nesting (because a JDynamiTe object is a “Dynamic Element” !). An optional tag is available that allows inserting "ignored blocks" in the template document. Since JDynamite 1.2, the list of template document "Variable keys" are available (because computed during pre-analysis of the document). See the "News" chapter to understand the benefits of this feature.
Separates presentation (HTML, XML, ...) from the content generation code. JDynamiTe HTML templates can be designed with your preferred HTML builder, or by a “true” HTML designer. HTML designers do not have to know JSP, Java, or any new template language. Ideal for example to create HTML pages from JSP (Java Server Page).
JRE 1.2 or higher “gnu.regexp” package 1.0.8 (included in JDynamiTe distribution) or higher JDynamiTe is a "lightweight" package : less than 15 kb for jdynamite.jar
Extract “jdynamite.jar” and “gnu-regexp-1.0.8.jar” from the distribution and copy it in a place in such a way that the “import cb.jdynamite.*” Java sentence works !
Download last version in JDynamiTe SourceForge domain.
JDynamiTe is the unique class you need to know.
See the documentation of this class in javadoc.
JDynamiTe 1.2 released !
Main new feature : now all the Variable keys of the template document are recorded during analysis (i.e. in the "setInput" method). All the values of these key/value pairs are initialized with an empty string value.
See here an simple example of use of this new feature.
- you can get the list of these keys with a new method called "getVariableKeys". It can be useful at the beginning of your process, for example to get all the Variable values from your database, even if you do not know in advance the keys.
- no more warning message if a Variable is not explicitly set before parsing the template document.
Other news in JDynamiTe 1.2 version :
- A new "setInput" method in the cb.jdynamite.JDynamiTe class. This new method takes a BufferedReader as parameter to read the input template document. Thanks to Chris Bailey who suggested it (and posted a patch at SourceForge JDynamiTe project home page).
- Bug fixed in the DefaultAnalyser class. Now template elements (VariableElement, DynamicElement) identifiers are really found in the RegExp subexpression into the tag.
JDynamiTe 1.1 is available.
The main new feature is the "Ignored Element" block.
This optional element can be used in template documents. An "Ignored Element" is a "block" which will be completely ignored (skipped during document analysis). It will not be part of the output document.
The greatest benefit is that it will allow HTML designers to build more realistic pages, with concrete data, giving a better idea of the possible dynamic page look and feel.
An "Ignored Element" can be inserted anywhere in the template document, even inside "Dynamic Element".
The better is to look at this simple example.