You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by by...@apache.org on 2007/09/18 10:07:29 UTC

svn commit: r576766 - /ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/template/FreeMarkerWorker.java

Author: byersa
Date: Tue Sep 18 01:07:29 2007
New Revision: 576766

URL: http://svn.apache.org/viewvc?rev=576766&view=rev
Log:
RenderTemplate was not working for .ftl files that are used as templates in CMS.
The "templateLocation" value in those cases is something like "DATARESOURCE:<id>"
and that causes makeReader(...) to fail.

renderTemplateFromString(...) still checks to see if a cached template exists, but
if it doesn't and since it has the template string, it builds the template from the
"templateString" and puts it in cashe.

Modified:
    ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/template/FreeMarkerWorker.java

Modified: ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/template/FreeMarkerWorker.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/template/FreeMarkerWorker.java?rev=576766&r1=576765&r2=576766&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/template/FreeMarkerWorker.java (original)
+++ ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/template/FreeMarkerWorker.java Tue Sep 18 01:07:29 2007
@@ -140,7 +140,11 @@
      * @param outWriter The Writer to render to
      */
     public static void renderTemplate(String templateLocation, String templateString, Map context, Writer outWriter) throws TemplateException, IOException {
-        renderTemplate(templateLocation, context, outWriter);
+        if (UtilValidate.isEmpty(templateString)) {
+            renderTemplate(templateLocation, context, outWriter);
+        } else {
+            renderTemplateFromString(templateString, templateLocation, context, outWriter);
+        }
     }
     
     /**
@@ -152,6 +156,23 @@
      */
     public static void renderTemplate(String templateLocation, Map context, Writer outWriter) throws TemplateException, IOException {
         Template template = getTemplate(templateLocation);
+        renderTemplate(template, context, outWriter);
+    }
+ 
+    public static void renderTemplateFromString(String templateString, String templateLocation, Map context, Writer outWriter) throws TemplateException, IOException {
+        Template template = (Template) cachedTemplates.get(templateLocation);
+        if (template == null) {
+            synchronized (cachedTemplates) {
+                template = (Template) cachedTemplates.get(templateLocation);
+            }
+        }
+        
+        if (template == null) {
+            Reader templateReader  =new StringReader(templateString);
+            template = new Template(templateLocation, templateReader, getDefaultOfbizConfig());
+            templateReader.close();
+            cachedTemplates.put(templateLocation, template);
+        }
         renderTemplate(template, context, outWriter);
     }