|
|
Welcome to CommonTemplate |
|
1. What's the CommonTemplate?
CommonTemplate is a "template engine",
a generic tool to generate text output: HTML, XML, Mail, Java source code, etc.
2. Which feature of CommonTemplate?
1) Only one syntax rule: $directive{expression}
2) WYSWYG in Dreamweaver and syntax coat is extensible.
3) Microkernel. Everything is additional except core API. Standard directives such as "for" and "if" could be replaced.
4) Better extensibility. The engine is just on the duty of translating the template into directive-tree. The directives will finish the rest of the job by themselves.
3. Where download CommonTemplate?
Download from SourceForge platform:
4. How to write CTL? more...
Only one syntax rule: $directive{expression}
<html>
<body>
$if{users != null && users.size > 0}
<table border="1">
$for{user : users}
<tr>
<td>${for.index + 1}</td>
<td>${user.name}</td>
<td>${user.coins}</td>
</tr>
$end
</table>
$end
</body>
</html>
HTML syntax coat: (WYSWYG)
(1) Comment Syntax Coat
<html>
<body>
<!--$if{users != null && users.size > 0}-->
<table border="1">
<!--$for{user : users}-->
<tr>
<td><!--$out{for.index + 1}-->1<!--$end--></td>
<td><!--$out{user.name}-->james<!--$end--></td>
<td><!--$out{user.coins}-->2.00<!--$end--></td>
</tr>
<!--$end-->
</table>
<!--$end-->
</body>
</html>
(2) Attribute Syntax Coat
<html>
<body>
<table m:if="users != null && users.size > 0" border="1">
<tr m:for="user : users">
<td><span m:out="for.index + 1">james</span></td>
<td><span m:out="user.name">james</span></td>
<td><span m:out="user.coins">2.00</span></td>
</tr>
</table>
</body>
</html>
5 .How to use CommonTemplate API? more...
(1) Java: (>=JRE1.5)
// import commontemplate module
import org.commontemplate.core.*;
import org.commontemplate.engine.*;
import org.commontemplate.standard.*;
import java.util.*;
// config and build factory
StandardConfiguration config = new StandardConfiguration();
config.loadStandardConfiguration();
config.set...
config.add...
...
Factory factory = new Engine(config);
// setting global context
GlobalContext globalContext = factory.getGlobalContext();
globalContext.defineVariable("name", "value");
...
// define data
Map model = ...
Appendable output = ...
Locale locale = ...
TimeZone timeZone = ...
// setting context
Context context = factory.createContext(output, locale, timeZone);
context.defineAllVariables(model);
context.defineVariable("name", "value");
...
// run
Template template = factory.getTemplate("mytemplate.mtl");
template.render(context);
// clean (try finally)
context.clear();
output.flush();
output.close();
(2) .Net: (>=CLR2.0)
......
|
|
|