smarty plugin foreachgroup

Im trying to use the below plugin, but ive just recently started to get into smarty, and an not even sure if im using the plugin correctly.  I created a new file and pasted the below function smarty_block_foreachgroup($params, $content, $smarty, &$repeat) into it and saved inside my smarty plugin folder.

the problem is when I try to run inside my own template the results are not what I desire.  here is my remplate code.

{foreachgroup from=$privileges group_by='$privileges->cid'}
                  {foreach from=$current_group item=privilege}
The above foreach loop works fine, but the catergory headers do not display and no sorting is applied.  

Maybe im missing something, who knows, but ive included all i could find on this plugin below.  Thanks in advace, John

foreachgroup Plugin

Author: Dan Culley

Like XSLT 2.0's for-each-group tag, this block function sorts an array or iterator into groups and iterates over each group. The grouping key is available via the $current_grouping_key variable and the items in the group are available via the $current_group array. I have found this useful for doing things such as alphabetical breakdowns as well as creating table headers for city groups. Currently, php code must be used in the group_by attribute, as the function uses the php eval function to execute it; I would be happy to modify this to use Smarty code if someone would be kind enough to post an explanation of how the $smarty->_eval() function works, as I have been unsuccessful in trying to use it.

{foreachgroup from=$customers group_by='$customers->homeAddress->city'}
            <th colspan="3">{$current_grouping_key}</th>
      {foreach from=$current_group item=$customer}

Sample Ouput 2:

| Washington                 |
| Joe     | Current  |  $0.00|
| Mike    | Inactive | $20.00|
| New York                   |
| Polly   | Inactive |  $0.00|
| Molly   | Current  | $80.00|


function smarty_block_foreachgroup($params, $content, $smarty, &$repeat)
      static $grouparray = array();
      //Check required parameters
      if (!isset($params['from']))
            $smarty->trigger_error('foreachgroup: missing "from" parameter.');
      if (!isset($params['group_by']))
            $smarty->trigger_error('foreachgroup: missing "groupby" parameter');
      //If this is the first pass, sort into groups
      if (is_null($content))
            $from = &$params['from'];
            foreach ($from as $current)
                  $key = eval('return '.$params['group_by'].';');
                  if (!isset($grouparray[$key]))
                        $grouparray[$key] = array();
                  $grouparray[$key][] = $current;
      //For all other passes, get the next group
      //and set special variables.
      if (list($key, $item) = each($grouparray))
            $smarty->assign('current_grouping_key', $key);
            $smarty->assign('current_group', $item);
            $repeat = true;
      //If no more groups, stop the cycle.
            $repeat = false;
      if (!is_null($content))
            return $content;
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cone13coneAuthor Commented:
Ive found a solution on my own, there was a problem with the template plugins eval statement when evaluating an array of objects.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.