Rache
Rache

Reputation: 217

How to read a template to get some properties defined in Freemarker template

I am using Freemarker template to read the template from a url and then replace some sections in my email before sending it. Is there any way to read the template and get value. For example, somewhere in the template there is a list of properties which i need to process the email before sending it. Also I will remove this section after processing the template. This part i can solve by setting datamodel value for showSection as false.

<div class="prop">
    <#if showSection == true>
        <key> <value>
        <key> <value>
        <key> <value>
    </#if>
</div>

I looked at the documentation but couldn't find a way to get sections from the template.

Upvotes: 0

Views: 408

Answers (1)

ddekany
ddekany

Reputation: 31112

There's no practical way of doing that. By "no practical" I mean that in theory you could parse the template yourself and extract that information, maybe even leverage the internal AST API of FreeMarker (which still doesn't parse the XML parts), but that would be ugly and a lot of work.

Also, especially as you remove that part from the template when it produces the output (and the purpose of a template is to produce output), can't you just store that information outside the template?

If you must put extractable data into a template, then the closest thing is something like <#ftl attributes={"myProps": { "a": 1, "b", 2 }} > and then template.getCustomAttribute("myProps") to get that Map.

Upvotes: 1

Related Questions