Question

I am trying to render a json that contains escaped xml. So " should be \", new lines \n, etc. To make it more readable I want to divide that into two files, one with json, the second one with xml. Both of them must be templates as they have some dynamic values.

{
"xml" : "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n ..."
}

json.ftl:

{
"xml" : "<#include "xml.ftl">"
}

xml.ftl:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>...

How can I achieve that escaping? I know that there are js_string and json_string but the problem is that I do not know how to apply them with include. Thanks for help

Was it helpful?

Solution

You could make a macro like this:

<#macro includeAsJsonString templateName>
  <#local captured><#include templateName></#local>
  ${captured?json_string}<#t>
</#macro>

and then you do this:

{
"xml" : "<@includeAsJsonString 'xml.ftl' />"
}

(Of course, you don't have to create a macro for this, but I think that's more reusable.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top