sidebar.tpl: SMARTY expression "every second item"?

Skinning and designing Serendipity (CSS, HTML, Smarty)
Post Reply
aschlemmer
Regular
Posts: 70
Joined: Fri Feb 23, 2007 6:13 pm
Location: Freiburg/Germany
Contact:

sidebar.tpl: SMARTY expression "every second item"?

Post by aschlemmer »

Hi!

My current left sidebar section in sidebar.tpl reads like this:

Code: Select all

{if $pluginside == 'Left'}{foreach from=$plugindata item=item}{if $item.class == "serendipity_categories_plugin"}<div class="container_category"><a id="sectionmenu"></a>
	{elseif $item.class == "serendipity_plugin_staticpage"}<div class="container_staticmenu">
	{else}<div class="serendipitySideBarItem announce container_{$item.class}">
	{/if}
	{if $item.title != ""}<h2 class="serendipitySideBarTitle {$item.class}">{$item.title}</h2>{/if}<div class="serendipitySideBarContent">{$item.content}</div>
	</div>
Now I like Smarty to render a certain additional <div> after every second siderbar item, like this:

Code: Select all

<div class="serendipitySideBarItem ...">Item 1</div>
<div class="serendipitySideBarItem ...">Item 2</div>
<div class="additional">Added by Smarty after every second sidebaritem (after 2,4,6, etc.)</div>
<div class="serendipitySideBarItem ...">Item 3</div>
<div class="serendipitySideBarItem ...">Item 4</div>
<div class="additional">Added by Smarty after every second sidebaritem (after 2,4,6, etc.)</div>
...
Any ideas someoneon how a nice Smarty expression for this task could look like? Thanks in advance,
Achim
www.ms-reporter.de
Pharma-unabhängige Infos für Multiple Sklerose-Betroffene
schnellze.it
Einzigartiges Zusammenspiel aus Popfetcher und s9y
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: sidebar.tpl: SMARTY expression "every second item"?

Post by garvinhicking »

Hi!

You can give a name to your foreach loop:

{foreach from=... item=... name=MYNAME}
{if $smarty.foreach.MYNAME.index % 2}
... is odd ...
{else}
... is even ...
{/if}
{/foreach}

HTH,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Don Chambers
Regular
Posts: 3652
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Re: sidebar.tpl: SMARTY expression "every second item"?

Post by Don Chambers »

garvinhicking wrote:Hi!

You can give a name to your foreach loop:

{foreach from=... item=... name=MYNAME}
{if $smarty.foreach.MYNAME.index % 2}
... is odd ...
{else}
... is even ...
{/if}
{/foreach}

HTH,
Garvin
Same concept, but I prefer this:

Code: Select all

{foreach from=... item=... name=MYNAME}
  {if $smarty.foreach.MYNAME.iteration is odd}
     ... is odd ...
  {else}
     ... is even ...
  {/if}
{/foreach}
=Don=
aschlemmer
Regular
Posts: 70
Joined: Fri Feb 23, 2007 6:13 pm
Location: Freiburg/Germany
Contact:

Re: sidebar.tpl: SMARTY expression "every second item"?

Post by aschlemmer »

Dear all,

thanks much for the instant help.
Today, I found the time to check the even/odd mechanism and yes, it works!
(I used the first suggested solution.)

Kind regards,
Achim
www.ms-reporter.de
Pharma-unabhängige Infos für Multiple Sklerose-Betroffene
schnellze.it
Einzigartiges Zusammenspiel aus Popfetcher und s9y
Post Reply