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 use data from a array in a object methode

 
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
andi_steiner
Smarty n00b


Joined: 17 Jul 2019
Posts: 2

PostPosted: Wed Jul 17, 2019 12:07 pm    Post subject: How use data from a array in a object methode Reply with quote

Hello All

PHP code is like this:
Code:

class myObj{
   protected $addr = [];

   public function addAddress($str){
      $this->addr[] = $str;
   }
   public function getAddresses(){
      return $this->addr;
   }

   ...... a lot of other functions comes here....
}

$obj = new myObj();
$obj->addAddress('aaa');
$obj->addAddress('bbb');
$obj->addAddress('ccc');

$obj2 = new myObj();
$obj2->addAddress('ddd');
$obj2->addAddress('eee');
$obj2->addAddress('fff');

$smarty = new Smarty();
$smarty->assign('obj', $obj);
$smarty->assign('obj2', $obj2);


HTML code is like this:
Code:

   {$obj->getAddresses()[0]} vs. {$obj2->getAddresses()[0]}


Why crash smarty by this code:
Code:
$obj->getAddresses()[0]

someone has a solution??
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Wed Jul 17, 2019 7:50 pm    Post subject: Reply with quote

What you are doing in the HTML SMARTY tags is called 'function array dereference' (FAD).

The question to answer is, is Smarty complaining about a syntax error, or is PHP crashing because the PHP version is older than when FAD was implemented (as of PHP 5.4)?
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Jul 18, 2019 1:01 pm    Post subject: Reply with quote

Code:
PHP Fatal error:  Uncaught  --> Smarty Compiler: Syntax error in template "file:C:\Users\Daemon\Documents\.github\test-001\testcase.php"  on line 1 "{$a->b()[0]}"  - Unexpected "[", expected one of: "}" <--
  thrown in C:\Users\Daemon\Documents\.github\test-001\vendor\smarty\smarty\libs\sysplugins\smarty_internal_templatecompilerbase.php on line 1


This is with @dev Smarty version. It seems the parser is not prepared.
Addressing the issue as a whole, please don't do this, ever.
Or best case, use ArrayAccess objects. Relying on language specifics to build your templates you are unnecessarily increasing the entry level to template writing.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Jul 18, 2019 9:07 pm    Post subject: Reply with quote

Testcase https://github.com/AnrDaemon/test-001/tree/forum-27812
Back to top
View user's profile Send private message
andi_steiner
Smarty n00b


Joined: 17 Jul 2019
Posts: 2

PostPosted: Tue Jul 23, 2019 9:41 am    Post subject: Re: How use data from a array in a object methode Reply with quote

andi_steiner wrote:
Hello All

PHP code is like this:
Code:

class myObj{
   protected $addr = [];

   public function addAddress($str){
      $this->addr[] = $str;
   }
   public function getAddresses(){
      return $this->addr;
   }

   ...... a lot of other functions comes here....
}

$obj = new myObj();
$obj->addAddress('aaa');
$obj->addAddress('bbb');
$obj->addAddress('ccc');

$obj2 = new myObj();
$obj2->addAddress('ddd');
$obj2->addAddress('eee');
$obj2->addAddress('fff');

$smarty = new Smarty();
$smarty->assign('obj', $obj);
$smarty->assign('obj2', $obj2);


HTML code is like this:
Code:

   {$obj->getAddresses()[0]} vs. {$obj2->getAddresses()[0]}


Why crash smarty by this code:
Code:
$obj->getAddresses()[0]

someone has a solution??


I have found a solution for this problem:

Code:

   {assign "addr2" $obj2->getAddresses() nocache}
   {foreach $obj->getAddresses() as $addr}
      {$addr} vs. {addr2[$addr@index]}
   {/foreach}
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