2012-02-12 17:07:33 -09:00
|
|
|
picotemplate
|
|
|
|
============
|
|
|
|
|
2012-02-12 17:35:43 -09:00
|
|
|
picotemplate is a simple, tiny C-Template workalike written in java
|
2012-02-12 17:07:33 -09:00
|
|
|
with the intention of being as high performance as possible.
|
2012-02-12 17:35:43 -09:00
|
|
|
picotemplate is very small, only three class files and is BSD licensed.
|
2012-02-12 17:07:33 -09:00
|
|
|
|
|
|
|
basic usage
|
|
|
|
-----------
|
|
|
|
|
|
|
|
First, create your template:
|
2012-02-12 17:35:43 -09:00
|
|
|
|
|
|
|
<html>
|
|
|
|
<body>
|
|
|
|
This is my template. My favorite food is {{FOOD}}.
|
|
|
|
</body>
|
|
|
|
</html>
|
2012-02-12 17:07:33 -09:00
|
|
|
|
|
|
|
|
|
|
|
Import the required classes:
|
2012-02-12 17:35:43 -09:00
|
|
|
|
|
|
|
import com.binarythought.picotemplate.Template;
|
|
|
|
import com.binarythought.picotemplate.TemplateDictionary;
|
2012-02-12 17:07:33 -09:00
|
|
|
|
|
|
|
|
|
|
|
Create your template and template dictionary:
|
2012-02-12 17:35:43 -09:00
|
|
|
|
|
|
|
Template template = new Template(new File("mytemplate.tpl"));
|
|
|
|
TemplateDictionary dict = new TemplateDictionary();
|
2012-02-12 17:07:33 -09:00
|
|
|
|
|
|
|
|
|
|
|
Assign a value to the "food" variable (Unassigned variables are not shown):
|
2012-02-12 17:35:43 -09:00
|
|
|
|
|
|
|
dict.put("food", "cookies");
|
2012-02-12 17:07:33 -09:00
|
|
|
|
|
|
|
|
|
|
|
And parse your template:
|
2012-02-12 17:35:43 -09:00
|
|
|
|
|
|
|
String result = template.parse(dict);
|
2012-02-12 17:07:33 -09:00
|
|
|
|
|
|
|
|
|
|
|
And the result:
|
2012-02-12 17:35:43 -09:00
|
|
|
|
|
|
|
<html>
|
|
|
|
<body>
|
|
|
|
This is my template. My favorite food is cookies.
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
|
2012-02-12 17:07:33 -09:00
|
|
|
|
|
|
|
advanced usage
|
|
|
|
--------------
|
|
|
|
picotemplate can selectively show areas of static content, called "sections".
|
|
|
|
It can also loop over these sections using child dictionaries. Consider the
|
|
|
|
following example:
|
2012-02-12 17:35:43 -09:00
|
|
|
|
|
|
|
<html>
|
|
|
|
<body>
|
|
|
|
{{FAVORITE_SHOW}} is probably my favorite show.
|
|
|
|
{{#GOODSHOWS}}
|
|
|
|
{{SHOW}} is pretty good, too..
|
|
|
|
{{/GOODSHOWS}}
|
|
|
|
</body>
|
|
|
|
</html>
|
2012-02-12 17:07:33 -09:00
|
|
|
|
|
|
|
|
|
|
|
Create your template and template dictionary as usual:
|
2012-02-12 17:35:43 -09:00
|
|
|
|
|
|
|
Template template = new Template(new File("mytemplate.tpl"));
|
|
|
|
TemplateDictionary dict = new TemplateDictionary();
|
2012-02-12 17:07:33 -09:00
|
|
|
|
|
|
|
|
|
|
|
Define our favorite show:
|
2012-02-12 17:35:43 -09:00
|
|
|
|
|
|
|
dict.put("favorite_show", "Happy Days");
|
2012-02-12 17:12:10 -09:00
|
|
|
|
2012-02-12 17:07:33 -09:00
|
|
|
|
|
|
|
Now show the section called "goodshows" (Sections are by default hidden, and
|
|
|
|
must be explicitly told to be shown):
|
2012-02-12 17:35:43 -09:00
|
|
|
|
|
|
|
dict.show("goodshows");
|
2012-02-12 17:12:10 -09:00
|
|
|
|
2012-02-12 17:07:33 -09:00
|
|
|
|
|
|
|
And add some shows for it to loop over:
|
2012-02-12 17:35:43 -09:00
|
|
|
|
|
|
|
TemplateDictionary child1 = dict.createChild("goodshows");
|
|
|
|
child1.put("show", "M.A.S.H");
|
|
|
|
TemplateDictionary child2 = dict.createChild("goodshows");
|
|
|
|
child2.put("show", "A-Team");
|
2012-02-12 17:07:33 -09:00
|
|
|
|
|
|
|
|
|
|
|
And the result:
|
2012-02-12 17:12:10 -09:00
|
|
|
|
2012-02-12 17:35:43 -09:00
|
|
|
<html>
|
|
|
|
<body>
|
|
|
|
Happy Days is probably my favorite show.
|
|
|
|
|
|
|
|
M.A.S.H is pretty good, too..
|
2012-02-12 17:12:10 -09:00
|
|
|
|
2012-02-12 17:35:43 -09:00
|
|
|
A-Team is pretty good, too..
|
|
|
|
</body>
|
|
|
|
</html>
|