4

I need to sort an array with smarty. I try to use this code :

My foreach:

 {foreach $hooks->addblock as $addblock}
  <ul>
  {foreach $addblock|@sortby:"prio" as $value}
    {$value@key}: {$value}
  {/foreach}
  </ul>
 {/foreach}

The var_dump of $hooks->addblock:

array(5) {
  ["filed_1"]=>
  array(5) {
    ["id"]=>
    string(7) "filed_1"
    ["title"]=>
    string(6) "filed1"
    ["field"]=>
    string(20) "This is test filed 1"
    ["size"]=>
    int(740)
    ["prio"]=>
    int(7)
  }
  ["filed_2"]=>
  array(5) {
    ["id"]=>
    string(7) "filed_2"
    ["title"]=>
    string(6) "filed2"
    ["field"]=>
    string(20) "This is test filed 2"
    ["size"]=>
    int(740)
    ["prio"]=>
    int(6)
  }

So, I want to sort array by value prio (num) ascending, but it does not come out properly!
Here the result:
Output smarty

I would like the line "field_2" be first. But I not find a solution.

This is the modifier (modifier.sortby.php): http://www.smarty.net/forums/viewtopic.php?p=23628#23628 I also have a recurring error:

[Sat Nov 24 20:04:52 2012] [error] [client 127.0.0.1] PHP Notice: Uninitialized string offset: 0 in /var/www/libs/plugins/modifier.sortby.php(33) : runtime-created function on line 1

0

2 Answers 2

4

PHP Notice: Uninitialized string offset: 0 in /var/www/libs/plugins/modifier.sortby.php(33) : runtime-created function on line 1

This provides a clue as to how the sortby modifier is working.

The documentation you linked to says:

The '-' lets you sort in reverse order, and the # lets you sort numerically rather than as a string (you can have '-#age' as well to sort numerically in reverse order)

Your prio field is an integer, and although in PHP you can do many operations transparently across integers and strings, the notice indicates that sortby has implemented string sort by accessing each character individually. This cannot possibly work with an integer.

So, you should use the # modifier:

{foreach $addblock|@sortby:"#prio" as $value}
                            ^
Sign up to request clarification or add additional context in comments.

Comments

0

Don't do it. Sorting is not a task the template engine should do. It's clearly business logic and needs to go into the controller.

5 Comments

why template not do it? and what solution you proposed to do this?
Sort the array in pure PHP before it is handed to Smarty, for example with uasort($addblock, function($a, $b) { return $a['prio']>$b['prio']; });
Arguably it's purely a presentational thing. Either way it's an interesting mystery.
Notice that Smarty templates are compiled. So what's the difference between sorting the array in the controller and compiled template? There is non. Sorting just takes place a little bit later when using Smarty approach.
Sometimes you don't have access to the backend...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.