You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@freemarker.apache.org by "Daniel Dekany (JIRA)" <ji...@apache.org> on 2017/09/23 07:19:03 UTC
[jira] [Commented] (FREEMARKER-73) ?seq to convert some models to a
sequence
[ https://issues.apache.org/jira/browse/FREEMARKER-73?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16177605#comment-16177605 ]
Daniel Dekany commented on FREEMARKER-73:
-----------------------------------------
Coincidentally, I have added {{?sequence}} about a week ago, and it should be released early October in 2.3.27. (I ran into a problem where I needed this in FM3 about two weeks ago, then decided to backport it, but before I could, someone asked for this feature on StakOverflow, and now you raise the same issue too... After 14 years of slumber, it seems that non-{{List}} {{Iterable}}-s decided to attack this month.) It should work for everything that's {{#list}}-able, which includes {{Iterable}}-s (assuming properly high {{incompatibleImprovements}} setting, or more precisely, any proper {{ObjectWrapper}}). Also, it returns sequences as is.
It doesn't accept hashes though. I try to avoid too smart things, the things that the reader of the template wouldn't expect. Also FTL can have values that are both collections and hashes. So if there's a sufficiently important use case for that, I would rather add {{?key_value_pairs}} or such. But we already have {{<#list hash as k, v>}}. Isn't that sufficient?
Regarding {{Stream}}-s, none of the stock {{ObjectWrapper}}-s recognize that interface, thus they aren't listable at the moment. But that's a separate issue.
> ?seq to convert some models to a sequence
> -----------------------------------------
>
> Key: FREEMARKER-73
> URL: https://issues.apache.org/jira/browse/FREEMARKER-73
> Project: Apache Freemarker
> Issue Type: Bug
> Components: engine
> Affects Versions: 2.3.26-incubating
> Reporter: Ondra Žižka
>
> The objects I feed to FreeMarker often cotain {{Iterator}}s rather than {{Collection}}s.
> While it's quite easy to write a FreeMarker function {{iteratorToList(Iterator)}}, it would be convenient to have the following:
> A built-in that would convert various models to a sequence. Could be named {{?seq}} or {{?sequence}}.
> Usage:
> {code}<#assign foo = myIterable?seq>{code}
> {code}<#assign foo = myIterator?seq>{code}
> * A sequence would stay intact.
> * An {{Iterable}} wrapped in FM model would become internally a {{List}} - all elements consumed and stored.
> For me, other cases are not that important, but with some imagination, other models could also leverage it, like,
> * A {{Stream}} could be also turned into a {{List}}.
> * A hash could turn into a sequence of key/value pairs. That could be useful for debugging purposes.
> I am not sure if I didn't propose that already, but I could only find FREEMARKER-62.
> Thanks for considering.
--
This message was sent by Atlassian JIRA
(v6.4.14#64029)