2 changed files with 56 additions and 66 deletions
@ -0,0 +1,2 @@ |
|||||
|
Picotemplate is a BSD-licensed C-Template workalike written in Java. |
||||
|
For usage, please see README.md |
@ -1,114 +1,102 @@ |
|||||
picotemplate |
picotemplate |
||||
============ |
============ |
||||
|
|
||||
`picotemplate` is a simple, tiny C-Template workalike written in java |
|
||||
|
picotemplate is a simple, tiny C-Template workalike written in java |
||||
with the intention of being as high performance as possible. |
with the intention of being as high performance as possible. |
||||
`picotemplate` is very small, only three class files and is BSD licensed. |
|
||||
|
picotemplate is very small, only three class files and is BSD licensed. |
||||
|
|
||||
basic usage |
basic usage |
||||
----------- |
----------- |
||||
|
|
||||
First, create your template: |
First, create your template: |
||||
<code><pre> |
|
||||
<html> |
|
||||
<body> |
|
||||
This is my template. My favorite food is {{FOOD}}. |
|
||||
</body> |
|
||||
</html> |
|
||||
</pre></code> |
|
||||
|
|
||||
|
<html> |
||||
|
<body> |
||||
|
This is my template. My favorite food is {{FOOD}}. |
||||
|
</body> |
||||
|
</html> |
||||
|
|
||||
|
|
||||
Import the required classes: |
Import the required classes: |
||||
<code><pre> |
|
||||
import com.binarythought.picotemplate.Template; |
|
||||
import com.binarythought.picotemplate.TemplateDictionary; |
|
||||
</pre></code> |
|
||||
|
|
||||
|
import com.binarythought.picotemplate.Template; |
||||
|
import com.binarythought.picotemplate.TemplateDictionary; |
||||
|
|
||||
|
|
||||
Create your template and template dictionary: |
Create your template and template dictionary: |
||||
<code><pre> |
|
||||
Template template = new Template(new File("mytemplate.tpl")); |
|
||||
TemplateDictionary dict = new TemplateDictionary(); |
|
||||
</pre></code> |
|
||||
|
|
||||
|
Template template = new Template(new File("mytemplate.tpl")); |
||||
|
TemplateDictionary dict = new TemplateDictionary(); |
||||
|
|
||||
|
|
||||
Assign a value to the "food" variable (Unassigned variables are not shown): |
Assign a value to the "food" variable (Unassigned variables are not shown): |
||||
<code><pre> |
|
||||
dict.put("food", "cookies"); |
|
||||
</pre></code> |
|
||||
|
|
||||
|
dict.put("food", "cookies"); |
||||
|
|
||||
|
|
||||
And parse your template: |
And parse your template: |
||||
<code><pre> |
|
||||
String result = template.parse(dict); |
|
||||
</pre></code> |
|
||||
|
|
||||
|
String result = template.parse(dict); |
||||
|
|
||||
|
|
||||
And the result: |
And the result: |
||||
<code><pre> |
|
||||
<html> |
|
||||
<body> |
|
||||
This is my template. My favorite food is cookies. |
|
||||
</body> |
|
||||
</html> |
|
||||
</pre></code> |
|
||||
|
|
||||
|
<html> |
||||
|
<body> |
||||
|
This is my template. My favorite food is cookies. |
||||
|
</body> |
||||
|
</html> |
||||
|
|
||||
|
|
||||
advanced usage |
advanced usage |
||||
-------------- |
-------------- |
||||
|
|
||||
picotemplate can selectively show areas of static content, called "sections". |
picotemplate can selectively show areas of static content, called "sections". |
||||
It can also loop over these sections using child dictionaries. Consider the |
It can also loop over these sections using child dictionaries. Consider the |
||||
following example: |
following example: |
||||
<code><pre> |
|
||||
<html> |
|
||||
<body> |
|
||||
{{FAVORITE_SHOW}} is probably my favorite show. |
|
||||
{{#GOODSHOWS}} |
|
||||
{{SHOW}} is pretty good, too.. |
|
||||
{{/GOODSHOWS}} |
|
||||
</body> |
|
||||
</html> |
|
||||
</pre></code> |
|
||||
|
|
||||
|
<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: |
Create your template and template dictionary as usual: |
||||
<code><pre> |
|
||||
Template template = new Template(new File("mytemplate.tpl")); |
|
||||
TemplateDictionary dict = new TemplateDictionary(); |
|
||||
</pre></code> |
|
||||
|
|
||||
|
Template template = new Template(new File("mytemplate.tpl")); |
||||
|
TemplateDictionary dict = new TemplateDictionary(); |
||||
|
|
||||
|
|
||||
Define our favorite show: |
Define our favorite show: |
||||
<code><pre> |
|
||||
dict.put("favorite_show", "Happy Days"); |
|
||||
</pre></code> |
|
||||
|
|
||||
|
dict.put("favorite_show", "Happy Days"); |
||||
|
|
||||
|
|
||||
Now show the section called "goodshows" (Sections are by default hidden, and |
Now show the section called "goodshows" (Sections are by default hidden, and |
||||
must be explicitly told to be shown): |
must be explicitly told to be shown): |
||||
<code><pre> |
|
||||
dict.show("goodshows"); |
|
||||
</pre></code> |
|
||||
|
|
||||
|
dict.show("goodshows"); |
||||
|
|
||||
|
|
||||
And add some shows for it to loop over: |
And add some shows for it to loop over: |
||||
<code><pre> |
|
||||
TemplateDictionary child1 = dict.createChild("goodshows"); |
|
||||
child1.put("show", "M.A.S.H"); |
|
||||
TemplateDictionary child2 = dict.createChild("goodshows"); |
|
||||
child2.put("show", "A-Team"); |
|
||||
</pre></code> |
|
||||
|
|
||||
|
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: |
And the result: |
||||
<code><pre> |
|
||||
<html> |
|
||||
<body> |
|
||||
Happy Days is probably my favorite show. |
|
||||
|
|
||||
M.A.S.H is pretty good, too.. |
|
||||
|
<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> |
|
||||
</pre></code> |
|
||||
|
A-Team is pretty good, too.. |
||||
|
</body> |
||||
|
</html> |
Reference in new issue