I’m wondering is there an easy way to expose the entire data model of a Freemarker template to JavaScript, preferably converting it to a JS object literal somewhere along the way.
I know how to iterate through the keys:
<#list .data_model?keys as key>
</#list>
And presumably I could build a global JS variable within the template as such?
You could traverse the whole data model with FreeMarker and then produce the “source code” for a corresponding JSON object, but you’d have to deal with things like string escaping (e.g. with the js_string builtin) and potentially large (recursive) object graphs.
Some years ago I wrote a generic data model “dumper” which prints a nice tree representation of the FreeMarker data model. It’s probably not exactly what you want or need, but maybe it’s an inspiration. You can find it here.