Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

How do i assign an array WITHIN Smarty template ?

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
sarahfoxnz
Smarty Regular


Joined: 11 Apr 2010
Posts: 76

PostPosted: Sat Oct 26, 2019 12:20 am    Post subject: How do i assign an array WITHIN Smarty template ? Reply with quote

Hi,

I've added simple values within a smarty foreach before which is easy enough, However I am now trying to add totals within / to an ARRAY - but getting errors.

Code:

{assign var="Levels" value=""}  // Blank array

...

....

(within a foreach loop)

{assign var=Levels.$va.level value=$Levels.$va.level+1}




ERROR: Fatal error: Uncaught --> Smarty Compiler: Syntax error in template

Basically $va.level is a value I want to capture - & i'm counting +1 each time that level is used.

Is there a way that I can 'initialise' an array - is that different to the above starting assign ?

also, how do I add 1 each time ?
Back to top
View user's profile Send private message
sarahfoxnz
Smarty Regular


Joined: 11 Apr 2010
Posts: 76

PostPosted: Sat Oct 26, 2019 6:58 am    Post subject: Reply with quote

Got it.

Before the foreach
Code:

{assign "levels" []}


within the loop

Code:

 {assign var="temp" value=$va.level}
{if ! isset($levels.$temp)}
{$levels.$temp = 0}
{/if}

{$levels.$temp = $levels.$temp+1}

Back to top
View user's profile Send private message
sarahfoxnz
Smarty Regular


Joined: 11 Apr 2010
Posts: 76

PostPosted: Sat Oct 26, 2019 11:19 pm    Post subject: Sort an array WITHIN Smarty Reply with quote

FURTHER: Ive got the array part working. (the new array is created WITHIN Smarty)

However when i do a foreach on the NEW array - It is not sorted. Its sowing the array but in the same order it was created.

I have Smarty 3.1.33. Is there a sort option within Smarty ? Or will i need to alter my main PHP script to create an unnecessary array ?
Back to top
View user's profile Send private message
sarahfoxnz
Smarty Regular


Joined: 11 Apr 2010
Posts: 76

PostPosted: Sun Oct 27, 2019 2:01 am    Post subject: Re: Sort an array WITHIN Smarty Reply with quote

sarahfoxnz wrote:
FURTHER: Ive got the array part working. (the new array is created WITHIN Smarty)

However when i do a foreach on the NEW array - It is not sorted. Its sowing the array but in the same order it was created.

I have Smarty 3.1.33. Is there a sort option within Smarty ? Or will i need to alter my main PHP script to create an unnecessary array ?


Code:

{$tmp = ksort($levels)}
{foreach from=$levels key=ka item=va}


In this case $tmp is not used anywhere - Its just a temporary variable.

Can this be updated to the Smarty documentation ? Took me hours to find it.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP