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

The following examples show how to use freemarker.core.Environment#getGlobalVariable() . 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: FtlContextFetcher.java    From scipio-erp with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public static MapStack<String> getContextFromEnvironment(Environment env) {
    if (env != null) {
        try {
            TemplateModel model = env.getGlobalVariable("context");
            if (model != null && model instanceof WrapperTemplateModel) {
                Object obj = ((WrapperTemplateModel) model).getWrappedObject();
                if (obj instanceof MapStack) {
                    return (MapStack<String>) obj;
                }
            }
        } catch (Exception e) {
            ;
        }
    }
    return null;
}
 
Example 2
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();
}
 
Example 3
Source File: FrontUtils.java    From Lottery with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 标签中获得站点
 * 
 * @param env
 * @return
 * @throws TemplateModelException
 */
public static CmsSite getSite(Environment env)
		throws TemplateModelException {
	TemplateModel model = env.getGlobalVariable(SITE);
	if (model instanceof AdapterTemplateModel) {
		return (CmsSite) ((AdapterTemplateModel) model)
				.getAdaptedObject(CmsSite.class);
	} else {
		throw new TemplateModelException("'" + SITE
				+ "' not found in DataModel");
	}
}
 
Example 4
Source File: FrontUtils.java    From Lottery with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 标签中获得页码
 * 
 * @param env
 * @return
 * @throws TemplateException
 */
public static int getPageNo(Environment env) throws TemplateException {
	TemplateModel pageNo = env.getGlobalVariable(PAGE_NO);
	if (pageNo instanceof TemplateNumberModel) {
		return ((TemplateNumberModel) pageNo).getAsNumber().intValue();
	} else {
		throw new TemplateModelException("'" + PAGE_NO
				+ "' not found in DataModel.");
	}
}
 
Example 5
Source File: ProcessTimeDirective.java    From Lottery with GNU General Public License v2.0 5 votes vote down vote up
private long getStartTime(Environment env) throws TemplateModelException {
	TemplateModel startTime = env.getGlobalVariable(START_TIME);
	if (startTime == null) {
		log.warn("Variable '{}' not found in GlobalVariable", START_TIME);
		return -1;
	}
	if (startTime instanceof TemplateNumberModel) {
		return ((TemplateNumberModel) startTime).getAsNumber().longValue();
	} else {
		throw new MustNumberException(START_TIME);
	}
}
 
Example 6
Source File: DirectiveUtils.java    From Lottery with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 获得RequestContext
 * 
 * ViewResolver中的exposeSpringMacroHelpers必须为true
 * 
 * @param env
 * @return
 * @throws TemplateException
 */
public static RequestContext getContext(Environment env)
		throws TemplateException {
	TemplateModel ctx = env
			.getGlobalVariable(SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE);
	if (ctx instanceof AdapterTemplateModel) {
		return (RequestContext) ((AdapterTemplateModel) ctx)
				.getAdaptedObject(RequestContext.class);
	} else {
		throw new TemplateModelException("RequestContext '"
				+ SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE
				+ "' not found in DataModel.");
	}
}
 
Example 7
Source File: LangFtlUtil.java    From scipio-erp with Apache License 2.0 3 votes vote down vote up
/**
 * Gets a var from main namespace with fallback on globals/data-model, or null if doesn't exit or null.
 * <p>
 * Avoids local variables and emulates a simple Freemarker var read in the main namespace.
 * <p>
 * Similar to {@link freemarker.core.Environment#getVariable(String)} but skips local
 * variables and always main namespace instead of current namespace.
 * <p>
 * NOTE: This probably makes the most sense to call from transforms as a means to read
 * "context/global" variables (using term loosely, while providing possibility for
 * templates to override using both #assign and #global directives.
 */
public static TemplateModel getMainNsOrGlobalVar(String name, Environment env) throws TemplateModelException {
    TemplateModel result = env.getMainNamespace().get(name);
    if (result == null) {
        result = env.getGlobalVariable(name);
    }
    return result;
}
 
Example 8
Source File: LangFtlUtil.java    From scipio-erp with Apache License 2.0 3 votes vote down vote up
/**
 * Gets a var from current namespace with fallback on globals/data-model, or null if doesn't exit or null.
 * <p>
 * Avoids local variables and emulates a simple Freemarker var read in the current namespace.
 * <p>
 * Similar to {@link freemarker.core.Environment#getVariable(String)} but skips local
 * variables.
 */
public static TemplateModel getCurrentNsOrGlobalVar(String name, Environment env) throws TemplateModelException {
    TemplateModel result = env.getCurrentNamespace().get(name);
    if (result == null) {
        result = env.getGlobalVariable(name);
    }
    return result;
}
 
Example 9
Source File: TransformUtil.java    From scipio-erp with Apache License 2.0 2 votes vote down vote up
/**
 * Abstracted method to retrieve a "context/global" var (loosely-defined) from the Freemarker environment.
 * At minimum, always include Freemarker globals and data model, encompassing Ofbiz context and globalContext.
 * In addition - SUBJECT TO CHANGE - may read from current or main namespace.
 * <p>
 * NOTE: 2016-10-13: Currently this only reads from globals and data model, ignoring main
 * and current namespaces. So it will only respond to changes made using #global directive and not #assign.
 * TODO: REVIEW: We will start with this more restrictive/safer behavior first and see in future if main
 * or current namespace should be considered. This should be made to match the FTL macro implementations.
 * Some of the more common variable names (such as "locale") can cause problematic conflicts.
 *
 * @see freemarker.core.Environment#getGlobalVariable(String)
 * @see com.ilscipio.scipio.ce.webapp.ftl.lang.LangFtlUtil#getMainNsOrGlobalVar(String, Environment)
 * @see com.ilscipio.scipio.ce.webapp.ftl.lang.LangFtlUtil#getCurrentNsOrGlobalVar(String, Environment)
 */
public static TemplateModel getFtlContextGlobalVar(String name, Environment env) throws TemplateModelException {
    //return LangFtlUtil.getMainNsOrGlobalVar(name, env);
    return env.getGlobalVariable(name);
}
 
Example 10
Source File: ContextFtlUtil.java    From scipio-erp with Apache License 2.0 2 votes vote down vote up
/**
 * Attempts to get the current "user" or "screen" locale normally found in screen context
 * as the simple "locale" variable.
 * TODO: REVIEW: this is currently using getGlobalVariable as most likely the fastest that
 * will avoid problems from macros - unclear if more or less reliable than trying to read
 * out of "context" map (which not guaranteed present).
 * <p>
 * NOTE: In most transforms, the preferred method is: {@link #getCurrentLocale(Environment)}.
 */
public static Locale getContextLocale(Environment env) throws TemplateModelException {
    WrapperTemplateModel model = (WrapperTemplateModel) env.getGlobalVariable("locale");
    return (model != null) ? (Locale) ((WrapperTemplateModel) model).getWrappedObject() : null;
}