Page 1 of 1

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

Posted: Wed Apr 28, 2010 10:33 am
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.

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

Posted: Wed Apr 28, 2010 10:55 am
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

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

Posted: Wed Apr 28, 2010 11:02 am
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  :-\

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

Posted: Wed Apr 28, 2010 11:06 am
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}

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

Posted: Wed Apr 28, 2010 11:08 am
by louisk
Yes great!
thanks a million  ;D

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

Posted: Wed Apr 28, 2010 11:19 am
by tallphil
No problem - I was looking for the same thing last week so it was fresh in my mind ;)