Foreach.first problems

Skinning and designing Serendipity (CSS, HTML, Smarty)
Post Reply
Krzyzak
Regular
Posts: 41
Joined: Mon Oct 16, 2006 1:13 pm

Foreach.first problems

Post by Krzyzak »

Hi.
I wish to show big icon in first news(and only there), so I`ve used smarty.foreach.first function.
So, I`ve edit my entreis.tpl:

Code: Select all

 {serendipity_hookPlugin hook="entries_header" addData="$entry_id"}
    {foreach from=$entries item="dategroup"}  
{foreach name="entryloop" from=$dategroup.entries item="entry"} 
  <div class="news">
{if $smarty.foreach.entryloop.first}
first news.
<!-- do some more stuff -->
{else}
second news.
<!-- and something more... -->
{/if}
<!-- here is other stuff-->
{/foreach}

{/foreach} 
...And that outputs me:

Code: Select all

Entry num | Text:
1         | First news
2         | First news
3         | second news
4         | first news
5         | second news
Could someone help me?
Sorry for my language errors- It`s not my native language.
judebert
Regular
Posts: 2478
Joined: Sat Oct 15, 2005 6:57 am
Location: Orlando, FL
Contact:

Post by judebert »

You see, we're inside two foreach loops here:

Code: Select all

{foreach from=$entries item="dategroup"}
    {foreach name="entryloop" from=$dategroup.entries item="entry"}
        {if $smarty.foreach.entryloop.first}
           First News!
        {/if}
    {/foreach}
{/foreach}
So, we're looking at a data structure that's grouped into... well... groups:

Code: Select all

date1
  entry1
date2
  entry2
  entry3
date3
  entry4
  entry5
In this example, we go through the outside loop three times: once for each date. We go through the inside loop once per entry. So for entries 1, 2, and 4, the inside loop is on its first iteration (exactly what you're seeing).

You could look for the .first on the outside loop, but then everything in the first date-group would get highlighted. (So if entry1 and entry2 were in date1, both would get the big icons.)

To get only the first entry, you need this:

Code: Select all

{foreach name="dateloop" from=$entries item="dategroup"}
    {foreach name="entryloop" from=$dategroup.entries item="entry"}
        {if $smarty.foreach.dateloop.first and $smarty.foreach.entryloop.first}
           First News!
        {/if}
    {/foreach}
{/foreach}
However, this will fail if you have sticky entries, since they always fall in the first dategroup, and you probably want the first actual NEWS item highlighted.

To get the *really* first entry, skipping stickies, I think you need this:

Code: Select all

{counter start=1}
{foreach from=$entries item="dategroup"}
    {foreach from=$dategroup.entries item="entry"}
        {if not $dategroup.is_sticky}
          {counter print=false assign='entrycount'}
        {/if}
        {if $entrycount == 1}
           First News!
        {/if}
    {/foreach}
{/foreach}
You could do stuff with {assign}, too, of course, but then your {if} gets really big. {counter} is pretty fast, and maybe you'll figure out something else to do with the entry count. (I like assigning multiple classes, one of which has the variable value in it, myself: <img src="whatever" class="newsicon newsicon{$entrycount}"/> provides tags for styling every icon, the first icon, and any later icons.)
Judebert
---
Website | Wishlist | PayPal
akb
Regular
Posts: 35
Joined: Tue Aug 22, 2006 6:43 pm
Contact:

Post by akb »

hi judebert,

just a quick correction to this:

a) the counter has to be initialized with 0, not 1. if you initialize it with 1 the first entry is incremented to 2.

b) the initialization outside the loop has to get a print=false too, otherwise the 1 is printed above the loop.

Code: Select all

{counter start=0 print=false}
{foreach from=$entries item="dategroup"}
    {foreach from=$dategroup.entries item="entry"}
        {if not $dategroup.is_sticky}
          {counter print=false assign='entrycount'}
        {/if}
        {if $entrycount == 1}
           First News!
        {/if}
    {/foreach}
{/foreach}
Best regards,

Arne
judebert
Regular
Posts: 2478
Joined: Sat Oct 15, 2005 6:57 am
Location: Orlando, FL
Contact:

Post by judebert »

Of course; thanks for the correction!

Krzyzak, I assume this worked for you?
Judebert
---
Website | Wishlist | PayPal
Post Reply