a tiny c-template work-alike for java
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

112 lines
2.2 KiB

  1. picotemplate
  2. ============
  3. `picotemplate` is a simple, tiny C-Template workalike written in java
  4. with the intention of being as high performance as possible.
  5. `picotemplate` is very small, only three class files and is BSD licensed.
  6. basic usage
  7. -----------
  8. First, create your template:
  9. ```html
  10. <html>
  11. <body>
  12. This is my template. My favorite food is {{FOOD}}.
  13. </body>
  14. </html>
  15. ```
  16. Import the required classes:
  17. ```java
  18. import com.binarythought.picotemplate.Template;
  19. import com.binarythought.picotemplate.TemplateDictionary;
  20. ```
  21. Create your template and template dictionary:
  22. ```java
  23. Template template = new Template(new File("mytemplate.tpl"));
  24. TemplateDictionary dict = new TemplateDictionary();
  25. ```
  26. Assign a value to the "food" variable (Unassigned variables are not shown):
  27. ```java
  28. dict.put("food", "cookies");
  29. ```
  30. And parse your template:
  31. ```java
  32. String result = template.parse(dict);
  33. ```
  34. And the result:
  35. ```html
  36. <html>
  37. <body>
  38. This is my template. My favorite food is cookies.
  39. </body>
  40. </html>
  41. ```
  42. advanced usage
  43. --------------
  44. picotemplate can selectively show areas of static content, called "sections".
  45. It can also loop over these sections using child dictionaries. Consider the
  46. following example:
  47. ```html
  48. <html>
  49. <body>
  50. {{FAVORITE_SHOW}} is probably my favorite show.
  51. {{#GOODSHOWS}}
  52. {{SHOW}} is pretty good, too..
  53. {{/GOODSHOWS}}
  54. </body>
  55. </html>
  56. ```
  57. Create your template and template dictionary as usual:
  58. ```java
  59. Template template = new Template(new File("mytemplate.tpl"));
  60. TemplateDictionary dict = new TemplateDictionary();
  61. ```
  62. Define our favorite show:
  63. ```java
  64. dict.put("favorite_show", "Happy Days");
  65. ```
  66. Now show the section called "goodshows" (Sections are by default hidden, and
  67. must be explicitly told to be shown):
  68. ```java
  69. dict.show("goodshows");
  70. ```
  71. And add some shows for it to loop over:
  72. ```java
  73. TemplateDictionary child1 = dict.createChild("goodshows");
  74. child1.put("show", "M.A.S.H");
  75. TemplateDictionary child2 = dict.createChild("goodshows");
  76. child2.put("show", "A-Team");
  77. ```
  78. And the result:
  79. ```html
  80. <html>
  81. <body>
  82. Happy Days is probably my favorite show.
  83. M.A.S.H is pretty good, too..
  84. A-Team is pretty good, too..
  85. </body>
  86. </html>
  87. ```