Sort data by variable in latte

materix
Backer | 82
+
0
-

I read about yet another Latte-awesomeness here: https://blog.nette.org/…e-like-a-pro: the group and sort by filters.

{foreach ($items|sort: by: 'name') as $item}
	{$item->name}
{/foreach}

Is it somehow possible to set the sort by key as a variable?

E.g. (this does not work)

{var $my_sort_key = 'name'}
{foreach ($items|sort: by: {$my_sort_key}) as $item}
	{$item->name}
{/foreach}
Marek Bartoš
Nette Blogger | 1244
+
+1
-
{var $my_sort_key = 'name'}
{foreach ($items|sort: by: $my_sort_key) as $item}
	{$item->name}
{/foreach}
materix
Backer | 82
+
0
-

Thanks! (I thought I had tried that…)

Can the sort-direction, also be a variable?

E.g.


{var $my_sort_key = 'name'}
{var $my_sort_dir = 'reverse'}
{foreach ($items|sort: by: $my_sort_key|$my_sort_dir) as $item}
	{$item->name}
{/foreach}

Last edited by materix (2024-06-26 09:08)

Marek Bartoš
Nette Blogger | 1244
+
0
-

Reverse is a filter and filters can't be (afaik) applied dynamically so probably not

Last edited by Marek Bartoš (2024-06-25 01:42)