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

Smarty Complex assignment - possible?

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


Joined: 07 Nov 2014
Posts: 1

PostPosted: Fri Nov 07, 2014 8:21 pm    Post subject: Smarty Complex assignment - possible? Reply with quote

Hello,

I am currently forced to use Smarty 2.6.12 and I'd like to know if the following problem is my misunderstanding, or perhaps a bug that has been fixed in a later version.

This code creates test data that is (in structure) similar to my real data:
Code:
class Abc {
   /* some properties */
}

class Foo {
   public $abcTab;
   public $ix;
}

$f1 = new Foo();
$f1->abcTab = ARRAY( 1 => new Abc(), 2 => new Abc() );
$f1->ix = 2;

$f2 = new Foo();
$f2->abcTab = ARRAY( 3 => new Abc(), 1 => new Abc() );
$f2->ix = 1;

$tpl = new Smarty;
$tpl->assign('fooTab', ARRAY($f1, $f2));
$tpl->display('mytemplate.tpl');


So, my simplified data structure is an array of objects, each containing another array of objects and a locator index. In reality, I have more locators and do more with abcTab than shown below, so I don't think that I can translate my data into a simplified representation when I transfer it into the template.

Now in mytemplate, I iterate over fooTab and per loop, I want to retrieve the Abc object in the $abcTab collection at index $ix, because I need to display it's properties and call some helper methods on it. I try it like this:
Code:

{foreach from=$fooTab item=foo}
  {assign var='anAbc' value=`$foo->abcTab[$foo->ix]`}
  Here is your {$anAbc->Bling} entry for {$anAbc->Do()}.<br>
{/foreach}

But Smarty misinterprets the assign, it compiles it to "`".($this->_tpl_vars['foo'])."->bar[".($this->_tpl_vars['foo'])."->ix]`"

What works is this (but I don't like the two-step approach):
Code:

  {assign var='ix' value=`$foo->ix`}
  {assign var='anAbc' value=`$foo->abcTab[$ix]`}


Is this two-step assign necessary, or are there later Smarty versions that understand the oneliner? Or is my one-liner plain wrong?

Greetings Smile
Robo
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Sun Nov 09, 2014 1:10 am    Post subject: Reply with quote

All Smarty2 versions have the problem when object properties are used within other object properties like your array index.

It has been resolved in Smarty3 but I think this is currently no option for you.

So you have to use your work around.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Fri Nov 21, 2014 12:30 am    Post subject: Reply with quote

I've found Smarty3 to be a simple drop-in replacement for Smarty2. The only issue is if you're using PHP code in templates, which you should really never do.
I suggest to migrate to Smarty3 at the nearest possible occasion. I.e. today.
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