freemarker-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Albert Kam <moonblade.w...@gmail.com>
Subject How to filter TemplateModel by 'type' and how it is put into namespace ?
Date Mon, 23 May 2016 13:44:31 GMT
Hi,

I'm trying to reuse some variables in the current namespace for the
next environment.

But i want only reuse only the variables assigned using the <#assign>
directive, whose value can be scalar, hash, or anything else that is
not a macro or function.

    Template template = cfg.getTemplate("lab.ftl");

    Map<String, Object> data = new HashMap<String, Object>();

    StringWriter result = new StringWriter();
    Environment env = template.createProcessingEnvironment(data, result);
    Namespace currentNamespace = env.getCurrentNamespace();
    env.process();

    System.out.println(result.toString());

    Map<String, TemplateModel> map = currentNamespace.toMap();
    for (String key: map.keySet()) {
        TemplateModel templateModel = map.get(key);
        System.out.println(key + "=" + templateModel);
        //boolean isNotMacroOrFunction = ... how ?
        //boolean isFromAssignDirective = ... how ?
    }


-- 
Do not pursue the past. Do not lose yourself in the future.
The past no longer is. The future has not yet come.
Looking deeply at life as it is in the very here and now,
the practitioner dwells in stability and freedom.
(Thich Nhat Hanh)

Mime
View raw message