Home Ask Login Register

Developers Planet

Your answer is one click away!

dylanmac February 2016

Freemarker: how to reorder a sequence using an array variable

I have a sequence that I want to reorder in the ftl. I'm wondering if there is a way to pass in an array var that I create in the page (using values that exist in the sequence) and use that as the reorder values. Here is the code I tried:

<#assign reorderedList = ["order3","order1","order4","order2"]>
<#list columns?sort_by('reorderedList') as column>

I also tried this:

<#assign reorderedList = [
    {"name":"grading"}] />

<#list columns?sort_by('reorderedList["name"]') as column>

I realize that neither of these adheres to the sort_by builtin spec ("columns" is not the hash var I created). Anyway any suggestions about how to achieve this would be appreciated.


ddekany February 2016

You shouldn't do that in FTL, and actually you can't on any sane way. Perhaps even adding ?sort_by was a bad idea back then, as sorting isn't really the duty of the MVC View (unless, you have some fancy UI component, but then, it sorts with JavaScript or on the server side via an Ajax callback). The template should get the lists already sorted, ideally. If you really can't do that, call some helper Java methods from the template to do that work.

dylanmac February 2016

So rather than reordering, I assigned each value to a var and outputted them in the order I wanted in the FTL. Inelegant but there are only four values.

Post Status

Asked in February 2016
Viewed 2,121 times
Voted 4
Answered 2 times


Leave an answer

Quote of the day: live life