0

I'm reading a key/value pair from a table which is denoted as such:

fruit:${my_fruit} in the FOOD column.

In my Freemarker code, I have, say:

<#assign my_fruit = "banana">

I want to wind up with the equivalent of

<#assign fruit = ${my_fruit}> executed.

I've tried many variations of

<#fruit_list = "${FOOD}"!?split(":")>

<#assign fruit_raw = r"<#assign " + "${fruit_list[0]}" + r"='" + "${fruit_list[1]}" + r"'>">

<#assign fruit_int = fruit_raw?interpret>

<@fruit_int?exec/>

Can anyone tell me where I'm going wrong? Many thanks.

  • As of the actual question, I have commented your answer below. But otherwise, it's quite odd if you need to assign to a dynamic variable name. Like, who will read that variable later, and how? (`.vars[furit_list[0]]` works, but is ugly.) Isn't this some use-case that can be properly solved with a `Map` in the data-model? – ddekany May 25 '21 at 00:08
  • There is no data model as such. We have no access to the java. We read in data from either a table or a json-like structure. – ikonoklast May 26 '21 at 06:29
  • FreeMarker itself can't read in data, so there's some custom Java code there that does that. – ddekany May 26 '21 at 11:21
  • Of course, but we have no access to it. – ikonoklast May 26 '21 at 21:42

2 Answers2

0

This seems to work:

<#if fruit_list[0]?? && fruit_list[1]??>
<#assign fruit_txt = "<#assign ${fruit_list[0]} = '${fruit_list[1]}'>">
<#assign fruit_raw = 'r"${fruit_txt}"?exec'>
<@fruit_raw?interpret/>
</#if>
  • I assume `?exec` mean to be `?eval`. That middle step that assigns to `fruit_raw` has no use, you could just write `<@fruit_txt?interpret/>`. In fact, you could just write `<@("<#assign ${fruit_list[0]} = '${fruit_list[1]}'>"?interpret) />`. – ddekany May 25 '21 at 00:03
  • In the Responsys variant of Freemarker, the exec builtin exists and it's different to eval. I left it in there, because the code above is a simplification of my actual situation. The exec does no harm and is occasionally required if "fruit_txt" becomes a more complex expression. – ikonoklast May 26 '21 at 06:25
  • I'm certain the usage of `'` in that line is messed up. It would be cleaner what the intent was if that's fixed. – ddekany May 26 '21 at 11:19
0

Based on the simplicity of your question <#assign fruit=my_fruit> will work in Responsys. I'm guessing though that you're looking for something more complicated? enter image description here