Reputation: 2190
I have two loops running in my code, I want to use an element from an array as the key in a second array, but am unsure how to do this with Smarty.
"$dateAndUserForEdit.$key.edit_id
" contains an integer (pulled from the db)
I want to use that value as the key in a second loop, which runs fine if I harcode in the integer:
{foreach from=$historyOfRepair.9 key=key item=i}
Pseudo code for the sort of thing I've been trying is:
{foreach from=$historyOfRepair.{$dateAndUserForEdit.$key.edit_id} key=key item=i}
But of course, this doesn't work! Can anybody help?
Upvotes: 1
Views: 3365
Reputation: 64399
I don't know if this is trick was needed for old versions, but you can (as I believe for quite some time now) do this:
given that:
{$someArray.someKey=9}
{$otherArray.9=$someValue}
Equivalent:
{$otherArray[$someARray.$someKey]=$someValue}
Upvotes: 1
Reputation: 1284
I would agree with Arvo. You will want to assign the id to a temporary variable so that it can be used as a substitution in the foreach loop. Note that in his example code, the value of $key will be replaced with the key of the current item of the $historyOfRepair array. Otherwise that works ( tested too :) ).
Upvotes: 0
Reputation: 296
I'm only commenting here because no one has given you any advice yet... I have never used Smarty as I've always just made my own templating systems (So I might be ignorant here and my advice useless)...
Can you just construct your array without smarty and then pass it to Smarty for display? Personally, rather than mess with custom template engine code, that's probably what I would do for something other than basic stuff.
Upvotes: 0
Reputation: 10570
Something like next may work (cannot test currently):
{assign var=edit_id value=$dateAndUserForEdit.$key.edit_id}
{foreach from=$historyOfRepair.$edit_id key=key item=i}
Upvotes: 5