user1122940
user1122940

Reputation:

Smarty 2 Variables I get an error when try to run 2 in the same string

I currently have the following in my database

tn_pic1

tn_pic2

They hold a filename for each one, for example tn_X_wm.jpg

X = the number and jpg could be gif, png, bmp

So for example in tn_pic1 I could have tn_1_wm.jpg but in tn_pic2 I could have tn_2_wm.png

In my .tpl file I currently have:

{if $item.pic_count > 0}
   {rand(1,$item.pic_count) assign="yourVar"}

   <img src="users/images/{$item.id}/{$item.tn_pic1}" width="100" height="100" class="smallpic" alt="" />
{/if}

I would like to use the rand number in $item.tn_pic1

I have tried {$item.tn_pic{$yourVar}} but get an error as I have 2 {}

So what I am wanting is {$item.tn_picX}

X being the rand number.

I am new'ish to smarty and not really sure how to sort it.

$item. pull from the database via a .php file

Upvotes: 0

Views: 36

Answers (1)

dev-null-dweller
dev-null-dweller

Reputation: 29462

You have to create whole key you want to read, assign it into variable and then access it without additional set of curly braces, something like:

{capture assign=pictureKey}tn_pic{rand(1,$item.pic_count)}{/capture}
<img src="users/images/{$item.id}/{$item[$pictureKey]}" width="100" height="100" class="smallpic" alt="" />

Upvotes: 0

Related Questions