0
# Template Processing
1
2
Template engine integrations including FreeMarker support and generic template processing utilities for dynamic content generation in the Liferay portal framework.
3
4
## Capabilities
5
6
### FreeMarker Integration
7
8
Comprehensive FreeMarker template engine integration providing dynamic content generation capabilities.
9
10
```java { .api }
11
/**
12
* FreeMarker template engine integration for portal content
13
*/
14
public class FreeMarkerEngine implements TemplateEngine {
15
16
/**
17
* Processes FreeMarker template with provided context
18
* @param templateName template name or path
19
* @param templateContext context variables for template
20
* @return processed template output
21
* @throws TemplateException if template processing fails
22
*/
23
public String processTemplate(String templateName, Map<String, Object> templateContext)
24
throws TemplateException;
25
26
/**
27
* Processes FreeMarker template from string content
28
* @param templateContent template source content
29
* @param templateContext context variables
30
* @return processed template output
31
* @throws TemplateException if processing fails
32
*/
33
public String processTemplateContent(String templateContent, Map<String, Object> templateContext)
34
throws TemplateException;
35
36
/**
37
* Gets FreeMarker configuration
38
* @return Configuration instance
39
*/
40
public Configuration getConfiguration();
41
42
/**
43
* Sets custom FreeMarker configuration
44
* @param configuration FreeMarker configuration
45
*/
46
public void setConfiguration(Configuration configuration);
47
}
48
49
/**
50
* FreeMarker template context manager
51
*/
52
public class FreeMarkerContext {
53
54
/**
55
* Creates template context with portal variables
56
* @param request HTTP servlet request
57
* @param response HTTP servlet response
58
* @return Map containing context variables
59
*/
60
public static Map<String, Object> createContext(HttpServletRequest request,
61
HttpServletResponse response);
62
63
/**
64
* Adds portal-specific variables to context
65
* @param context template context map
66
* @param request HTTP servlet request
67
*/
68
public static void addPortalVariables(Map<String, Object> context,
69
HttpServletRequest request);
70
71
/**
72
* Adds user-specific variables to context
73
* @param context template context map
74
* @param user current user
75
*/
76
public static void addUserVariables(Map<String, Object> context, User user);
77
}
78
```
79
80
### Generic Template Processing
81
82
Generic template processing utilities supporting multiple template engines and formats.
83
84
```java { .api }
85
/**
86
* Generic template manager supporting multiple template engines
87
*/
88
public class TemplateManager {
89
90
/**
91
* Processes template using specified engine
92
* @param templateId template identifier
93
* @param engineType template engine type
94
* @param context template context variables
95
* @return processed template output
96
* @throws TemplateException if processing fails
97
*/
98
public String processTemplate(String templateId, String engineType,
99
Map<String, Object> context) throws TemplateException;
100
101
/**
102
* Registers template engine implementation
103
* @param engineType engine type identifier
104
* @param engine template engine implementation
105
*/
106
public void registerEngine(String engineType, TemplateEngine engine);
107
108
/**
109
* Gets available template engines
110
* @return Set of available engine types
111
*/
112
public Set<String> getAvailableEngines();
113
}
114
115
/**
116
* Template resource manager for loading templates from various sources
117
*/
118
public class TemplateResourceManager {
119
120
/**
121
* Loads template from file system
122
* @param templatePath path to template file
123
* @return template content as string
124
* @throws IOException if template cannot be loaded
125
*/
126
public String loadTemplate(String templatePath) throws IOException;
127
128
/**
129
* Loads template from classpath resource
130
* @param resourcePath classpath resource path
131
* @return template content as string
132
* @throws IOException if resource cannot be loaded
133
*/
134
public String loadTemplateFromClasspath(String resourcePath) throws IOException;
135
136
/**
137
* Loads template from database
138
* @param templateId database template identifier
139
* @return template content as string
140
* @throws SystemException if database access fails
141
*/
142
public String loadTemplateFromDatabase(long templateId) throws SystemException;
143
144
/**
145
* Caches template content for performance
146
* @param templateId template identifier
147
* @param content template content
148
*/
149
public void cacheTemplate(String templateId, String content);
150
}
151
```
152
153
## Usage Examples
154
155
**FreeMarker Template Processing:**
156
157
```java
158
// Create FreeMarker engine
159
FreeMarkerEngine engine = new FreeMarkerEngine();
160
161
// Create template context
162
Map<String, Object> context = FreeMarkerContext.createContext(request, response);
163
context.put("pageTitle", "Welcome to Portal");
164
context.put("userList", userService.getActiveUsers());
165
166
// Process template
167
String output = engine.processTemplate("welcome.ftl", context);
168
169
// Write to response
170
response.getWriter().write(output);
171
```
172
173
**Template Content Processing:**
174
175
```java
176
// Template content as string
177
String templateContent = """
178
<html>
179
<head><title>${pageTitle}</title></head>
180
<body>
181
<h1>Welcome ${user.firstName}!</h1>
182
<p>You have ${messageCount} messages.</p>
183
</body>
184
</html>
185
""";
186
187
// Process template content
188
Map<String, Object> context = new HashMap<>();
189
context.put("pageTitle", "User Dashboard");
190
context.put("user", getCurrentUser());
191
context.put("messageCount", getMessageCount());
192
193
String result = engine.processTemplateContent(templateContent, context);
194
```
195
196
## Integration with Portal Framework
197
198
Template processing integrates with:
199
200
- **Portlet Rendering** - Dynamic portlet content generation
201
- **Email Templates** - Personalized email content
202
- **Web Content** - CMS template processing
203
- **Theme Integration** - Dynamic theme customization
204
- **Workflow Templates** - Business process templates
205
206
## Error Handling
207
208
- **TemplateException** - Template processing errors
209
- **TemplateNotFoundException** - Missing template files
210
- **TemplateParseException** - Template syntax errors
211
- **ContextException** - Template context issues