|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
sarahfoxnz Smarty Regular
Joined: 11 Apr 2010 Posts: 76
|
Posted: Mon Feb 15, 2016 9:40 am Post subject: Foreach - Do something when value changes ?? |
|
|
Hello.
frequently, i want to do a foreach / loop of an array & display the information on the screen.
I currenty use ASSIGN to
a) get one of the values & assign it to a temporary value
b) compare the temporary value to the OLD temporary value (2 separate values).
if it changes - then do something (display extra HTML), and then assign 'old' temporary value with the new / current temporary value.
Ie - i have 2-4 rows of data within the same 'group', & when the group changes, I display a new heading etc....
this will usually be 5-10 lines of Smarty code do do the assigning/comparing
I was wondering if there was a faster way with a single built-in functionto do this.
"Compare this value with the previous value, if it has changed - do something"
(or if it has NOT changed - do something). |
|
Back to top |
|
CrazyTemplate Smarty Rookie
Joined: 12 Sep 2011 Posts: 31
|
Posted: Mon Feb 15, 2016 2:36 pm Post subject: |
|
|
So you currently have something like this?
Code: |
{foreach $VAR as $V}
{if $V != $PREVIOUS}
do something
{else}
do something else
{/if}
{/foreach}
|
I'm not sure there is much else that you can do to optimize the above, unless the values somehow can be compared before being passed to Smarty. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Mon Feb 15, 2016 5:42 pm Post subject: |
|
|
I'm leaning to the "you're doing it wrong" side.
Remove business logic from template. |
|
Back to top |
|
sarahfoxnz Smarty Regular
Joined: 11 Apr 2010 Posts: 76
|
Posted: Tue Feb 16, 2016 8:57 am Post subject: |
|
|
CrazyTemplate wrote: | So you currently have something like this?
[code]
{foreach $VAR as $V}
{if $V != $PREVIOUS}
do something
|
Yes i've got something like that..
it works - Though its a bit messy (extra Smarty code that )..
I'm sure in future there can be a built-in function - to put headers etc in-between where arrays change their values... |
|
Back to top |
|
sarahfoxnz Smarty Regular
Joined: 11 Apr 2010 Posts: 76
|
Posted: Tue Feb 16, 2016 8:59 am Post subject: |
|
|
AnrDaemon wrote: | I'm leaning to the "you're doing it wrong" side.
Remove business logic from template. |
Normally yes..
Ive done a LOT of processing in my PHP script.. But the display side in the smarty template, i want to put headers etc,.. when the array changes its primary value (& loops through the sub-arrays)
EDIT: The way i'm doing it now - does work.. im just wondering if there is (or can be) a built-in function etc to save on coding...)
maybe in future - & if people do want it / it is beneficial |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Tue Feb 16, 2016 9:57 am Post subject: |
|
|
Headers is a part of business logic.
End chapter, end book. (q) |
|
Back to top |
|
|
|
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
|