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 to show the first x elements of an array

 
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
wvdploeg
Smarty Rookie


Joined: 05 Aug 2003
Posts: 19

PostPosted: Mon Aug 18, 2003 4:57 pm    Post subject: How to show the first x elements of an array Reply with quote

I want to do something in smarty, but I do not know how to realize it.
I have an array with information and I want to show only the first x elements of that array. The problem is that I do not know in advance how many elements the array has, it can be more or less then the number of elements I want to show the content of.

How to show an array is clear to me (foreach or section), but how can I 'break' out of it after x elements ?

Thanks in advance
Wilko
Back to top
View user's profile Send private message
CirTap
Smarty Pro


Joined: 04 Jun 2003
Posts: 106

PostPosted: Mon Aug 18, 2003 5:20 pm    Post subject: Reply with quote

Hi,
For a template-only solution you'd require {section .. max=##}, but I'd limit the array in advance within the PHP script that assign()s it to Smarty.
It has to be created somewhere and if the limit exceeds, just stop populating it any more and assign() the now limited array as usual. This delimiter can be something simple like a counter you'd increase in a while() or for() loop.
[php:1:76d1caa4f3]$max_items = 10;
$cnt = 0;
$myArray = array();
while ($row = $DB->fetch_row()) {
$myArray = $row[...];
$cnt++;
if ($cnt > $max_items) break; // enough is enough: stop here.
}
$smarty->assign('LimitedArray', $myArray);[/php:1:76d1caa4f3]
Of course, with a DB select you still have LIMIT to reduce the amount of data even before.
No matter how, this way you do not only reduce the load of unused data but also prevent any conditions/calculations inside the template.

Have fun,
CirTap
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Mon Aug 18, 2003 5:29 pm    Post subject: Reply with quote

Code:

{section  name=foo loop=$array}
{if $smarty.section.iteration<=$x}


{/if}
{/section}


the same works with "foreach" instead of "section". there is no break in smarty. if you feel the above is inefficient (which is not necessarily the case) then you have to truncate your in php before assigning it to smarty.

an alternative would be:
Code:

{section name=foo loop=$array|@array_slice:0:$x}
...
{/section}

(it's a php-function used as a modifier, hence not very designer-friendly Smile )
Back to top
View user's profile Send private message Send e-mail Visit poster's website
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Mon Aug 18, 2003 5:32 pm    Post subject: Reply with quote

arrrg!

CirTap's max=$x is the best solution IMHO, it's just made for that.
i forgot about this option.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
wvdploeg
Smarty Rookie


Joined: 05 Aug 2003
Posts: 19

PostPosted: Mon Aug 18, 2003 5:54 pm    Post subject: Thanks... Reply with quote

Wow... that's a fast answer... thanks.
Back to top
View user's profile Send private message
CirTap
Smarty Pro


Joined: 04 Jun 2003
Posts: 106

PostPosted: Mon Aug 18, 2003 6:16 pm    Post subject: Reply with quote

messju wrote:
arrrg!
CirTap's max=$x is the best solution IMHO, it's just made for that.
i forgot about this option.

... and I just RTFM Wink

Have fun,
CirTap
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Mon Aug 18, 2003 7:05 pm    Post subject: Reply with quote

There may be instances where stopping a loop could be beneficial, but in general I'd recommend passing only what is required to Smarty. So, only pass the exact array elements you want displayed instead of figuring out how to stop the loop prematurely in the template. This keeps the template logic as slim as possible, doesn't assign unnecessary data and puts the logic in the proper place (IMHO)
Back to top
View user's profile Send private message Visit poster's website
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Mon Aug 18, 2003 7:54 pm    Post subject: Reply with quote

mohrt wrote:
There may be instances where stopping a loop could be beneficial, but in general I'd recommend passing only what is required to Smarty. So, only pass the exact array elements you want displayed instead of figuring out how to stop the loop prematurely in the template. This keeps the template logic as slim as possible, doesn't assign unnecessary data and puts the logic in the proper place (IMHO)


100% agreed. but sometimes assigning is done in a bulk way and only the template decides that a sub-portion should be displayed. the same workflow may be used to assign an overview of all entries and an info-box that displays only the few latest entries. i'd only give away the convienience handling them both the same way as much as possible when performance bites me. but i wouldn't optimize in advance in this particular case.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
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