1
LazyBadger
First block in foreach cycle - how to detect?

Which I can use for detecting output of first block (topmost) inside placeholder's cycle?
Quis custodiet ipsos custodes?

Webmaster of
XOOPS2.RU
XOOPS Modules Proving Ground
XOOPS Themes Exhibition

2
Dave_L
Re: First block in foreach cycle - how to detect?
  • 2005/9/24 11:56

  • Dave_L

  • XOOPS is my life!

  • Posts: 2277

  • Since: 2003/11/7


Is this for PHP, or for Smarty (templates)?

For PHP, you can use a flag:

$first_iteration true;
foreach (
$array) {
   if (
$first_iteration) {
      ...
      
$first_iteration false;
   }
   ...
}


You could use the same approach in Smarty, but Smarty also has an iteration property that can be tested within the loop, so you don't need to define a separate flag.

3
LazyBadger
Re: First block in foreach cycle - how to detect?

Smarty, pure Smarty for theme. PHP isn't a problem
Now I use for (first|second|other) this snippet
<{assign var='pos' value='top'}>
<{foreach 
item=block from=$xoops_lblocks}>

<{if 
$pos == 'top'}>
   <
div id="lnav1"><{$block.content}> div>
<{elseif 
$pos == 'second'}>
   <
div id="ltn1"> <span class="title"><{$block.title}>spandiv>
   <
div id="lnav2"><{$block.content}>div>
<{else}>
   <
div id="ltn2"> <span class="title"><{$block.title}>spandiv>
   <
div id="lnav2"><{$block.content}>div>
<{/if}>

<{if 
$pos == 'second'}>
   <{
assign var='pos' value='rest'}>
<{/if}>
<{if 
$pos == 'top'}>
   <{
assign var='pos' value='second'}>
<{/if}>

<{/foreach}>


but I hate this dirty code
Quis custodiet ipsos custodes?

Webmaster of
XOOPS2.RU
XOOPS Modules Proving Ground
XOOPS Themes Exhibition

4
Mithrandir
Re: First block in foreach cycle - how to detect?

<{foreach item=block from=$xoops_lblocks [b]name=leftblocks[/b]}>
    <{if 
$smarty.foreach.leftblocks.first}>
        <
div id="lnav1"><{$block.content}> div>
    <{elseif 
$smarty.foreach.leftblocks.iteration == 2}>
        <
div id="ltn1"> <span class="title"><{$block.title}>spandiv>
        <
div id="lnav2"><{$block.content}>div>
    <{else}>
        <
div id="ltn2"> <span class="title"><{$block.title}>spandiv>
   <
div id="lnav2"><{$block.content}>div>
    <{/if}>
<{/foreach}>
"When you can flatten entire cities at a whim, a tendency towards quiet reflection and seeing-things-from-the-other-fellow's-point-of-view is seldom necessary."

Cusix Software

Login

Who's Online

145 user(s) are online (100 user(s) are browsing Support Forums)


Members: 0


Guests: 145


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Nov 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits