Search code examples
freemarkerbeanshellfmpp

FMPP: How to set a Freemarker variable from a BeanShell script


Is it possible to set one or more freemarker variable in a case like:

<#assign test=pp.loadData('eval', '
a="test1";
b="test2";
return "test";')>

and having access to a and b in the freemarker script ?


Solution

  • I guess it can't be done without writing a custom DataLoader. I'm saying "guess" because maybe I don't know about a BeanShell trick. The closest I could get is using return this.namespace; and then ${test.getVariable('a')}. This is too verbose of course.

    Update: Actually, the following horror is even closer:

    <#assign test=pp.loadData('eval', '
        a="test1";
        b="test2";
    
        // This should be factored out into a common function somehow
        ns = this.namespace;
        vars = new HashMap();
        for (name : ns.getVariableNames()) {
          vars.put(name, ns.getVariable(name));
        }
        return vars;
    ')>
    
    ${test.a}