Tags: mixer2 java TemplateEngine

Mixer2 is the xhtml dedicated template engine. Because Mixer2 use JAXB (Java Architecture for XML Binding) internally, the usage is not only as a template engine, but also, Mixer2 can be used in another way.

Mixer2 has some different functionality.

  1. as a alternative to JSP, Velocity, and other template engine technology
  2. as a XHTML parser
  3. as as XHTML tag snippet generator

In thie post, I introduce "XHTML snippet generator" functionality of Mixer2.

You must fix up your html template as "full html" like "<html>...</html>" in order to use mixer2Engine.loadHtmlTemplate() .

But if you need to output xhtml tag using mixer2Engine.saveToString(), you can get any tag snippet as string. So, you can create some helper class like custom tag of JSP.

For example, If you must use jsp as the base template but also you want to use Mixer2, You can use them combine them.

If you create the new tag instance, use "new" the tag classes. Also you can use TagCreator.

Div div = TagCreator.div();
	Span span = TagCreator.span();
	span.getContent().add("Hello World !");
	span.setStyle("color:red;");
	div.getContent().add(span);
	String result = mixer2Engine.saveToString(div);
	System.out.println(result);

output:

Hello World !

If you create table tag, you can use TableBuilder.

String[][] data = new String[][]{
    // id, name, price
    {"1", "cookie", "2.50"},
    {"2", "candy",  "3.75"}
};

TableBuilder tBuilder = new TableBuilder();
for (String[] row : data) {
    tBuilder
        .addTr()
        .addTd(row[0])
        .addTd(row[1])
        .addTd(row[2]);
}
Table table = tBuilder.build();
String str = mixer2Engine.saveToString(table);
System.out.println(str);

output:

<table>
	<tbody>
		<tr>
			<td>1</td>
			<td>cookie</td>
			<td>2.50</td>
		</tr>
		<tr>
			<td>2</td>
			<td>candy</td>
			<td>3.75</td>
		</tr>
	</tbody>
	</table>

For more detail of TableBuilder, see Mixer2 official site.

How about it ? Mixer2 and other template technology can exist together. Try Mixer2 !