Resolved: $smarty.foreach.entry_loop.last

Discussion corner for Developers of Serendipity.
Post Reply
Don Chambers
Regular
Posts: 3652
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Resolved: $smarty.foreach.entry_loop.last

Post by Don Chambers »

I'm working on a template where I use serendipity_fetchPrintEntries several consecutive times to get entries for a specific category.... so, I'll get 5 entries from category 1, then 5 entries for category 2, then 5 entries for category 3.

I am fetching with the same entries_custom.tpl file each time. I want to add a unique classname to the LAST entry for each of these lists so I have something like this:

Index.tpl:

Code: Select all

{serendipity_fetchPrintEntries limit="0,5" category="1" noCache="true" fetchDrafts="false" full="false" use_footer="false" template="entries_custom.tpl"}
entries_custom.tpl:

Code: Select all

{foreach from=$entries item="dategroup"}
    {foreach from=$dategroup.entries item="entry" name="entry_loop"}
        <div class="all_entries{if $smarty.foreach.entry_loop.last} last_entry{/if}">
The first time through this process (ie, fetching for my first category) - it works fine.... entries 1-4 get the class "all_entries" and the 5th entry gets the class "all_entries last_entry", just as expected.

However, when I then fetch for the next categories, they are all receiving the last_entry class, which means $smarty.foreach.entry_loop.last is evaluating to true. I've tried noCache and unique block names on the fetchPrintEntries, but nothing seems to be resetting that foreach.last.

Ideas?
Last edited by Don Chambers on Tue Feb 26, 2008 12:10 am, edited 1 time in total.
=Don=
judebert
Regular
Posts: 2478
Joined: Sat Oct 15, 2005 6:57 am
Location: Orlando, FL
Contact:

Post by judebert »

Does this still happen if you change the limit for each category? I assume more than 5 entries exist for each fetched category, the entries printed are actually from the fetched category, the entries printed contain valid data, etc; the only problem is that they have the wrong class assigned.

Very odd behavior.
Judebert
---
Website | Wishlist | PayPal
Don Chambers
Regular
Posts: 3652
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Post by Don Chambers »

I do not want to change the limit for each category...

Yes, more than 5 entries exist for each category...

Yes, the entries fetched are from the correct category, which is different each time....

Yes, the entries contain valid data, no special characters, etc.

and... yes. The only issue is that the wrong class is assigned because the $smarty.foreach.entry_loop.last is evaluating to "true" for each consecutive serendipity_fetchPrintEntries call.
=Don=
judebert
Regular
Posts: 2478
Joined: Sat Oct 15, 2005 6:57 am
Location: Orlando, FL
Contact:

Post by judebert »

Don Chambers wrote:I do not want to change the limit for each category...
Not as a permanent solution, but as a temporary fact-finding mission.

But don't bother; I think the problem may be described on the Smarty forums.

I know these aren't exactly "nested" foreach loops, but let's give something a try anyway.

Index.tpl:

Code: Select all

{assign var='fetch' value='cat1'}
{serendipity_fetchPrintEntries limit="0,5" category="1" noCache="true" fetchDrafts="false" full="false" use_footer="false" template="entries_custom.tpl"}
Of course, assign a new value to 'fetch' before each call to serendipity_fetchPrintEntries.

entries_custom.tpl:

Code: Select all

<code>DEBUG: fetch name = {$fetch}</code>
{foreach from=$entries item="dategroup"}
    {foreach from=$dategroup.entries item="entry" name=$fetch}
        <div class="all_entries{if $smarty.foreach.$fetch.last} last_entry{/if}">
I believe that'll work with any Smarty >= 2.6.0. It gives the foreach loop a unique name every time we call the entries_custom.tpl.

I'll keep looking to see if I can find something a little more elegant.
Judebert
---
Website | Wishlist | PayPal
Don Chambers
Regular
Posts: 3652
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Post by Don Chambers »

Resolved - needed to know I was at the last for both loops, not just the second loop.
=Don=
Post Reply