|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
balupton Smarty Rookie
Joined: 15 Feb 2008 Posts: 11 Location: Perth, Australia
|
Posted: Sat Mar 29, 2008 7:34 pm Post subject: Best way to do: first last odd even |
|
|
Has anyone come up with a better way to do the css class names for first, last, odd and even?
Here is what I have come up with:
Code: | <ul class="header inline menu">
{foreach from=$menu_items item=i name=menu_items}
{cycle values='odd,even' assign='class'}
{if $smarty.foreach.menu_items.first}
{assign var='class' value=$class|cat:' first'}
{elseif $smarty.foreach.menu_items.last}
{assign var='class' value=$class|cat:' last'}
{/if}
<li class="{$class}"><a href="index.php?page={$i.code}">{$i.title}</a></li>
{/foreach}
</ul> |
Surely there must be an simpler way of doing this? Like an inbuilt thing into smarty's foreach function...
Last edited by balupton on Sat Mar 29, 2008 8:56 pm; edited 1 time in total |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Sat Mar 29, 2008 8:26 pm Post subject: |
|
|
I think there is no better way. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Sun Mar 30, 2008 12:11 am Post subject: |
|
|
Here is a way to get rid of the assign, thats about it, aside from assigning class values in PHP to the $menu_items array elements.
Code: | <ul class="header inline menu">
{foreach from=$menu_items item=i name=menu_items}
{if $smarty.foreach.menu_items.first}
<li class="{cycle values='odd,even'} first">
{elseif $smarty.foreach.menu_items.last}
<li class="{cycle values='odd,even'} last">
{else}
<li class="{cycle values='odd,even'}">
{/if}
<a href="index.php?page={$i.code}">{$i.title}</a></li>
{/foreach}
</ul> |
|
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Sun Mar 30, 2008 12:16 am Post subject: |
|
|
Another option to cleanup the code, write a custom function to create your class name, passing it the foreach index and the array size:
Code: |
<ul class="header inline menu">
{foreach from=$menu_items item=i name=menu_items}
<li class="{getclass index=$smarty.foreach.menu_items.index total=$menu_items|@count}"><a href="index.php?page={$i.code}">{$i.title}</a></li>
{/foreach}
</ul> |
|
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|