cdramey
/
picotemplate
Archived
1
0
Fork 0
a tiny c-template work-alike for java
This repository has been archived on 2021-02-02. You can view files and clone it, but cannot push or open issues or pull requests.
Go to file
Christopher Ramey 422b727686 Finally fixed markdown 2012-02-13 02:35:43 +00:00
res Merge from existing repo 2012-02-13 02:07:33 +00:00
src/com/binarythought/picotemplate Merge from existing repo 2012-02-13 02:07:33 +00:00
.gitignore Merge from existing repo 2012-02-13 02:07:33 +00:00
LICENSE Merge from existing repo 2012-02-13 02:07:33 +00:00
README Finally fixed markdown 2012-02-13 02:35:43 +00:00
README.md Finally fixed markdown 2012-02-13 02:35:43 +00:00
build.xml Merge from existing repo 2012-02-13 02:07:33 +00:00

README.md

picotemplate

picotemplate is a simple, tiny C-Template workalike written in java with the intention of being as high performance as possible. picotemplate is very small, only three class files and is BSD licensed.

basic usage

First, create your template:

<html>
  <body>
    This is my template. My favorite food is {{FOOD}}.
  </body>
</html>

Import the required classes:

import com.binarythought.picotemplate.Template;
import com.binarythought.picotemplate.TemplateDictionary;

Create your template and template dictionary:

Template template = new Template(new File("mytemplate.tpl"));
TemplateDictionary dict = new TemplateDictionary();

Assign a value to the "food" variable (Unassigned variables are not shown):

dict.put("food", "cookies");

And parse your template:

String result = template.parse(dict);

And the result:

<html>
  <body>
    This is my template. My favorite food is cookies.
  </body>
</html>

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:

<html>
  <body>
    {{FAVORITE_SHOW}} is probably my favorite show.
    {{#GOODSHOWS}}
    {{SHOW}} is pretty good, too..
    {{/GOODSHOWS}}
  </body>
</html>

Create your template and template dictionary as usual:

Template template = new Template(new File("mytemplate.tpl"));
TemplateDictionary dict = new TemplateDictionary();

Define our favorite show:

dict.put("favorite_show", "Happy Days");

Now show the section called "goodshows" (Sections are by default hidden, and must be explicitly told to be shown):

dict.show("goodshows");

And add some shows for it to loop over:

TemplateDictionary child1 = dict.createChild("goodshows");
child1.put("show", "M.A.S.H");
TemplateDictionary child2 = dict.createChild("goodshows");
child2.put("show", "A-Team");

And the result:

<html>
  <body>
    Happy Days is probably my favorite show.

    M.A.S.H is pretty good, too..

    A-Team is pretty good, too..
  </body>
</html>