|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
andi_steiner Smarty n00b
Joined: 17 Jul 2019 Posts: 2
|
Posted: Wed Jul 17, 2019 12:07 pm Post subject: How use data from a array in a object methode |
|
|
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 |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Wed Jul 17, 2019 7:50 pm Post subject: |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Thu Jul 18, 2019 1:01 pm Post subject: |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
|
Back to top |
|
andi_steiner Smarty n00b
Joined: 17 Jul 2019 Posts: 2
|
Posted: Tue Jul 23, 2019 9:41 am Post subject: Re: How use data from a array in a object methode |
|
|
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 |
|
|
|
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
|