Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning a variable to an object in Freemarker templates

I am trying to avoid repeatedly refering to objects like this:

${outter.inner.assignThisGuy.field1}

and would like to refer to lowest level, i.e.

${assignThisGuy.field1}

This will help me remove repeated null checks and make my template tidier. I'm not sure if this is possible. All the notes refer to assigning a variable to a type like ?string, ?date, etc.

When I try to do it I get an exception:

freemarker.template.TemplateException: Expected hash. thisGuy evaluated instead to freemarker.template.SimpleScalar 

I have tried variations on:

[#assign thisGuy = "${outter.inner.assignThisGuy}" ]

I just want to be sure this is not possible before some of my lines become massive in length!

like image 894
Steve Avatar asked Dec 06 '13 16:12

Steve


1 Answers

It is possible, like this:

<#assign thisGuy = outter.inner.assignThisGuy>
...
${thisGuy.field1}
like image 78
ddekany Avatar answered Sep 20 '22 18:09

ddekany