Is there some way to calculate index inside the array brackets? I’ll explain what i mean
I created an array in Smarty by exploding a string that i got from php. Now i want to get the value at the last index. So i tried this
{assign var='array' value=','|explode:$no} //$no is the comma separated string
<h1>{$array[$array|@count]}</h1> //this should be a blank value because i am going past the last index
this gave me error
Fatal error: Smarty error: [in categories.tpl line 8]: syntax error: unrecognized tag: $array[$array|@count]
then i tried
{assign var='array' value=','|explode:$no} //$no is the comma separated string
{assign var='c' value=$array|@count}
<h1>{$array[$c-1]}</h1> //$c is the count
this gave yhe following error
Fatal error: Smarty error: [in categories.tpl line 8]: syntax error: unrecognized tag: $array[$c-1]
Finally the code which worked was this
{assign var='array' value=','|explode:$no}
{assign var='c' value=$array|@count}
{assign var='c1' value=$c-1}
<h1>{$array[$c1]}</h1>
Now this is stupid because i have to first calculate the index and store in it a variable to use it. Cant i use an “on the fly generated index“?
Thanks.
As per my comment, I still first recommend doing the logic (or at least assign the array to the presentation layer instead of exploding a string) in your PHP or write a plugin or modifier.
From your code it seems that you’d like to get the last item in the string. I think you might be able to use PHP’s
array_popfunction.