Re: How to put macros inside #assign
Daniel Dekany <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
Monday, January 21, 2013, 1:01:31 PM, George Pogorelschi wrote:
> Hello,
>
> I'm new to freemaker. I have the following assign:
>
> <#assign trueFalse = {"true" : "Yes", "false" : "No"}/>
>
> I want to localize the values so i tried to write it like this:
>
> <#assign trueFalse = {"true" : '<@spring.message code="radioYes"/>', "false" :
> '<@spring.message code="radioNo"/>'}/>
>
> But instead of the values for "Yes" and "No" I get the string "<@spring.message
code="radioYes"/>>".
> Any ideea how to implement this.
Macro and directive calls (<#...>, <@...>) can occur only outside
other FreeMarker constructs, since they are for direct printing to the
output and for other side-effects and flow control. So you need a
*function* (or method) here, like <#assign ... "false":
spring.message("radioNo")>, but maybe Spring doesn't provide a
function variation. That's something that they definitely should do,
but, if they don't do that, you can still write a wrapper function.
Create a something like lib/utils.ftl, and there:
<#function message code>
<#local s><@spring.message code="radioYes"/></#local>
<#return s>
</#function>
and then in the templates:
<#import "lib/utils.ftl" as u>
...
<#assign ... "false": u.message("radioNo")>
This isn't very fast though, as what happens here is that Spring gets
that message as a String internally, then prints it to the output
Writer @spring.message, but that output is captured by FreeMarker's
#local directive, and converted back to String and stored in "s". So
the real solution is implementing the function in Java as a
TemplateMethodModelEx, and there use the Spring API to get that String
directly. Or, even better, framework code like this has better place
in the data-model...
--
Best regards,
Daniel Dekany
------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. SALE $99.99 this month only -- learn more at:
http://p.sf.net/sfu/learnmore_122412