View previous topic :: View next topic |
Author |
Message |
wvdploeg Smarty Rookie
Joined: 05 Aug 2003 Posts: 19
|
Posted: Mon Aug 18, 2003 4:57 pm Post subject: How to show the first x elements of an array |
|
|
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 |
|
CirTap Smarty Pro
Joined: 04 Jun 2003 Posts: 106
|
Posted: Mon Aug 18, 2003 5:20 pm Post subject: |
|
|
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Mon Aug 18, 2003 5:29 pm Post subject: |
|
|
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 ) |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Mon Aug 18, 2003 5:32 pm Post subject: |
|
|
arrrg!
CirTap's max=$x is the best solution IMHO, it's just made for that.
i forgot about this option. |
|
Back to top |
|
wvdploeg Smarty Rookie
Joined: 05 Aug 2003 Posts: 19
|
Posted: Mon Aug 18, 2003 5:54 pm Post subject: Thanks... |
|
|
Wow... that's a fast answer... thanks. |
|
Back to top |
|
CirTap Smarty Pro
Joined: 04 Jun 2003 Posts: 106
|
Posted: Mon Aug 18, 2003 6:16 pm Post subject: |
|
|
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
Have fun,
CirTap |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Mon Aug 18, 2003 7:05 pm Post subject: |
|
|
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Mon Aug 18, 2003 7:54 pm Post subject: |
|
|
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 |
|
|