Search code examples
javafreemarker

Freemarker - print an object into another object


I'm currently working with the FreeMarker Java Template Engine (.ftl)

I have a config file object that i would like to print into another ftl file (OutputScript.ftl)

Here is my config.ftl

<#assign config = {
"hp": {
    "product" : {
        "title": {
            "top": "true",
            "bottom": "false"
        }
    }
}
} />

Here is my OutputScript.ftl

<script>
window.object = {
    config : {
        // write the config object inside
    }
}
</script>

I have set up a page (page.ftl) where I call both files

<#import "/config/config.ftl" as config />
<#attempt><#include "XXX/OutputScript.ftl" /><#recover><!--Error: module OutputScript.ftl ${.error}--></#attempt>

Currently, i'm not able to to print it out. I have tried stuff like this without any luck

          <#if config.config.hp??>
            <#list config.config.hp as page>
                ${key}: ${page[key]}
            </#list>
        </#if>

Also, can we make it dynamic?(if my config file will have more indentation, will it still work?)

Thanks


Solution

  • I have updated Freemarker to the last version in order to use the ?eval_json

    <#assign config = '{
        "test" :"testValue",
        "hp": {
            "product" : {
                "title": {
                    "top": {
                        "header": {
                            "big": "true",
                            "test" : 14
                        },
                        "header2": {
                            "big2": "true2"
                        },
                        "header3" : "true3"
                    }
                }
            }
        }
    }'>
    
    <#assign configRedux = config?eval_json>