Java Code Examples for freemarker.core.Environment#setGlobalVariable()

The following examples show how to use freemarker.core.Environment#setGlobalVariable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ContextFtlUtil.java    From scipio-erp with Apache License 2.0 6 votes vote down vote up
/**
 * Wipes all request vars and sets a new holder map.
 * <p>
 * Attempts to set the same map for as many contexts present as possible, for compatibility.
 * <p>
 * <em>NOTE:</em> in some cases this call is not necessary, but it's a good idea anyway
 * because it will set the same map for request and context (but not ftl - FIXME?),
 * which might prevent problems in odd rendering cases.
 * This should be called at beginning of rendering at a point where as many of the parameters
 * are non-null as possible (but env will probably usually be null).
 */
public static void resetRequestVars(HttpServletRequest request,
        Map<String, Object> context, Environment env) throws TemplateModelException {
    RequestVarMapWrapper mapWrapper = new RequestVarMapWrapper();
    if (request != null) {
        request.setAttribute(ContextFtlUtil.REQUEST_VAR_MAP_NAME_REQATTRIBS, mapWrapper);
    }
    Map<String, Object> globalContext = getGlobalContext(context, env);
    if (globalContext != null) {
        globalContext.put(ContextFtlUtil.REQUEST_VAR_MAP_NAME_GLOBALCONTEXT, mapWrapper);
    }
    if (env != null) {
        // Here we "hide" our variables in freemarker globals
        env.setGlobalVariable(ContextFtlUtil.REQUEST_VAR_MAP_NAME_FTLGLOBALS, new RequestVarMapWrapperModel(mapWrapper.getRawMap()));
    }
}
 
Example 2
Source File: MacroFormRenderer.java    From scipio-erp with Apache License 2.0 6 votes vote down vote up
/**
 * SCIPIO: appends special page scripts to macro call.
 * New 2017-04-21.
 */
private void appendFormPageScripts(Appendable writer, Appendable sr, Map<String, Object> context, ModelForm modelForm) throws IOException {
    // SCIPIO: 2017-04-21: special pageScripts
    List<Object> pageScripts = new ArrayList<>();
    for(ModelPageScript pageScript : modelForm.getPageScripts()) {
        pageScripts.add(pageScript.getScript(context));
    }
    Environment env;
    try {
        env = getEnvironment(writer);
        freemarker.template.TemplateModel pageScriptsModel = env.getObjectWrapper().wrap(pageScripts);
        env.setGlobalVariable("_scpFormRenPageScripts", pageScriptsModel);
    } catch (TemplateException e) {
        throw new IOException(e);
    }
    sr.append(" pageScripts=_scpFormRenPageScripts");
}
 
Example 3
Source File: ContextFtlUtil.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
/**
 * Removes the whole request vars map.
 */
public static void removeRequestVars(HttpServletRequest request,
        Map<String, Object> context, Environment env) throws TemplateModelException {
    if (request != null) {
        request.removeAttribute(ContextFtlUtil.REQUEST_VAR_MAP_NAME_REQATTRIBS);
    }
    Map<String, Object> globalContext = getGlobalContext(context, env);
    if (globalContext != null) {
        globalContext.remove(ContextFtlUtil.REQUEST_VAR_MAP_NAME_GLOBALCONTEXT);
    }
    if (env != null) {
        env.setGlobalVariable(ContextFtlUtil.REQUEST_VAR_MAP_NAME_FTLGLOBALS, null);
    }
}
 
Example 4
Source File: ContextFtlUtil.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
private static Map<String, Object> getRequestVarMapFromFtlGlobals(Environment env) {
    RequestVarMapWrapperModel mapWrapper = null;
    try {
        mapWrapper = (RequestVarMapWrapperModel) env.getGlobalVariable(ContextFtlUtil.REQUEST_VAR_MAP_NAME_FTLGLOBALS);
    } catch (TemplateModelException e) {
        Debug.logError(e, "Scipio: Error getting request var map from FTL globals", module);
    }
    if (mapWrapper == null) {
        // FIXME: should try to get underlying map from request or globalContext
        mapWrapper = new RequestVarMapWrapperModel();
        env.setGlobalVariable(ContextFtlUtil.REQUEST_VAR_MAP_NAME_FTLGLOBALS, mapWrapper);
    }
    return mapWrapper.getRawMap();
}