1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.portal.service.template;
35
36 import fr.paris.lutece.portal.service.datastore.DatastoreService;
37 import fr.paris.lutece.portal.service.i18n.I18nService;
38 import fr.paris.lutece.portal.service.i18n.I18nTemplateMethod;
39 import fr.paris.lutece.portal.service.plugin.Plugin;
40 import fr.paris.lutece.portal.service.plugin.PluginService;
41 import fr.paris.lutece.portal.service.util.AppLogService;
42 import fr.paris.lutece.util.html.HtmlTemplate;
43
44 import java.util.Locale;
45
46
47
48
49
50 public final class AppTemplateService
51 {
52
53 private static String _strTemplateDefaultPath;
54 private static IFreeMarkerTemplateService _freeMarkerTemplateService;
55
56
57
58
59 private AppTemplateService( )
60 {
61 }
62
63
64
65
66
67
68
69 public static void init( String strTemplatePath )
70 {
71 _strTemplateDefaultPath = strTemplatePath;
72 getFreeMarkerTemplateService( ).setSharedVariable( "i18n", new I18nTemplateMethod( ) );
73 }
74
75
76
77
78 public static void initAutoIncludes( )
79 {
80
81 Plugin corePlugin = PluginService.getCore( );
82 addPluginMacros( corePlugin );
83
84
85 for ( Plugin plugin : PluginService.getPluginList( ) )
86 {
87 addPluginMacros( plugin );
88 }
89
90
91 CommonsService.activateCommons( CommonsService.getCurrentCommonsKey( ) );
92 }
93
94
95
96
97
98
99
100 private static void addPluginMacros( Plugin plugin )
101 {
102 for ( String strFileName : plugin.getFreeMarkerMacrosFiles( ) )
103 {
104 AppLogService.info( "New freemarker autoinclude : {} from {}", strFileName, plugin.getName( ) );
105 getFreeMarkerTemplateService( ).addPluginMacros( strFileName );
106 }
107 }
108
109
110
111
112 public static void resetCache( )
113 {
114 getFreeMarkerTemplateService( ).resetCache( );
115 }
116
117
118
119
120 public static void resetConfiguration( )
121 {
122 getFreeMarkerTemplateService( ).resetConfiguration( );
123 }
124
125
126
127
128
129
130
131
132 public static HtmlTemplate getTemplate( String strTemplate )
133 {
134 return getTemplate( strTemplate, _strTemplateDefaultPath );
135 }
136
137
138
139
140
141
142
143
144
145
146
147 public static HtmlTemplate getTemplate( String strTemplate, String strPath )
148 {
149 return getTemplate( strTemplate, strPath, null, null );
150 }
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165 public static HtmlTemplate getTemplate( String strTemplate, Locale locale )
166 {
167 return getTemplate( strTemplate, _strTemplateDefaultPath, locale, null );
168 }
169
170
171
172
173
174
175
176
177
178
179
180
181
182 public static HtmlTemplate getTemplate( String strTemplate, Locale locale, Object model )
183 {
184 HtmlTemplate template;
185
186
187 template = getTemplate( strTemplate, _strTemplateDefaultPath, locale, model );
188
189 return template;
190 }
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206 public static HtmlTemplate getTemplate( String strTemplate, String strPath, Locale locale, Object model )
207 {
208 HtmlTemplate template;
209
210
211 template = loadTemplate( strPath, strTemplate, locale, model );
212
213 return template;
214 }
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229 public static HtmlTemplate getTemplateFromStringFtl( String strFreemarkerTemplateData, Locale locale, Object model )
230 {
231 HtmlTemplate template = getFreeMarkerTemplateService( ).loadTemplateFromStringFtl( strFreemarkerTemplateData, locale, model );
232
233 if ( locale != null )
234 {
235 String strLocalized = I18nService.localize( template.getHtml( ), locale );
236 template = new HtmlTemplate( strLocalized );
237 }
238 return template;
239 }
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256 public static HtmlTemplate getTemplateFromStringFtl( String strFreemarkerTemplateName,String strFreemarkerTemplateData, Locale locale, Object model,boolean bResetCache )
257 {
258 HtmlTemplate template = getFreeMarkerTemplateService( ).loadTemplateFromStringFtl( strFreemarkerTemplateName,strFreemarkerTemplateData, locale, model,bResetCache );
259
260 if ( locale != null )
261 {
262 String strLocalized = I18nService.localize( template.getHtml( ), locale );
263 template = new HtmlTemplate( strLocalized );
264 }
265 return template;
266 }
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281 private static HtmlTemplate loadTemplate( String strPath, String strTemplate, Locale locale, Object model )
282 {
283 HtmlTemplate template;
284 template = getFreeMarkerTemplateService( ).loadTemplate( strPath, strTemplate, locale, model );
285
286 if ( locale != null )
287 {
288 String strLocalized = I18nService.localize( template.getHtml( ), locale );
289 template = new HtmlTemplate( strLocalized );
290 }
291
292 template = new HtmlTemplate( DatastoreService.replaceKeys( template.getHtml( ) ) );
293
294 return template;
295 }
296
297
298
299
300
301
302 private static IFreeMarkerTemplateService getFreeMarkerTemplateService( )
303 {
304 if ( _freeMarkerTemplateService == null )
305 {
306 _freeMarkerTemplateService = FreeMarkerTemplateService.getInstance( );
307 _freeMarkerTemplateService.init( _strTemplateDefaultPath );
308 }
309
310 return _freeMarkerTemplateService;
311 }
312 }