Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

How to order an array of objects with help of modifiers

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
davsto
Smarty n00b


Joined: 04 Jul 2016
Posts: 2

PostPosted: Mon Jul 04, 2016 5:39 pm    Post subject: How to order an array of objects with help of modifiers Reply with quote

Hi there,

I'm new to Smarty and struggling with modifiers currently while trying to sort an array with objects.

My current template code that I have to modify looks like:
Code:

{foreach from=$bookShelf item=$book}
    {include file="/book_details.tpl"}
{/foreach}


The corresponding output looks like:
Code:

book title A, id: 13
book title B, id: 10
book title C, id: 17


Now, my aim is to order the books with the help of the id property.
The result should look like:
Code:

book title B, id: 10
book title A, id: 13
book title C, id: 17


I tried to add a file to my project called modifier.sortby.php with the following content (content originally from this forum):

Code:
<?php
/**
 * modifier.sortby.php - 2005-08-19
 * ------------------------------------------------------------------
 * Smarty Plugin - Array Sort
 * Sorts an array by supplied fields
 * by cablehead, messju and pscs on Smarty Forums
 * orig by dholmes at jccc d0t net - from http://au.php.net/function.uasort
 */

function array_sort_by_attribute(&$data,$sortby) {
    static $sort_funcs = array();
    if (empty($sort_funcs[$sortby])) {
        $code = "$c=0;";
        foreach (split(',',$sortby) as $key) {
            $d = '1';
            if (substr($key,0,1) == '-') {
                $d = '-1';
                $key = substr($key,1);
            }
            if (substr($key,0,1) == '#') {
                $key = substr($key,1);
                $code .= "if ( $a['$key'] > $b['$key']) return $d * 1;n";
                $code .= "if ( $a['$key'] < $b['$key']) return $d * -1;n";
            } else {
                $code .= "if ( ($c = strcasecmp($a['$key'],$b['$key'])) != 0 ) return $d * $c;n";
            }
        }
        $code .= 'return $c;';
        $sort_func = $sort_funcs[$sortby] = create_function('$a, $b', $code);
    } else {
        $sort_func = $sort_funcs[$sortby];
    }
    uasort($data,$sort_func);
}

# Modifier: sortby - allows arrays of named arrays to be sorted by given field
function smarty_modifier_sortby($arrData,$sortfields) {
    array_sort_by_attribute($arrData,$sortfields);
    return $arrData;
}

$smarty->register_modifier( "sortby", "smarty_modifier_sortby" );
?>


Unfortunately, when using the modifier...
Code:

{foreach $bookShelf|@sortby:"id" as $book}
    {include file="/book_details.tpl"}
{/foreach}


... an error occurs:
Quote:

SmartyCompilerException: Syntax Error in template "/pathToFile/list.tpl" on line m "{foreach $bookShelf|@sortby"id" as $book}" unknown modifier "sortby" in ...\Smarty\sysplugins\smarty_internal_templatecompilerbase.php on line n


Do you have any ideas for me how to fix it?
Do I have to place the modifier file in a certain folder?
Or do I have to register the modifier in another file?


Thank you very much in advance.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Mon Jul 04, 2016 8:11 pm    Post subject: Reply with quote

The list should be ordered before it is passed to the template.
Back to top
View user's profile Send private message
davsto
Smarty n00b


Joined: 04 Jul 2016
Posts: 2

PostPosted: Tue Jul 05, 2016 7:13 am    Post subject: Reply with quote

Thanks for your reply.

I'm totally with you and would prefer it that way as well. But I get the list from a 3rd party component written by a sub-contractor and do not have the access to the list at a further location in code. That was the reason for me to decide using a modifier.

Isn't it possible to order the list in the template?
I thought I can use the @ sign to modify the whole list before the foreach loop iterates through the items.
Code:
{foreach $bookShelf|@sortby:"id" as $book}
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue Jul 05, 2016 7:15 am    Post subject: Reply with quote

"@" is an error suppression operator and must not be used in production code.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP