Skip to content

Commit 19e40e8

Browse files
committed
Improve Templates section (#65)
1 parent e442d89 commit 19e40e8

File tree

1 file changed

+64
-11
lines changed

1 file changed

+64
-11
lines changed

_posts/2015-03-17-templates.md

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ date: 2015-03-17 17:37:21
66
order: 40
77
---
88

9-
Not all applications are REST based and you might need to generate some HTML.
10-
It is not productive to inline the HTML in strings in your code and concatenate them at request time.
9+
Not all applications are `REST` based and you might need to generate some HTML.
10+
It is not productive to inline the `HTML` in strings in your code and concatenate them at request time.
1111
Pippo ships with Freemarker template engine as default and other engines as a builtin alternatives.
12-
These engines are optional and Pippo detects automatically the template engine using __ServiceLocator__.
13-
You can set programmatically the desired template engine using `setTemplateEngine(TemplateEngine templateEngine)` from
14-
__Appplication__.
12+
13+
To use a template engine is optional and Pippo detects automatically the template engine using __ServiceLocator__.
14+
If you want to use a template engine in your application, you must add `pippo-<engine name>` as dependency for your project.
15+
Other option is to set programmatically the desired template engine using `Application#setTemplateEngine(TemplateEngine templateEngine)`,
16+
that in case that you want to create by hand the template engine.
1517

1618
Pippo comes (out of the box) with some template engines:
1719

@@ -32,24 +34,46 @@ To use one of these template engines just add a dependency in your project:
3234
</dependency>
3335
```
3436

35-
If you want to add support for other template engine in your application, please create a new module/project, add file
36-
`ro.pippo.core.TemplateEngine` in _src/main/resources/META-INF/services_ folder with your class name that implements
37-
TemplateEngine as content (for Jade the content file is _ro.pippo.jade.JadeTemplateEngine_).
37+
All templates by default are localized in `/templates` folder (`src/main/resources/templates` for Maven projects).
38+
So, by default the templates are loaded from classpath.
39+
See below all templates from [pippo-demo-basic]({{ site.demourl }}/pippo-demo-basic) project:
40+
41+
```bash
42+
$ tree src/main/resources/templates
43+
src/main/resources/templates
44+
├── files.ftl
45+
└── hello.ftl
46+
```
47+
48+
You can change the location of templates by adding the following line in `conf/application.properties`:
3849

39-
Bellow is a code snippet about how you can use a template as response to a request:
50+
```properties
51+
template.pathPrefix = /
52+
```
53+
54+
Using above snippet we changed the templates location from `/templates` to `/`.
55+
Don't remember that the template will be loaded from classpath as resource.
56+
57+
Below is a code snippet about how you can use a template as response to a request:
4058

4159
```java
4260
GET("/contact/{id}", routeContext -> {
61+
// retrieve some request's parameters values
4362
int id = routeContext.getParameter("id").toInt(0);
4463
String action = routeContext.getParameter("action").toString("new");
4564

65+
// create the template model
4666
Map<String, Object> model = new HashMap<>();
4767
model.put("id", id);
4868
model.put("action", action)
69+
70+
// render the template using data from model
4971
routeContext.render("contact", model);
5072
});
5173
```
5274

75+
In above route, Pippo will find a template with name "contact" and will respond with the result of rendering template by the template engine (a String).
76+
5377
Don't forget that `locals` variables from a response will be available automatically to all templates for the current request/response cycle.
5478
So, maybe the shortest version is:
5579

@@ -61,9 +85,21 @@ GET("/contact/{id}", routeContext -> {
6185
});
6286
```
6387

64-
88+
From the above code snippets you can see that we call the `render` method with the template name as parameter but without extension.
89+
That means that you can change anytime the template file extension without to change the route handler code.
90+
Also, you can change the template engine without to change teh route handler code (for Freemarker template engine you supply a template `contact.ftl` file,
91+
for Pebble template engine you supply a `contact.peb` file, and so on).
92+
93+
Each template engine has a default file extension but you can change it if you want.
94+
For example, `PebbleTemplateEngine` has `peb` as default file extension but you can change it in `html`.
95+
The customization can be applied by adding the following line in `conf/application.properties`:
96+
97+
```properties
98+
template.extension = html
99+
```
100+
65101
For each template engine we expose its configuration. For example __Freemarker__ works with `freemarker.template.Configuration` and __Jade__ works with `de.neuland.jade4j.JadeConfiguration`.
66-
In _Application.onInit_ you can create a new instance for a discovered template engine or you can modify its configuration.
102+
In _Application#onInit_ you can create a new instance for a discovered template engine or you can modify its configuration.
67103

68104
```java
69105
public class CrudApplication extends Application {
@@ -104,4 +140,21 @@ public class CrudApplication extends Application {
104140
}
105141
```
106142

143+
By default, all template engines disable the cache in `dev` and `test` mode (to speed the development - change template and refresh the page in browser)
144+
and enable the cache in `prod` mode (to improve the performance).
145+
146+
Majority of builtin template engine supplied by Pippo, come with useful functions (extensions) that increase the readability of template:
147+
- webjarsAt
148+
- publicAt
149+
- i18n
150+
Read the documentation page allocated to each template engine for more information. The links to individual pages are presented in the start of this page.
151+
152+
If you want to add support for other template engine in your application you must follow below steps:
153+
- create a new module/project
154+
- create a class (ex. MyTemplateEngine) that extends `AbstractTemplateEngine` (or implements `TemplateEngine`)
155+
- mark your template engine class as service using one of the methods below
156+
- automatically via `@MetaInfServices` annotation (mark your template engine class with this annotation)
157+
- manually, add file `ro.pippo.core.TemplateEngine` in _src/main/resources/META-INF/services_ folder with your class name that implements
158+
TemplateEngine as content (for Jade the content file is _ro.pippo.jade.JadeTemplateEngine_).
159+
107160
For more information about how to implement a template engine please see _pippo-freemarker_ and _pippo-jade_ modules.

0 commit comments

Comments
 (0)