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
51
52
53 public final class AppTemplateService
54 {
55
56 private static String _strTemplateDefaultPath;
57 private static IFreeMarkerTemplateService _freeMarkerTemplateService;
58
59
60
61
62 private AppTemplateService( )
63 {
64 }
65
66
67
68
69
70 public static void init( String strTemplatePath )
71 {
72 _strTemplateDefaultPath = strTemplatePath;
73 getFreeMarkerTemplateService( ).setSharedVariable( "i18n", new I18nTemplateMethod( ) );
74 }
75
76
77
78
79 public static void initAutoIncludes( )
80 {
81
82 Plugin corePlugin = PluginService.getCore( );
83 addPluginMacros( corePlugin );
84
85
86 for ( Plugin plugin : PluginService.getPluginList( ) )
87 {
88 addPluginMacros( plugin );
89 }
90 }
91
92
93
94
95
96
97 private static void addPluginMacros( Plugin plugin )
98 {
99 for ( String strFileName : plugin.getFreeMarkerMacrosFiles( ) )
100 {
101 AppLogService.info( "New freemarker autoinclude : " + strFileName + " from " + plugin.getName( ) );
102 getFreeMarkerTemplateService( ).addPluginMacros( strFileName );
103 }
104 }
105
106
107
108
109 public static void resetCache( )
110 {
111 getFreeMarkerTemplateService( ).resetCache( );
112 }
113
114
115
116
117 public static void resetConfiguration( )
118 {
119 getFreeMarkerTemplateService( ).resetConfiguration( );
120 }
121
122
123
124
125
126
127
128
129 public static HtmlTemplate getTemplate( String strTemplate )
130 {
131 return getTemplate( strTemplate, _strTemplateDefaultPath );
132 }
133
134
135
136
137
138
139
140
141
142
143 public static HtmlTemplate getTemplate( String strTemplate, String strPath )
144 {
145 return getTemplate( strTemplate, strPath, null, null );
146 }
147
148
149
150
151
152
153
154
155
156
157
158
159
160 public static HtmlTemplate getTemplate( String strTemplate, Locale locale )
161 {
162 return getTemplate( strTemplate, _strTemplateDefaultPath, locale, null );
163 }
164
165
166
167
168
169
170
171
172
173
174 public static HtmlTemplate getTemplate( String strTemplate, Locale locale, Object model )
175 {
176 HtmlTemplate template;
177
178
179 template = getTemplate( strTemplate, _strTemplateDefaultPath, locale, model );
180
181 return template;
182 }
183
184
185
186
187
188
189
190
191
192
193
194
195 public static HtmlTemplate getTemplate( String strTemplate, String strPath, Locale locale, Object model )
196 {
197 HtmlTemplate template;
198
199
200 template = loadTemplate( strPath, strTemplate, locale, model );
201
202 return template;
203 }
204
205
206
207
208
209
210
211
212
213
214
215
216
217 @Deprecated
218 public static HtmlTemplate getTemplateFromStringFtl( String strFreemarkerTemplateData, Locale locale, Object model )
219 {
220 HtmlTemplate template;
221
222 template = loadTemplate( strFreemarkerTemplateData, locale, model );
223
224 return template;
225 }
226
227
228
229
230
231
232
233
234
235 private static HtmlTemplate loadTemplate( String strPath, String strTemplate, Locale locale, Object model )
236 {
237 HtmlTemplate template;
238 template = getFreeMarkerTemplateService( ).loadTemplate( strPath, strTemplate, locale, model );
239
240 if ( locale != null )
241 {
242 String strLocalized = I18nService.localize( template.getHtml( ), locale );
243 template = new HtmlTemplate( strLocalized );
244 }
245
246 template = new HtmlTemplate( DatastoreService.replaceKeys( template.getHtml( ) ) );
247
248 return template;
249 }
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264 @Deprecated
265 private static HtmlTemplate loadTemplate( String strTemplateData, Locale locale, Object model )
266 {
267 HtmlTemplate template;
268 template = getFreeMarkerTemplateService( ).loadTemplate( strTemplateData, locale, model );
269
270 if ( locale != null )
271 {
272 String strLocalized = I18nService.localize( template.getHtml( ), locale );
273 template = new HtmlTemplate( strLocalized );
274 }
275
276 return template;
277 }
278
279
280
281
282
283 private static IFreeMarkerTemplateService getFreeMarkerTemplateService( )
284 {
285 if ( _freeMarkerTemplateService == null )
286 {
287 _freeMarkerTemplateService = FreeMarkerTemplateService.getInstance( );
288 _freeMarkerTemplateService.init( _strTemplateDefaultPath );
289 }
290
291 return _freeMarkerTemplateService;
292 }
293 }