Re: How to assign a variable to an evaluated string
Rebecca Younes <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <C81038F8.E47%[email protected]> |
Yes, you're right, the comma needs to be inside the <#if>. I hadn't realized that FreeMarker split() will create a final empty array item if the string ends in the delimiter, since that's counter to the behavior of a lot of programming languages (though it is explained in the doc). Another approach is to strip off a string-final delimiter before doing the split, in order to tolerate error on the part of the template authors. From: "Daniels, Troy (US SSA)" <[email protected]> Subject: Re: [FreeMarker-user] How to assign a variable to an evaluated string To: FreeMarker-user <[email protected]> Message-ID: <mailman.365990.1273603460.8007.freemarker-user@lists.sourceforge.net> Content-Type: text/plain; charset="us-ascii" ________________________________ From: Rebecca Younes [mailto:[email protected]] Sent: Monday, May 10, 2010 1:30 PM To: [email protected] Subject: [FreeMarker-user] How to assign a variable to an evaluated string Consider this template code: <#assign useOranges=false> <#assign fruit> apples, bananas, <#if useOranges> oranges </#if> </#assign> <#assign arr=fruit?split(",")> ${arr?size}<br /> ${fruit} Your second comma is outside the #if, so I think you will always get three elements, although the last one might be empty. Have you tried <#assign fruit> apples, bananas <#if useOranges> , oranges </#if> </#assign> which wyould give you apples, bananas without the trailing comma. Troy This outputs: 3 apples, bananas, What I want is for ${arr?size} to print 2 rather than 3: i.e., for the contents of fruit to be evaluated before assigning it to arr. I've tried adding: <#assign fruit2>${fruit}</#assign> <#assign arr=fruit2?split(",")> I also tried the eval built-in: <#assign fruit2>${fruit?eval}</#assign> <#assign arr=fruit2?split(",")> Neither of these produce the desired output. Is there a way to do this in FreeMarker? ------------------------------------------------------------------------------ _______________________________________________ FreeMarker-user mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/freemarker-user