freemarker-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Daniel Dekany <ddek...@freemail.hu>
Subject Re: How to filter TemplateModel by 'type' and how it is put into namespace ?
Date Mon, 23 May 2016 17:24:26 GMT
#assign will put the values into the current namespace, and I think
nothing else will in practice.

If something is a macro or function... Ignore the deprecation warning
and do

  templateModel instance freemarker.core.Macro

This matches both macros and functions... (Legacy like these will be
fixed if there will be an FM 3... it can't really be in FM 2.)


Monday, May 23, 2016, 3:44:31 PM, Albert Kam wrote:

> 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 ?
>     }
>
>

-- 
Thanks,
 Daniel Dekany


Mime
View raw message