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

{foreach} Use methods of objects stored in array

 
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 -> Tips and Tricks
View previous topic :: View next topic  
Author Message
mscotch
Smarty n00b


Joined: 01 Mar 2009
Posts: 1

PostPosted: Sun Mar 01, 2009 1:25 am    Post subject: {foreach} Use methods of objects stored in array Reply with quote

Hello

I'd problems with accessing my getter-functions of my objects stored in an array. I didnt want to assign each object so I found this solution to access class-methods in a {foreach} even if the objects are stored in an array.

Please take a look at the following Wrapper and give me feedback!

Code:

<?php
   /**
    * @author Michael Schnyder
    * @version 2009-02-28
    *
    * Use this Class to have full access Object Arrays in Smarty Template Engine
    *
    * Smarty can handle Arrays of Object, but only if the Array ist returned from a Function, while running a smarty-tpl.
    * If you assign an array of Objects to smarty, you won't be able to access them in an forach-loop. But if you have
    * the ability to call a function of a Object wich returns an array of other objects, it is possible to use them.
    *
    * Example
    * =======
    *
    * Assign variables to smarty NOT WORKING!!!
    * -----------------------------------------
    *
    * PHP:
    * class MyClass {
    *     private $foo;
    *     public function  __construct($v) { $this->foo = $v; }
    *     public function getFoo() { return $this->foo; }
    * }
    *
    * $smarty->assign('ObjectArray', array(new MyClass("bar"), new MyClass("bar2"));
    *
    * Smarty:
    * {foreach from=$ObjectArray item=oi}
    * {$i->getFoo()}
    * {/foreach}
    *
    *
    * Assign Wrapper to smarty, WORKING!!!
    * ------------------------------------
    *
    * PHP:
    * class MyClass {
    *     private $foo;
    *     public function  __construct($v) { $this->foo = $v; }
    *     public function getFoo() { return $this->foo; }
    * }
    *
    * class Wrapper {
    *     function giveMeArray() {
    *         return array(new MyClass("bar"), new MyClass("bar2"));
    *     }
    * }
    *
    * $smarty->assign('ObjectArrayWrapper', new Wrapper());
    *
    * Smarty
    * {foreach from=$ObjectArrayWrapper->giveMyArray() item=oi}
    * {$i->getFoo()}
    * {/foreach}
    */

class SmartyObjectArrayWrapper {
   
   private $OAs;
   
   function __construct() {
      $this->OAs = array();
   }
   
   function addObjectArray($oa, $accessFunction) {
      
      $this->OAs[$accessFunction] = $oa;
      
   }
   
   
   //Handle all other Functions
   function __call($name, $arruments) {
      
      if(array_key_exists($name, $this->OAs)) {
         $res = $this->OAs[$name];
      }
      
      return $res;
   
      
   }
}

?>



The trick is to call inside the tpl a function (of the Wrapper-Object) wich returns the array of objects in runtime of smarty an not while assign.

If have no Idea why it doesn't work with simplier way, can anyone tell me?

Michael
Back to top
View user's profile Send private message
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 423

PostPosted: Sun Mar 01, 2009 8:09 pm    Post subject: try if works; something more. Reply with quote

It looks you are working with singleton design pattern. Please try the following, if this works for you:

* Make the variable static too, to hold the objects, instead of just private.

* Assign the object by reference.

Another, {$i->getFoo()} looks irrelevant in this piece of code.
Back to top
View user's profile Send private message Visit poster's website
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 -> Tips and Tricks 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