Search in sources :

Example 1 with Context

use of freemarker.core.Macro.Context in project freemarker by apache.

the class BuiltinVariable method _eval.

@Override
TemplateModel _eval(Environment env) throws TemplateException {
    if (parseTimeValue != null) {
        return parseTimeValue;
    }
    if (name == NAMESPACE) {
        return env.getCurrentNamespace();
    }
    if (name == MAIN) {
        return env.getMainNamespace();
    }
    if (name == GLOBALS) {
        return env.getGlobalVariables();
    }
    if (name == LOCALS) {
        Macro.Context ctx = env.getCurrentMacroContext();
        return ctx == null ? null : ctx.getLocals();
    }
    if (name == DATA_MODEL || name == DATA_MODEL_CC) {
        return env.getDataModel();
    }
    if (name == VARS) {
        return new VarsHash(env);
    }
    if (name == LOCALE) {
        return new SimpleScalar(env.getLocale().toString());
    }
    if (name == LOCALE_OBJECT || name == LOCALE_OBJECT_CC) {
        return env.getObjectWrapper().wrap(env.getLocale());
    }
    if (name == LANG) {
        return new SimpleScalar(env.getLocale().getLanguage());
    }
    if (name == CURRENT_NODE || name == NODE || name == CURRENT_NODE_CC) {
        return env.getCurrentVisitorNode();
    }
    if (name == TEMPLATE_NAME || name == TEMPLATE_NAME_CC) {
        // remains broken.
        return (env.getConfiguration().getIncompatibleImprovements().intValue() >= _TemplateAPI.VERSION_INT_2_3_23) ? new SimpleScalar(env.getTemplate230().getName()) : new SimpleScalar(env.getTemplate().getName());
    }
    if (name == MAIN_TEMPLATE_NAME || name == MAIN_TEMPLATE_NAME_CC) {
        return SimpleScalar.newInstanceOrNull(env.getMainTemplate().getName());
    }
    if (name == CURRENT_TEMPLATE_NAME || name == CURRENT_TEMPLATE_NAME_CC) {
        return SimpleScalar.newInstanceOrNull(env.getCurrentTemplate().getName());
    }
    if (name == PASS) {
        return Macro.DO_NOTHING_MACRO;
    }
    if (name == OUTPUT_ENCODING || name == OUTPUT_ENCODING_CC) {
        String s = env.getOutputEncoding();
        return SimpleScalar.newInstanceOrNull(s);
    }
    if (name == URL_ESCAPING_CHARSET || name == URL_ESCAPING_CHARSET_CC) {
        String s = env.getURLEscapingCharset();
        return SimpleScalar.newInstanceOrNull(s);
    }
    if (name == ERROR) {
        return new SimpleScalar(env.getCurrentRecoveredErrorMessage());
    }
    if (name == NOW) {
        return new SimpleDate(new Date(), TemplateDateModel.DATETIME);
    }
    if (name == VERSION) {
        return new SimpleScalar(Configuration.getVersionNumber());
    }
    if (name == INCOMPATIBLE_IMPROVEMENTS || name == INCOMPATIBLE_IMPROVEMENTS_CC) {
        return new SimpleScalar(env.getConfiguration().getIncompatibleImprovements().toString());
    }
    if (name == GET_OPTIONAL_TEMPLATE) {
        return GetOptionalTemplateMethod.INSTANCE;
    }
    if (name == GET_OPTIONAL_TEMPLATE_CC) {
        return GetOptionalTemplateMethod.INSTANCE_CC;
    }
    if (name == CALLER_TEMPLATE_NAME || name == CALLER_TEMPLATE_NAME_CC) {
        Context ctx = env.getCurrentMacroContext();
        if (ctx == null) {
            throw new TemplateException("Can't get ." + name + " here, as there's no macro or function (that's " + "implemented in the template) call in context.", env);
        }
        TemplateObject callPlace = ctx.callPlace;
        String name = callPlace != null ? callPlace.getTemplate().getName() : null;
        return name != null ? new SimpleScalar(name) : TemplateScalarModel.EMPTY_STRING;
    }
    throw new _MiscTemplateException(this, "Invalid special variable: ", name);
}
Also used : Context(freemarker.core.Macro.Context) TemplateException(freemarker.template.TemplateException) SimpleDate(freemarker.template.SimpleDate) Context(freemarker.core.Macro.Context) SimpleScalar(freemarker.template.SimpleScalar) SimpleDate(freemarker.template.SimpleDate) Date(java.util.Date)

Aggregations

Context (freemarker.core.Macro.Context)1 SimpleDate (freemarker.template.SimpleDate)1 SimpleScalar (freemarker.template.SimpleScalar)1 TemplateException (freemarker.template.TemplateException)1 Date (java.util.Date)1