[SOLVED] news module: show first 3 items with defferent template then the rest

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Post Reply
louisk
Forum Members
Forum Members
Posts: 89
Joined: Wed May 30, 2007 7:40 pm

[SOLVED] news module: show first 3 items with defferent template then the rest

Post by louisk »

I would like to give the first three news items a different markup then the others.
This is a smarty question.

What is the code for that?

I understand how to have the first item have a different markup then the rest.

Code: Select all

{foreach from=$items item=entry name=foo}
{if $smarty.foreach.foo.first}
...
{else}
...
{/if}
{/foreach}

Now I would like to have the first 3 items.
Last edited by louisk on Wed Apr 28, 2010 11:22 am, edited 1 time in total.
User avatar
tallphil
Forum Members
Forum Members
Posts: 20
Joined: Sat Jan 30, 2010 8:01 pm
Location: Cambridge, UK

Re: news module: show first 3 items with defferent template then the rest

Post by tallphil »

You can add key=i to your foreach statement, and then a nested if statement to count the iterations of the loop (assuming that your array is a normal numerical array, where the keys are 0,1,2,3...)

Code: Select all

{foreach from=$items item=entry key=i name=foo}
{if i < 3}
    {* Markup for first three items *}
{else}
    {* Markup for rest *}
{/if}
{/foreach}

See http://www.smarty.net/manual/en/languag ... oreach.php for more information
louisk
Forum Members
Forum Members
Posts: 89
Joined: Wed May 30, 2007 7:40 pm

Re: news module: show first 3 items with defferent template then the rest

Post by louisk »

Thanks tallphil for the quick reply.
However, it's not working. It's now displaying all posts with the markup for the first three items ???
I have read the smarty docs but I need some help  :-\
User avatar
tallphil
Forum Members
Forum Members
Posts: 20
Joined: Sat Jan 30, 2010 8:01 pm
Location: Cambridge, UK

Re: news module: show first 3 items with defferent template then the rest

Post by tallphil »

Oops, probably because I missed the dollar sign off the i... Try:

Code: Select all

{foreach from=$items item=entry key=i name=foo}
{if $i < 3}
    {* Markup for first three items *}
{else}
    {* Markup for rest *}
{/if}
{/foreach}
louisk
Forum Members
Forum Members
Posts: 89
Joined: Wed May 30, 2007 7:40 pm

Re: news module: show first 3 items with defferent template then the rest

Post by louisk »

Yes great!
thanks a million  ;D
User avatar
tallphil
Forum Members
Forum Members
Posts: 20
Joined: Sat Jan 30, 2010 8:01 pm
Location: Cambridge, UK

Re: news module: show first 3 items with defferent template then the rest

Post by tallphil »

No problem - I was looking for the same thing last week so it was fresh in my mind ;)
Last edited by tallphil on Wed Apr 28, 2010 11:31 am, edited 1 time in total.
Post Reply

Return to “Modules/Add-Ons”