1

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?

4 Answers 4

5

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}
Sign up to request clarification or add additional context in comments.

2 Comments

Arvo is right I've ran into the same problem and this was the solution I used.
An oldy, but found it high up in google: I've used a different solution without an extra assign. Added it as answer below
1

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}

2 Comments

Didn't work for me. Might be because your version of Smarty is different than mine?
'=' should be inside the braces, then it works (Smarty 3). I edited the post to reflect that. Nice solution, @Nanne!
0

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.

Comments

0

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 :) ).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.