Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Improved: MacroFormRenderer refactoring of label, display and text
fields

(OFBIZ-11900)

New RenderableFtl elements to represent pre-rendered FTL strings and FTL
macro calls. RenderableFtl elements are able to render themselves to
strings which are processed as an FTL template by the FtlWriter class.

For labels, display fields and text fields, MacroFormRenderer no longer
generates FTL to write to a template itself, but instead calls
RenderableFtlFormElementsBuilder to create corresponding RenderableFtl
elements which are then processed by FtlWriter. This is a WIP to reduce
complexity in MacroFormRenderer.
  • Loading branch information
danwatford committed Jan 12, 2021
1 parent d32814e commit 3795727
Show file tree
Hide file tree
Showing 21 changed files with 1,865 additions and 545 deletions.
Expand Up @@ -51,16 +51,16 @@ Licensed to the Apache Software Foundation (ASF) under one
import org.apache.ofbiz.widget.renderer.VisualTheme;
import org.apache.ofbiz.widget.renderer.macro.MacroFormRenderer;

import freemarker.template.TemplateException;


/**
* CmsEvents
*/
public class CmsEvents {

private static final String MODULE = CmsEvents.class.getName();

private CmsEvents() {
}

public static String cms(HttpServletRequest request, HttpServletResponse response) {
Delegator delegator = (Delegator) request.getAttribute("delegator");
LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
Expand Down Expand Up @@ -331,9 +331,6 @@ public static String cms(HttpServletRequest request, HttpServletResponse respons
ContentWorker.renderSubContentAsText(dispatcher, contentId, writer, mapKey, templateMap, locale, "text/html", true);
}

} catch (TemplateException e) {
throw new GeneralRuntimeException(String.format(
"Error creating form renderer while rendering content [%s] with path alias [%s]", contentId, pathInfo), e);
} catch (IOException e) {
throw new GeneralRuntimeException(String.format(
"Error in the response writer/output stream while rendering content [%s] with path alias [%s]",
Expand Down
Expand Up @@ -25,6 +25,7 @@
import org.apache.ofbiz.base.util.UtilMisc;
import org.apache.ofbiz.base.util.template.FreeMarkerWorker;
import org.apache.ofbiz.widget.renderer.VisualTheme;
import org.apache.ofbiz.widget.renderer.macro.renderable.RenderableFtl;

import java.io.IOException;
import java.io.Reader;
Expand All @@ -34,6 +35,9 @@
import java.util.Map;
import java.util.WeakHashMap;

/**
* Processes FTL templates and writes result to Appendables.
*/
public final class FtlWriter {
private static final String MODULE = FtlWriter.class.getName();

Expand All @@ -46,17 +50,35 @@ public FtlWriter(final String macroLibraryPath, final VisualTheme visualTheme) t
this.visualTheme = visualTheme;
}

public void executeMacro(Appendable writer, Locale locale, String macro) {
/**
* Process the given RenderableFTL as a template and write the result to the Appendable.
*
* @param writer The Appendable to write the result of the template processing to.
* @param renderableFtl The Renderable FTL to process as a template.
*/
public void processFtl(final Appendable writer, final RenderableFtl renderableFtl) {
processFtlString(writer, null, renderableFtl.toFtlString());
}

/**
* Process the given FTL string as a template and write the result to the Appendable.
*
* @param writer The Appendable to write the result of the template processing to.
* @param ftlString The FTL string to process as a template.
*/
public void processFtlString(Appendable writer, Locale locale, String ftlString) {
try {
Environment environment = getEnvironment(writer, locale);
final Environment environment = getEnvironment(writer, locale);
environment.setVariable("visualTheme", FreeMarkerWorker.autoWrap(visualTheme, environment));
environment.setVariable("modelTheme", FreeMarkerWorker.autoWrap(visualTheme.getModelTheme(), environment));
Reader templateReader = new StringReader(macro);
Template template = new Template(new UID().toString(), templateReader, FreeMarkerWorker.getDefaultOfbizConfig());
environment.setVariable("modelTheme",
FreeMarkerWorker.autoWrap(visualTheme.getModelTheme(), environment));
Reader templateReader = new StringReader(ftlString);
Template template = new Template(new UID().toString(), templateReader,
FreeMarkerWorker.getDefaultOfbizConfig());
templateReader.close();
environment.include(template);
} catch (TemplateException | IOException e) {
Debug.logError(e, "Error rendering screen thru ftl, macro: " + macro, MODULE);
Debug.logError(e, "Error rendering ftl, ftlString: " + ftlString, MODULE);
}
}

Expand Down

0 comments on commit 3795727

Please sign in to comment.