Re: [#list with more than 1 variable and a complex condition, like java's for loop?
"Newman, John W" <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <D688C4D318A63D4290AA94A33E06F7D201C784336A@msxmbxnsprd14.acct.upmchs.net> |
Yeah that's about right. My use case is a little different - here's an example of what I ended up with:
[#ftl]
[#macro splice list1 list2]
[#local n = list1?size /]
[#local m = list2?size /]
[#local i = 0 /]
[#local j = 0 /]
[#local nextSortOrder = 1 /]
[#list 0..(n + m) as dummy]
[#if i < n]
[#local currentSortOrder = list1[i] /]
[#if currentSortOrder == nextSortOrder]
[#nested "list1" currentSortOrder /]
[#local i = i + 1 /]
[#elseif j < m]
[#local currentSortOrder = list2[j] /]
[#nested "list2" currentSortOrder /]
[#local j = j + 1 /]
[/#if]
[#local nextSortOrder = nextSortOrder + 1 /]
[#else]
[#break /]
[/#if]
[/#list]
[#list j..m - 1 as k]
[#nested "list2" list2[k] /]
[/#list]
[/#macro]
[@splice [1, 3, 5, 7, 9, 10], [2, 4, 6, 8, 11, 12]; type, val]
[#if type == "list1"]
First list: ${val}<br />
[#elseif type == "list2"]
Second list: ${val}<br />
[/#if]
[/@]
IMO [#continue /], [#stopGracefully /], [#for], & [#while] are things worth looking at adding to the language. But we can certainly get by fine without them, it is just extra code. More important would be nested callbacks from a macro (this was discussed on this list at length a long time ago, http://osdir.com/ml/web.freemarker.user/2005-09/msg00018.html). That is what I need here to get rid of the [#if with the string compare, we have this in a few places and it is dirty but it works. If I could add one thing to FM, it would be that.
Thanks
-----Original Message-----
From: Daniel Dekany [mailto:[email protected]]
Sent: Thursday, June 04, 2009 5:33 AM
To: Newman, John W
Cc: FreeMarker-user
Subject: Re: [FreeMarker-user] [#list with more than 1 variable and a complex condition, like java's for loop?
Wednesday, June 3, 2009, 11:31:34 PM, Newman, John W wrote:
> Hello,
>
> Is there any way to do this in FM:
>
>
> for (int i = 0, j = 0; i < n || j < m;) {
> }
>
> Normally I never do things like that, but it works out great here.
> And I have to render almost the same thing in FM as what I've done
> in java using that loop. I can't seem to get anywhere with [#list
> and [#break .. any help is appreciated! Thanks
[#assign i = 0]
[#assign j = 0]
[#list 0..1000000 as ignore]
[#if !(i < n || j < m)]
[#break]
[/#if]
...
[/#list]
--
Best regards,
Daniel Dekany
------------------------------------------------------------------------------
OpenSolaris 2009.06 is a cutting edge operating system for enterprises
looking to deploy the next generation of Solaris that includes the latest
innovations from Sun and the OpenSource community. Download a copy and
enjoy capabilities such as Networking, Storage and Virtualization.
Go to: http://p.sf.net/sfu/opensolaris-get