4

I just want to know how to access an array in SMARTY that have generated index via smarty. I want to access it later.

Got something like this:

{if $smarty.get.{param_{$k.id}_{$p.num}} eq $key}

and I like to access $smarty.get.(dynamic generated index)

Tried a couple variations, but didn't have any luck.

3
  • working right now. When I get an answer I will accept it. Commented Jun 19, 2012 at 17:49
  • have you tried {$smarty.get[$your_var]} ? Commented Jun 19, 2012 at 18:55
  • @Alex with [] don't work. Also give an parse error. Commented Jun 19, 2012 at 23:04

3 Answers 3

4

You can use variable for indexes, but you cannot build string the way you tried. Try to build a custom variable before:

{$myIndex = "param_{$k.id}_{$p.num}"}
{if $smarty.get.$myIndex eq $key}
    ...

This should work.

Sign up to request clarification or add additional context in comments.

3 Comments

It is not giving an error, but var seems to be empty... Have to check if I have some sort of error in my code.
Works like a charm. Still want to know why when I build string outside it goes well, but when I want to build a string in actual index it fails.
I think it's by design. Your code just isn't valid Smarty syntax.
3

In my case I used UnLoCo solution and tried to directly access array content (without if statement) with dynamic key and it works perfectly :

{$arr1["column_{$arr2.month}_price"]}

1 Comment

Confirmed, in PS 1.6.1.12 {$myVar["keybase_{$i}"]} works.
0

This will work also

{if $smarty.get["param_{$k.id}_{$p.num}"] eq $key}
  ...

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.